0
class speciallist(list):

    def __new__(self): 
        return self

    def custommethod(self,param):
         #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?

0

1 Answer 1

5

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.