June 8, 2020

Python classes and object oriented programming

I learned object oriented programming using Ruby, and have used it with Java and Javascript as well. Python is much the same, yet with its own way of doing things.

Creating an object

The __init__ method gets called when a new object is "instantiated". As with all other object methods in Python, a first argument, named "self" by tradition is inserted before other arguments.

To create a new object, treat the class name as a function, i.e. if we have "class Rodent" we make a new object via:

x = Rodent ()

Object variables

These are accessed via self.variable -- no magic here.

Class variables

The big wart here is that any object variables with the same name as a class variable will hide the class variable -- so keep the names distinct.

From the outside world, we access class variables and object variables the same way, i.e. by x.variable, and this is where the above wart shows up.

Within methods, self.variable accesses object variables and Rodent.variable will access class variables. Class variables may also be initialized without any prefix at all outside of any methods, and within a class, as follows:

class Rodent :
    count = 0
    def __init__ ( self, name ) :
	Rodent.count += 1
	self.name = name
In the above, the class variable "count" keeps track of how many rodents have been instantiated. The instance variable "self.name" is the name of the particular rodent. Outside the class we can use x.name and/or x.count to access these given that x is an instantiated rodent object.

Encapsulation and private attributes

Like Perl, Python drops the ball here. There is a convention that attributes prefixed with an underscore are private, and good citizens should not access them from outside the class, but this is merely a convention.

Overriding methods of builtin classes

Python comes up short here also. Would you like to add a method to the list class? Sorry, not without building your own custom version of Python. Note that you can freely do this in Ruby. You need to create a class of your own (let's say "mylist") that inherits from "list" and adds the desired methods. This will solve the problem for some cases, but not all.

Magic methods

By defining certain magic methods, you can do things such as make the "+" infix operator work with elements of your class.
Feedback? Questions? Drop me a line!

Tom's Computer Info / tom@mmto.org