1

I have a class and a sub-class, I'd like to pass the whole of the self of the class to the sub-class. I can pass self over to the new class explicitly easily enough, e.g.

class foo:
    def __init__(self, a, b):
        self.a = a
        self.b = b
        self.c = 'foo'
        
    def foo_method(self):
        print('a foo method')

class bar(foo):
    def __init__(self, foo_object):
        self.a = foo_object.a
        self.b = foo_object.b
        self.c = foo_object.c
    
    def bar_method(self):
        print('a bar method')

foo_object = foo(a = 'a', b = 'b')
bar_object = bar(foo_object)

bar_object.a

Is there a more succinct way to pass these over? Something like:

class bar(foo):
    def __init__(self, foo_object):
        self = self.foo_object

Update:

Thanks https://stackoverflow.com/users/10104112/bastien-antoine, the following solution worked:


class bar(foo):

    def __init__(self, foo_object):
        self.__dict__ = foo_object.__dict__.copy()
    
    def bar_method(self):
        print('a bar method with ' + str(self.c))

  • As you are inheriting 'bar' from 'foo', you don't need to explicitly set a, b, c in bar. You can directly call foo's constructor from bar using super keyword. In this way you'll be able to use foo's members in bar. – Abhishek Kuvalekar Aug 03 '20 at 16:14
  • You might already know this, but a typical subclass does not usually take an instance of the parent class as an argument. Certainly there are use-cases where this is desirable, for example [decorators](https://en.wikipedia.org/wiki/Decorator_pattern), but you should think about whether this is truly necessary for your own scenario. – Kevin Aug 03 '20 at 16:15
  • Good point. I suppose the question is the same if `bar` was an entirely different class, where the class foo is being passed in as a parameter. – user3322865 Aug 04 '20 at 13:07

2 Answers2

2

Have you tried the copy builtins library?

Otherwise I think you can easily implement your own .copy() method that would copy the values from the old object __dict__ to the new one. Something like this:

class MyObject:

    a = None

    def set_default_values(self):
        self.a = 1

    def copy(self, old):
        if type(self) == type(old):
            self.__dict__ = old.__dict__.copy()
        else:
            raise TypeError('Wrong type')

if __name__ == "__main__":
    obj_1 = MyObject()
    print(obj_1.a)

    obj_1.set_default_values()
    print(obj_1.a)

    obj_2 = MyObject()
    print(obj_2.a)

    obj_2.copy(obj_1)
    print(obj_2.a)

Note that I've added a type checking to be sure that you copy attributes that would exist otherwise, but I think simply self.__dict__ = old.__dict__.copy() would work fine, thought you might end up with attributes you might not suppose to have in the new object.

Hope this helps!

bastantoine
  • 582
  • 4
  • 21
0

I think that you can do that with

    class bar(foo):
        def __init__(self):
            super(bar, self).__init__()

with this code, you ran the init function for the subclass