6

I am new to Python. I came across Python code in an OpenFlow controller that I am working on.

class SimpleSwitch(app_manager.RyuApp):
    OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]

    def __init__(self, *args, **kwargs):
        super(SimpleSwitch, self).__init__(*args, **kwargs)
        self.mac_to_port = {}

My questions are as follows.

  1. Is __init__ the constructor for a class?

  2. Is self the same as C++'s this pointer?

  3. Does super(SimpleSwitch, self).__init__(*args, **kwargs) mean calling constructor for parent/super class?

  4. Can you add a new member to self as mac_to_port? Or has that been already added and just being initialized here?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • 4
    You would be well advised to read [the Python tutorial](http://docs.python.org/2/tutorial/) to get an introduction to the basics of classes in Python. – BrenBarn Apr 12 '14 at 08:04

2 Answers2

8
  1. __init__ is the initialiser; __new__ is the constructor. See e.g. this question.
  2. Effectively yes: the first argument to instance methods in Python, called self by convention, is the instance itself.
  3. Calling the parent class's initialiser, yes.
  4. It's adding a new attribute to SimpleSwitch in addition to what the parent class already has, an empty dictionary.
Community
  • 1
  • 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • what if SimpleSwitch does not have a super class? – liv2hak Apr 12 '14 at 08:18
  • 2
    Also, the name `self` is a convention in python, whereas `this` is a keyword in C++. – juanchopanza Apr 12 '14 at 08:18
  • 2
    `__init__` is much more analogous to a C++ constructor than `__new__`. The constructor doesn't *create* a new object - rather, it is implicitly run on the newly-created object after it is created and before it is returned to the calling code, very much like `__init__`. `__new__` creates and returns a new object (or might retrieve and returns an interned one), making it more like C++'s [`operator new`](http://www.cplusplus.com/reference/new/operator%20new/). – lvc Apr 12 '14 at 08:22
  • @jonrsharpe - Oh the parent class is the one specified as a parameter in class definition? – liv2hak Apr 12 '14 at 08:26
  • @liv2hak in Python 3, every Python class subclasses the builtin class `object` if it doesn't subclass anything else. In Python 2, it's recommended to always subclass object (making it a "new style class"), because the case where it doesn't (old style classes) was only kept for backward compatibility - and one of the things that is different is that `super` doesn't work on old-style classes. – lvc Apr 12 '14 at 08:26
0
  1. Super in Python is not like C++'s super. I have not used C++, but I can tell you that super in python does not act the same. Instead of calling the parent, python super calls the children of the class in which super is called, then moves in an interesting chain. Think of three tiered class system where there is a single base class, two subclasses to that base class, and two subclasses to those subclasses. Calling super on the bottom tier would call the parent immediately above it, but calling super in one of the second-tier classes would call their children first, then it looks to the side and calls the other classes on it's own tier, then the children of that same-tier class are called. Once all of the same-tier classes and all of their children re called, then super calls the parent of the middle-tier classes.

It's hard to explain in words. Watch Raymond Hettinger's "super considered super" talk from PyCon. He gives a very good explanation of how it works, and why python's super should not be called 'super'.

JNYRanger
  • 6,829
  • 12
  • 53
  • 81