小编har*_*rsh的帖子

如何编写可以使用不同数量的参数调用的方法?

我的代码如下:

class A:
    def TestMethod(self):
        print 'first method'

    def TestMethod(self, i):
        print 'second method', i

ob = A()
ob.TestMethod()
ob.TestMethod(10)
Run Code Online (Sandbox Code Playgroud)

它给出了一个错误..

Traceback (most recent call last):
File "stack.py", line 9, in <module>
    ob.TestMethod()
TypeError: TestMethod() takes exactly 2 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)

如何使用不同数量的参数调用方法?

python

1
推荐指数
1
解决办法
63
查看次数

标签 统计

python ×1