2017-06-12 55 views
-1

相同的论点我有一个类的成员,是另一个类的列表。所以,我想是这样Python的:设置一个功能采取与另一个

class primary_class: 
    def __init__(self): 
     self.slist = [] 
    def add_secondary_class(self, <arguments>): 
     self.slist.append(secondary_class(<arguments>)) 

基本上我希望能够通过add_secondary_class参数完全相同的方式我想将它们传递到secondary_class构造。

+2

'* args'和'** kwargs'的路要走。 https://stackoverflow.com/questions/3394835/args-and-kwargs –

+0

你真的尝试过吗?另外,'* args'和'** kwargs'在这里非常有用 – Evert

+0

[相关](https://stackoverflow.com/q/42471083/1222951)。 –

回答

1
def add_secondary_class(self, *args): 
    self.slist.append(secondary_class(*args)) 

,或者如果你有kwargs

def add_secondary_class(self, **kwargs): 
    self.slist.append(secondary_class(**kwargs)) 
+0

唉这不行。使用'** kwargs',我最终得到 'TypeError:add_secondary_class()只需要1个参数(给出8个)' – goi42

+0

你是怎么调用add_secondary_class()函数的? –

+0

'a = primary_class(); a.add_secondary_class()' – goi42