Search code examples
pythonoopinheritanceinitialization

How to get class of object being created in __init__ parameters


So I am trying to create a class with an initialization method that needs to get the type of the object being created in order to properly set the default values of the init arguments.

To give a concrete example with code, say I have the following class:

def __init__(self, foo, bar=type(self).some_class_variable, ham=type(self).some_other_class_variable):
        self.foo = foo
        self.bar = bar
        self.ham = self.some_function(ham)

This is the functionality I am looking for, but my IDE is saying "self" is not defined which I can understand, as self has yet to be instantiated. So my question is how would I go about implementing this properly? I do not want to "hardcode" the class type in where I currently have type(self) because subclasses of this class may have their own values for some_class_variable and some_other_class_variable and I want those subclasses to use the class variable corresponding to their type.

My solution needs to work in Python 3.6 & Python 3.7 but I would really prefer to find a solution that works in all versions of Python 3.6 and later if possible.


Solution

  • I think that you should put it in the body, not the parameter.

    def __init__(self, foo):
            self.foo = foo
            self.bar = type(self).some_class_variable
            self.ham = self.some_function(type(self).some_other_class_variable)
    

    EDIT: If the values are defaults, you can do this:

    default_value = 'default pls'
    def __init__(self, foo, bar=default_value, ham=default_value):
        self.foo = foo
        if default_value == bar: 
            self.bar = type(self).some_class_variable
        if default_value == ham: 
            self.ham = self.some_function(type(self).some_other_class_variable)