Menu Close


Python: classes

Object Oriented

Python is an imperative, procedural, functional and object oriented language. As you can see is in fact a hybrid language. That means we can define classes and we can create instances of the classes that are called objects, like in Java. This may be true but the syntax is a lot different.



The __init__  function is the constructor. This has 2 parameters: self and name. When a new object is called from class Dog, only one parameters is required.


Many developers have difficulty to understand difference between a class attributes and object attributes. Any variable defined in local scope of the class are shared between all objects of the class.  Object attributes, must use self. prefix. Both kind of attributes can be modified in the constructor, but class attributes are shared between objects. That is the difference.


A class can inherit from another class. Observe in the constructor of “Dog” we call the super() this is the super class and __init__ that is the constructor for the parent class. super() is Python 3 syntax. For Python 2 this we could be a call like this: super(Dog, self).__init(“Dog”)

See also: Python documentation