problem inheriting from list
class speciallist(list):
def __new__(self):
return self
def custommethod(self,par开发者_运维百科am):
#do stuff
return value
listesp = speciallist()
listesp.custommethod(param)
I get "unbound method custommethod() must be called with speciallist instance as first argument"
I thought it was going to call the method from the class, why does it do this?
That's a sneaky one. Short answer, remove the __new__ method from your definition.
The __new__ method is a class method, so it takes the class, not the instance, as its first argument. It's designed to create an instance (possibly of another class) and return it. You're simply returning the class itself, not an instance of it. Python lets you call custommethod from there, but it's not bound to an instance, so it doesn't automatically get self inserted as the first argument.
To set up an instance, use the __init__ method (which takes self as its first argument, but returns nothing).
加载中,请稍侯......
精彩评论