简体   繁体   中英

unbound method __init__() must be called with... (but it doesn't make sense...)

First off, I know to call the base class init method in the sub class' init method, and I am doing that in this situation (see the snippets below)... unfortunately, I can't give you code to reproduce this issue because it doesn't make sense to me why it's happening, and I can't create a simple/stripped down case that reproduces it... So, I wonder if someone else has run across it and knows what else I should be looking for that's possibly causing it.

Now more details: I'm creating a rigging system for Maya and I have a base class called 'RigModule' serving as the basis for pretty much all of the rig related items and nodes I'm creating and managing, and all other rig modules essentially sub class from this - they handle creating, recording data, and rebuilding them in a new maya scene to help keep everything clean and tidy.

I have some 'category' rig modules that serve as a collection of other rig modules which is basically just a way of keeping things organized for recording the data out of Maya. These categories are subclasses of another rig module called 'BaseCategory' in the system. I have one called 'AttributeCategory' which works fine, and another called 'AttrPoseCategory' that errors out every time I try to create an instance of it with this message:

unbound method __init__() must be called with BaseSet instance as first argument (got AttrPoseCategory instance instead)

the subclassing hierarchy (is that what you call it?) goes like this (each of these classes is a subclass of the one above it, and they are all in separate python files):

object  # this is the python base class 'object'
    RigModule
        BaseSet
            BaseCategory

In each of these classes, I am calling the init of the class it is a subclass of... so paired down a bit (I copy/pasted and then removed the non-init stuff):

class BaseSet(RigModule):
    def __init__(self):
        RigModule.__init__(self)
class BaseCategory(BaseSet):
    def __init__(self):
        BaseSet.__init__(self)
class AttributeCategory(BaseCategory):
    def __init__(self):
        BaseCategory.__init__(self)
class AttrPoseCategory(BaseCategory):
    def __init__(self):
        BaseCategory.__init__(self)

As far as I can tell, I'm doing the sub-classes 'correctly' but I don't understand why my 'AttrPoseCategory' class is getting that unbound init method error while 'AttributeCategory' does not (I also have other category classes that are working fine, it's just this one that has a problem). Can you think of anything else I should look for - perhaps within the file or even directory structure elsewhere - that might cause this?

The misunderstanding is, indeed in your call structure:

class AttrPoseCategory(BaseCategory):
    def __init__(self):
        BaseCategory.__init__(self)

Here you are calling BaseCategory as though you would be outside of any sub-class - that is, you're calling a class method (not an instance method) of a class. With instance methods, the first self parameter is automatically spliced in with the value of the instance you're calling. However, this is not the case with class methods, which instead take the class as its first argument.

So what you've got here is a class you're calling a method on, not an instance. You're sending it a class, so that part works, however you're sending self . In the scope you make the call, self is actually AttrPoseCategory - hence your error.

As noted in other answers, using super().__init__() will properly call the next parent class's init method. ( super(Child, self).__init__() in Python 2.7 iirc).

FWIW, it's unlikely you would be experiencing this problem in one class and not another. I would try with both classes in the same file, to eliminate the possibility they're importing things differently. Checking that their other dunder methods ( __new__ in particular) are the same might matter as well.

你应该使用super().__init__()而不是ClassName.__init__(self) super().__init__()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM