int
which translates to saying that variables that store integers are variables which are instances (objects) of the int
class.Note for Static Language ProgrammersNote that even integers are treated as objects (of theint
class). This is unlike C++ and Java (before version 1.5) where integers are primitive native types.Seehelp(int)
for more details on the class.C# and Java 1.5 programmers will find this similar to the boxing and unboxing concept.
class
keyword. The fields and methods of the class are listed in an indented block.self
self
.self
- any other name is definitely frowned upon. There are many advantages to using a standard name - any reader of your program will immediately recognize it and even specialized IDEs (Integrated Development Environments) can help you if you use self
.Note for C++/Java/C# ProgrammersTheself
in Python is equivalent to thethis
pointer in C++ and thethis
reference in Java and C#.
self
and why you don't need to give a value for it. An example will make this clear. Say you have a class called MyClass
and an instance of this class called myobject
. When you call a method of this object as myobject.method(arg1, arg2)
, this is automatically converted by Python into MyClass.method(myobject, arg1, arg2)
- this is all the special self
is about.self
.oop_simplestclass.py
).class
statement and the name of the class. This is followed by an indented block of statements which form the body of the class. In this case, we have an empty block which is indicated using the pass
statement.Person
class in the __main__
module.self
variable. We will now see an example (save as oop_method.py
).self
in action. Notice that the say_hi
method takes no parameters but still has the self
in the function definition.__init__
method__init__
method now.__init__
method is run as soon as an object of a class is instantiated (i.e. created). The method is useful to do any initialization (i.e. passing initial values to your object) you want to do with your object. Notice the double underscores both at the beginning and at the end of the name.oop_init.py
):__init__
method as taking a parameter name
(along with the usual self
). Here, we just create a new field also called name
. Notice these are two different variables even though they are both called 'name'. There is no problem because the dotted notation self.name
means that there is something called "name" that is part of the object called "self" and the other name
is a local variable. Since we explicitly indicate which name we are referring to, there is no confusion.p
, of the class Person
, we do so by using the class name, followed by the arguments in the parentheses: p = Person('Swaroop').__init__
method. This is the special significance of this method.self.name
field in our methods which is demonstrated in the say_hi
method.oop_objvar.py
):population
belongs to the Robot
class and hence is a class variable. The name
variable belongs to the object (it is assigned using self
) and hence is an object variable.population
class variable as Robot.population
and not as self.population
. We refer to the object variable name
using self.name
notation in the methods of that object. Remember this simple difference between class and object variables. Also note that an object variable with the same name as a class variable will hide the class variable!Robot.population
, we could have also used self.__class__.population
because every object refers to its class via the self.__class__
attribute.how_many
is actually a method that belongs to the class and not to the object. This means we can define it as either a classmethod
or a staticmethod
depending on whether we need to know which class we are part of. Since we refer to a class variable, let's use classmethod
.@classmethod
decorator is the same as calling:__init__
method is used to initialize the Robot
instance with a name. In this method, we increase the population
count by 1 since we have one more robot being added. Also observe that the values of self.name
is specific to each object which indicates the nature of object variables.self
only. This is called an attribute reference.Robot.__doc__
and the method docstring as Robot.say_hi.__doc__
die
method, we simply decrease the Robot.population
count by 1.__privatevar
, Python uses name-mangling to effectively make it a private variable.Note for C++/Java/C# ProgrammersAll class members (including the data members) are public and all the methods are virtual in Python.
SchoolMember
and then have the teacher and student classes inherit from this class, i.e. they will become sub-types of this type (class) and then we can add specific characteristics to these sub-types.SchoolMember
, this is automatically reflected in the subtypes as well. For example, you can add a new ID card field for both teachers and students by simply adding it to the SchoolMember class. However, changes in the subtypes do not affect other subtypes. Another advantage is that you can refer to a teacher or student object as a SchoolMember
object which could be useful in some situations such as counting of the number of school members. This is called polymorphism where a sub-type can be substituted in any situation where a parent type is expected, i.e. the object can be treated as an instance of the parent class.SchoolMember
class in this situation is known as the base class or the superclass. The Teacher
and Student
classes are called the derived classes or subclasses.oop_subclass.py
):class Teacher(SchoolMember)
). Next, we observe that the __init__
method of the base class is explicitly called using the self
variable so that we can initialize the base class part of an instance in the subclass. This is very important to remember- Since we are defining a __init__
method in Teacher
and Student
subclasses, Python does not automatically call the constructor of the base class SchoolMember
, you have to explicitly call it yourself.__init__
method in a subclass, Python will call the constructor of the base class automatically.Teacher
or Student
as we would an instance of SchoolMember
and access the tell
method of SchoolMember
by simply typing Teacher.tell
or Student.tell
, we instead define another tell
method in each subclass (using the tell
method of SchoolMember
for part of it) to tailor it for that subclass. Because we have done this, when we write Teacher.tell
Python uses the tell
method for that subclass vs the superclass. However, if we did not have a tell
method in the subclass, Python would use the tell
method in the superclass. Python always starts looking for methods in the actual subclass type first, and if it doesnt find anything, it starts looking at the methods in the subclasss base classes, one by one in the order they are specified in the tuple (here we only have 1 base class, but you can have multiple base classes) in the class definition.end
parameter is used in the print
function in the superclass's tell()
method to print a line and allow the next print to continue on the same line. This is a trick to make print
not print a \n
(newline) symbol at the end of the printing.