0
该类应该接受Keys
属性的参数,但是当我尝试MyUserInstance.Keys('foo')
时,它不起作用。相反,它表示TypeError: Keys() takes exactly 2 arguments (1 given)
。它怎么错了?Python类属性没有正确接受参数
# User
class User:
def __init__(self,
Username = 'New User',
**kwargs):
self.Username = Username
self.__dict__.update(kwargs)
@property
def Keys(self,collection):
try:
return {k:1 for k in db.UserPreferences.find_one({
'Type':'VisibleKeys',
'UserID':self._id,
'CollectionName':collection})['Keys']}
except:
return None
def __repr__(self):
return '<User: "%s">' % self.Username
感谢您的回复,但我认为我已经把你放错了路。为了清晰起见,我编辑了这个问题。假设我已经有一个实例。我只想用'collection'作为参数从我的数据库中返回一个字典。为什么它告诉我我清楚地指出了错误的参数数目,我不知道? – MFB 2012-08-10 03:57:18
是的资本命名使它看起来像班级。但其余答案仍然正确。您使用的是财产错误。他们不采取参议员。删除属性修饰器并使其成为一种常规方法,并且它将按照您使用它的方式工作。 – jdi 2012-08-10 04:05:03
非常感谢并为此感到抱歉。 – MFB 2012-08-10 04:08:29