假设我的代码执行复杂的操作并打印结果,但就本文而言,请说它只是这样做:
class tagFinder:
def descendants(context, tag):
print context
print tag
但是我说这个课程有多个功能。我该如何运行此功能?或者甚至当我说我做python filename.py ..我如何调用该函数并为上下文和标记提供输入?
答案 0 :(得分:4)
~ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import filename
>>> a = tagFinder()
>>> a.descendants(arg1, arg2)
# output
答案 1 :(得分:2)
这会抛出错误
>>> class tagFinder:
... def descendants(context, tag):
... print context
... print tag
...
>>>
>>> a = tagFinder()
>>> a.descendants('arg1', 'arg2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descendants() takes exactly 2 arguments (3 given)
>>>
你的方法首先应该是自我。
class tagFinder:
def descendants(self, context, tag):
print context
print tag
除非'上下文'是指自我。在这种情况下,您可以使用单个参数调用。
>>> a.descendants('arg2')
答案 2 :(得分:0)
您可以在stdin上传递一小段python代码:
python <<< 'import filename; filename.tagFinder().descendants(None, "p")'
# The above is a bash-ism equivalent to:
echo 'import filename; filename.tagFinder().descendants(None, "p")' | python