7

I was going through DiveIntoPython and came across this:

Java and Powerbuilder support function overloading by argument list, i.e. one class can have multiple methods with the same name but a different number of arguments, or arguments of different types. Other languages (most notably PL/SQL) even support function overloading by argument name; i.e. one class can have multiple methods with the same name and the same number of arguments of the same type but different argument names. Python supports neither of these; it has no form of function overloading whatsoever. Methods are defined solely by their name, and there can be only one method per class with a given name. So if a descendant class has an __init__ method, it always overrides the ancestor __init__ method, even if the descendant defines it with a different argument list. And the same rule applies to any other method.

Isn't this a major disadvantage that a subclass's __init__ method will always override a superclass's __init__ method? So if I'm initializing some variables and calling some functions in a class class1's __init__, then I derive a subclass class2(class1) of it, I'd have to reinitialize all of class1's variables and call those functions in class2's __init__?

I'm pretty sure I'm misunderstanding all this, so it'd be great if someone clarifies this up.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user1265125
  • 2,608
  • 8
  • 42
  • 65

2 Answers2

14

You're right that defining __init__ in a subclass overrides the superclass's __init__, but you can always use super(CurrentClass, self).__init__ to call the superclass's constructor from the subclass. So, you don't have to "manually" duplicate the superclass's initialization work.

As a side note, even though Python doesn't support method overloading, it supports default arguments (in addition to optional arguments via *args and **kwargs), which means you can easily emulate the behavior of overloaded functions by simply accepting different subsets of arguments in your function/method implementation.

tom
  • 18,953
  • 4
  • 35
  • 35
  • 1
    And if you use the `*args` / `**kwdargs**` method to do some dispatch to specific functions, then you realise it doesn't actually hurt to have the functions named differently anyway :) – Jon Clements Dec 21 '12 at 11:41
  • @tom of course I did that, is super a new word? I'm trying at http://mathcs.holycross.edu/~kwalsh/python/ since I don't have python in this machine – Polyana Fontes Dec 21 '12 at 11:54
  • `super` was added in Python 2.2 (a while ago). The version of Python you're using must be older than that. – tom Dec 21 '12 at 11:57
  • 3
    @tom `super` will not work in all cases, it must be a new style class, this is why I was getting the error. `BaseClass.__init__(self)` will work. Test cases: [Direct call](http://ideone.com/IJperH), [failed super call](http://ideone.com/jLAQ8b), [succeed super call in new style class](http://ideone.com/zEV8PX), [Direct call in new style class](http://ideone.com/Ad3oxL) – Polyana Fontes Dec 21 '12 at 12:08
  • "you can easily emulate the behavior of overloaded functions by simply accepting different subsets of arguments in your function/method implementation." But please be responsible. Don't try to jam wildly different function signatures into a single function. – Karl Knechtel Dec 21 '12 at 14:19
2

So if I'm initializing some variables and calling some functions in a class class1's __init__, then I derive a subclass class2(class1) of it, I'd have to reinitialize all of class1's variables and call those functions in class2's __init__?

No. You just have to call the superclass's __init__(). Here, and here, you can find how to do it.

Community
  • 1
  • 1
0xc0de
  • 8,028
  • 5
  • 49
  • 75