1

Is there a way to copy attribute values from the object of base class to the object of derived class?

class Base():
    def __init__(self):
       self.a = 10
       self.b = None

class Derived(Base):
       self.c = 30

def main():
    base = Base()
    base.b = 20
    derived = Derived(base)

I tried to find a way more pythonic to copy values of the object of base class since the base class has a number of variables, but I couldn't find a neat way.

I want the variable "derived" to have its value "a = 10 b = 20 c = 30".

Han
  • 625
  • 2
  • 7
  • 25
  • 1
    This is what I really want. Refer to @Martijn Pieters 's answer. http://stackoverflow.com/questions/21699707/python-how-to-copy-all-attibutes-from-base-class-to-derived-one – Han Oct 08 '15 at 15:44

2 Answers2

2

That is not how to do inheritance. You're passing an instance of the base class to the __init__ of the derived class, which is completely unrelated to inheriting its attributes. Instead, do:

class Base():

    def __init__(self):
       self.a = 10


class Derived(Base):

    def __init__(self):
       super(Derived, self).__init__()  # calls Base.__init__
       self.b = 'abc'


def main():
    base = Base()
    derived = Derived()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Thank you for your valuable comment. What should I do if some attributes are changed after construction. I updated my original question. – Han Oct 08 '15 at 13:22
  • @HanulLee http://stackoverflow.com/help/someone-answers – jonrsharpe Oct 08 '15 at 13:22
  • Sorry, I'm a newbie here. My question was ambiguous. I updated my question. I appreciate your help. – Han Oct 08 '15 at 13:30
  • @HanulLee what are you actually trying to achieve here? Why are you trying to create a derived class instance from a base class instance? Why don't you make the attribute values parameters of `__init__`? Perhaps you should follow an introductory Python OOP tutorial? – jonrsharpe Oct 08 '15 at 13:31
  • I just want to do something like casting the parent object and copy it to child object in pythonic way. I know I can pass the values as parameters, but I'm just curious if there is a better way. – Han Oct 08 '15 at 13:43
  • Why don't you just directly create an object of the child class? – jonrsharpe Oct 08 '15 at 13:46
  • No problem. I think I can directly create an object, and copy the variables as like follows "derived = Derived() derived.a = base.a derived.b = base.b". However, I want to do this with a short code. I tried deepcopy(base), but it didn't work with error "Derived instance has no attribute 'c'". – Han Oct 08 '15 at 14:24
  • Yes, I suppose you could, but *why would you want to*? I think you're going about this fundamentally the wrong way. Have a look at e.g. http://stackoverflow.com/a/32867914/3001761 – jonrsharpe Oct 08 '15 at 14:25
  • I think I found a similar answer what I want to do [link](http://stackoverflow.com/questions/3464061/cast-base-class-to-derived-class-python-or-more-pythonic-way-of-extending-class), but I want to add a new attribute in the derived class. – Han Oct 08 '15 at 14:26
  • I read your answer at the link, and it was a big help for me. Sorry but, I solved the problem with __dict__ method. I really appreciate your help. – Han Oct 08 '15 at 15:42
0

how about using default arguments?

class Base():
    def __init__(self, a=10):
       self.a = a
class Derived(Base):
    def __init__(self, a=10, b=None):
       super().__init__(a)  # python 3
       # Base.__init__(self, a)  # python 2
       self.b = b or 'abc'

def main():
    base = Base()
    derived = Derived()

    print(base.a)
    print(derived.a, derived.b)

main()

# output:
# 10
# 10 abc

also note how you call the 'constructor' __init__ of the base class that takes care of setting self.a.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111