1

Say class A has an init that is overloaded? How do I overload the init in the first place where it takes more than 1 argument set?

Also, when extending to a child class how can I make sure the child init is compatible with the overloaded parent init?

b1g1
  • 11
  • 1
  • Please consider making this question clearer with some example code demonstrating what you are trying to ask. – dcarson Jan 06 '15 at 02:32

1 Answers1

0

Python does not support function overload since it's a dynamic language, see: Overloaded functions in python?

If I understand you correctly, you need super to call the method of your base class from your child class:

In [95]: class Base(object):
    ...:     def __init__(self, x):
    ...:         print 'in Base.ctor, x is:', x
    ...: 
    ...: class Child(Base):
    ...:     def __init__(self, x, y):
    ...:         super(Child, self).__init__(x)

In [96]: c = Child(1,2)
in Base.ctor, x is: 1

see: Understanding Python super() with __init__() methods

Community
  • 1
  • 1
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • `super(self.__class__, self)` is broken if inheritance continues -- try a `class Grandchild(Child): pass` and see what happens on instantiating if. In Python 2, you need to be explicit in `super`, using `super(Child, self)` in this case. – Alex Martelli Jan 06 '15 at 03:28