我正在尝试定义一个将 python 列表作为其输入参数之一的方法。相比之下,常规函数接受列表作为输入参数没有问题。怎么来的?
# Simple function that works
def func(param1, param2):
for item in param1:
print item+" "+param2
var1 = ['sjd', 'jkfgljf', 'poipopo', 'uyuyuyu']
var2 = 'is nonsense'
func(var1, var2)
# Simple function produces the following output:
# sjd is nonsense
# jkfgljf is nonsense
# poipopo is nonsense
# uyuyuyu is nonsense
如果我尝试使用这样的类中的方法获得类似的效果:
# Simple class
class test():
def __init__(self):
pass
def test_method(par1, par2):
for itm in par1:
print itm+" "+par2
# This executes with no error
obj = test()
# This fails
obj.test_method(var1, var2)
# Error message will be:
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: test_method() takes exactly 2 arguments (3 given)
好像我错过了一些非常基本的东西,任何帮助将不胜感激。