Tutorial: Objects and Classes in Python and Sage

Author: Florent Hivert <florent.hivert@univ-rouen.fr>

This tutorial is an introduction to object oriented programming in Python and Sage. It requires basic knowledges on imperative/procedural programming (the most common programming style) that is conditional instructions, loops, functions (see Tutorial: Programming in Python and Sage), but now further knowledge about objects and classes is assumed. It is designed as an alternating sequence of formal introduction and exercises. Solutions to the exercises are given at the end.

Foreword: variables, names and objects

As an object oriented, language, Python’s ‘’variables’’ behavior may be surprising for people used to imperative language like C or Maple. The reason is because they are not variables but names.

The following explanation is borrowed from David Goodger.

Other languages have “variables”

In many other languages, assigning to a variable puts a value into a box.
int a = 1;
_images/a1box.png

Box “a” now contains an integer 1.

Assigning another value to the same variable replaces the contents of the box:

a = 2;
_images/a2box.png

Now box “a” contains an integer 2.

Assigning one variable to another makes a copy of the value and puts it in the new box:

int b = a;
_images/b2box.png _images/a2box.png
“b” is a second box, with a copy of integer 2. Box “a” has a separate copy.

Python has “names”

In Python, a “name” or “identifier” is like a parcel tag (or nametag) attached to an object.
a = 1
_images/a1tag.png

Here, an integer 1 object has a tag labelled “a”.

If we reassign to “a”, we just move the tag to another object:

a = 2
_images/a2tag.png _images/1.png

Now the name “a” is attached to an integer 2 object.

The original integer 1 object no longer has a tag “a”. It may live on, but we can’t get to it through the name “a”. (When an object has no more references or tags, it is removed from memory.)

If we assign one name to another, we’re just attaching another nametag to an existing object:

b = a
_images/ab2tag.png

The name “b” is just a second tag bound to the same object as “a”.

Although we commonly refer to “variables” even in Python (because it’s common terminology), we really mean “names” or “identifiers”. In Python, “variables” are nametags for values, not labelled boxes.

As a consequence:

{{{id=0| l = [1,2,3] l1 = l l1[1] = 0 l /// [1, 0, 3] }}}

Object oriented programming paradigm

The object oriented programming paradigm relies on the two following fundamental rules:

  1. Any thing of the real (or mathematical) world which needs to be manipulated by the computer is modeled by an object.
  2. Each object is an instance of some class.

At this point, those two rules are a little meaningless, so let’s give some more or less precise definition of the terms:


object
a portion of memory which contains the information needed to model the real world thing.
class
defines the data structure used to store the objects which are instance of the class together with their behavior.

Let’s start with some examples: We consider the vector space over \QQ whose basis is indexed by permutations, and a particular element in it:

{{{id=1| F = CombinatorialFreeModule(QQ, Permutations()) el = 3*F([1,3,2])+ F([1,2,3]) el /// B[[1, 2, 3]] + 3*B[[1, 3, 2]] }}}

In python, everything is an object so there isn’t any difference between types and classes. On can get the class of the object el by:

{{{id=2| type(el) /// }}}

As such, this is not very informative. We’ll go back to it later. The data associated to objects are stored in so called attributes. They are accessed through the syntax obj.attributes_name:

{{{id=3| el._monomial_coefficients /// {[1, 2, 3]: 1, [1, 3, 2]: 3} }}}

Modifying the attribute modifies the objects:

{{{id=4| el._monomial_coefficients[Permutation([3,2,1])] = 1/2 el /// B[[1, 2, 3]] + 3*B[[1, 3, 2]] + 1/2*B[[3, 2, 1]] }}}

Warning

as a user, you are not supposed to do that by yourself (see note on private attributes below).

As an element of a vector space el has a particular behavior:

{{{id=5| 2*el /// 2*B[[1, 2, 3]] + 6*B[[1, 3, 2]] + B[[3, 2, 1]] }}} {{{id=6| el.support() /// [[1, 2, 3], [1, 3, 2], [3, 2, 1]] }}} {{{id=7| el.coefficient([1, 2, 3]) /// 1 }}}

The behavior is defined through methods (support, coefficient). Note that this is true, even for equality, printing or mathematical operations. For example the call a == b actually is translated to the method call a.__eq__(b). The names of those special methods which are usually called through operators are fixed by the Python language and are of the form __name__. Example include __eq__, __le__ for operators == and <=, __repr__ (see Sage specifics about classes) for printing, __add__ and __mult__ for operators + and * (see http://docs.python.org/library/) for a complete list:

{{{id=8| el.__eq__(F([1,3,2])) /// False }}} {{{id=9| el.__repr__() /// 'B[[1, 2, 3]] + 3*B[[1, 3, 2]] + 1/2*B[[3, 2, 1]]' }}} {{{id=10| el.__mul__(2) /// 2*B[[1, 2, 3]] + 6*B[[1, 3, 2]] + B[[3, 2, 1]] }}}

Some particular actions allows to modify the data structure of el:

{{{id=11| el.rename("bla") el /// bla }}}

Note

The class is stored in a particular attribute called __class__ the normal attribute are stored in a dictionary called __dict__:

{{{id=12| F = CombinatorialFreeModule(QQ, Permutations()) el = 3*F([1,3,2])+ F([1,2,3]) el.rename("foo") el.__class__ /// }}} {{{id=13| el.__dict__ /// {'_monomial_coefficients': {[1, 2, 3]: 1, [1, 3, 2]: 3}, '__custom_name': 'foo'} }}}

Lots of sage objects are not Python objects but compiled Cython objects. Python sees them as builtin objects and you don’t have access to the data structure. Examples include integers and permutation group elements:

{{{id=14| e = Integer(9) type(e) /// }}} {{{id=15| e.__dict__ /// }}} {{{id=16| e.__dict__.keys() /// ['__module__', '_reduction', '__doc__', '_sage_src_lines_'] }}} {{{id=17| id4 = SymmetricGroup(4).one() type(id4) /// }}} {{{id=18| id4.__dict__ /// }}}

Note

Each objects corresponds to a portion of memory called its identity in python. You can get the identity using id:

{{{id=19| el = Integer(9) id(el) # random /// 139813642977744 }}} {{{id=20| el1 = el; id(el1) == id(el) /// True }}} {{{id=21| el1 is el /// True }}}

This is different from mathematical identity:

{{{id=22| el2 = Integer(9) el2 == el1 /// True }}} {{{id=23| el2 is el1 /// False }}} {{{id=24| id(el2) == id(el) /// False }}}

Summary

To define some object, you first have to write a class. The class will defines the methods and the attributes of the object.

method
particular kind of function associated with an object used to get information about the object or to manipulate it.
attribute
variables where the info about the object are stored;

An example: glass of beverage in a restaurant

Let’s write a small class about glasses in a restaurant:

{{{id=25| class Glass(object): def __init__(self, size): assert size > 0 self._size = float(size) self._content = float(0.0) def __repr__(self): if self._content == 0.0: return "An empty glass of size %s"%(self._size) else: return "A glass of size %s cl containing %s cl of water"%( self._size, self._content) def fill(self): self._content = self._size def empty(self): self._content = float(0.0) /// }}}

Let’s create a small glass:

{{{id=26| myGlass = Glass(10); myGlass /// An empty glass of size 10.0 }}} {{{id=27| myGlass.fill(); myGlass /// A glass of size 10.0 cl containing 10.0 cl of water }}} {{{id=28| myGlass.empty(); myGlass /// An empty glass of size 10.0 }}}

Some comments:

  1. The method __init__ is used to initialize the object, it is used by the so called constructor of the class that is executed when calling Glass(10).
  2. The method __repr__ is supposed to return a string which is used to print the object.

Note

Private Attributes

  • most of the time, in order to ensure consistency of the data structures, the user should not be allowed to change directly the attribute of an object. Those attributes are called private. Since there is no mechanism to ensure privacy in python, the convention is the following: private attribute are marked by an underscore.
  • as a consequence attribute access is only made through methods. Methods for reading or writing a private attribute are called accessors.
  • methods which are only for internal use are also prefixed with an underscore.

Exercises

  1. add a method is_empty which returns true if a glass is empty.
  2. define a method drink with a parameter amount which allows to partially drink the water in the glass. Raise an error if one asks to drink more water than there is in the glass or a negative amount of water.
  3. Allows the glass to be filled with wine, beer or other beverage. The method fill should accept a parameter beverage. The beverage is stored in an attribute _beverage. Update the method __repr__ accordingly.
  4. Add an attribute _clean and methods is_clean and wash. At the creation a glass is clean, as soon as it’s filled it becomes dirty, and must be washed to become clean again.
  5. Test everything.
  6. Make sure that everything is tested.
  7. Test everything again.

Inheritance

The problem: object of different classes may share a common behavior.

For example, if one wants to deal now with different dishes (forks, spoons ...) then there is common behavior (becoming dirty and being washed). So the different classes associated to the different kinds of dishes should have the same clean, is_clean and wash methods. But copying and pasting code is very bad for maintenance: mistake are copied, and to change anything one has to remember the location of all the copy ! So there is a need for a mechanism which allows to factorizes the common behavior. It is called inheritance or sub-classing: one write a base class which factorizes the common behavior and reuse the methods from this class.

We first write a small class ‘’AbstractDish’’ which implement the “clean-dirty-wash” behavior:

{{{id=29| class AbstractDish(object): def __init__(self): self._clean = True def is_clean(self): return self._clean def state(self): return "clean" if self.is_clean() else "dirty" def __repr__(self): return "An unspecified %s dish"%self.state() def _make_dirty(self): self._clean = False def wash(self): self._clean = True /// }}}

Now one can reuse this behavior within a class Spoon:

{{{id=30| class Spoon(AbstractDish): def __repr__(self): return "A %s spoon"%self.state() def eat_with(self): self._make_dirty() /// }}}

Let’s tests it:

{{{id=31| s = Spoon(); s /// A clean spoon }}} {{{id=32| s.is_clean() /// True }}} {{{id=33| s.eat_with(); s /// A dirty spoon }}} {{{id=34| s.is_clean() /// False }}} {{{id=35| s.wash(); s /// A clean spoon }}}

Summary

  1. Any class can reuse the behavior of another class. One says that the subclass inherits from the superclass or that it derives from it.

  2. Any instance of the subclass is also an instance its superclass:

    {{{id=36| type(s) /// }}} {{{id=37| isinstance(s, Spoon) /// True }}} {{{id=38| isinstance(s, AbstractDish) /// True }}}
  3. If a subclass redefines a method, then it replaces the former one. One says that the subclass overloads the method. One can nevertheless explicitly call the hidden superclass method.

    {{{id=39| s.__repr__() /// 'A clean spoon' }}} {{{id=40| Spoon.__repr__(s) /// 'A clean spoon' }}} {{{id=41| AbstractDish.__repr__(s) /// 'An unspecified clean dish' }}}

Note

Advanced superclass method call

Sometimes one wants to call an overloaded method without knowing in which class it is defined. On use the super operator:

{{{id=42| super(Spoon, s).__repr__() /// 'An unspecified clean dish' }}}

A very common usage of this construct is to call the __init__ method of the super classes:

{{{id=43| class Spoon(AbstractDish): def __init__(self): print "Building a spoon" super(Spoon, self).__init__() def __repr__(self): return "A %s spoon"%self.state() def eat_with(self): self.make_dirty() s = Spoon() /// Building a spoon }}} {{{id=44| s /// A clean spoon }}}

Exercises

  1. Modify the class Glasses so that it inherits from Dish.
  2. Write a class Plate whose instance can contain any meals together with a class Fork. Avoid at much as possible code duplication (hint: you can write a factorized class ContainerDish).
  3. Test everything.

Sage specifics about classes

Compared to Python, Sage has its particular way to handles objects:

  • Any classes for mathematical objects in Sage should inherits from SageObject rather than from object.
  • Printing should be done through _repr_ instead of __repr__ to allows for renaming.
  • More generally, Sage specific special methods are usually named _meth_ rather than __meth__. For example, lots of classes implement _hash_ which is used and cached by __hash__.

Solutions to the exercises

  1. Here is a solution to the first exercise:

    {{{id=45| class Glass(object): def __init__(self, size): assert size > 0 self._size = float(size) self.wash() def __repr__(self): if self._content == 0.0: return "An empty glass of size %s"%(self._size) else: return "A glass of size %s cl containing %s cl of %s"%( self._size, self._content, self._beverage) def content(self): return self._content def beverage(self): return self._beverage def fill(self, beverage = "water"): if not self.is_clean(): raise ValueError, "Don't want to fill a dirty glass" self._clean = False self._content = self._size self._beverage = beverage def empty(self): self._content = float(0.0) def is_empty(self): return self._content == 0.0 def drink(self, amount): if amount <= 0.0: raise ValueError, "amount must be positive" elif amount > self._content: raise ValueError, "not enough beverage in the glass" else: self._content -= float(amount) def is_clean(self): return self._clean def wash(self): self._content = float(0.0) self._beverage = None self._clean = True /// }}}
  2. Let’s check that everything is working as expected:

    {{{id=46| G = Glass(10.0) G /// An empty glass of size 10.0 }}} {{{id=47| G.is_empty() /// True }}} {{{id=48| G.drink(2) /// Traceback (most recent call last): ValueError: not enough beverage in the glass }}} {{{id=49| G.fill("beer") G /// A glass of size 10.0 cl containing 10.0 cl of beer }}} {{{id=50| G.is_empty() /// False }}} {{{id=51| G.is_clean() /// False }}} {{{id=52| G.drink(5.0) G /// A glass of size 10.0 cl containing 5.0 cl of beer }}} {{{id=53| G.is_empty() /// False }}} {{{id=54| G.is_clean() /// False }}} {{{id=55| G.drink(5) G /// An empty glass of size 10.0 }}} {{{id=56| G.is_clean() /// False }}} {{{id=57| G.fill("orange juice") /// Traceback (most recent call last): ValueError: Don't want to fill a dirty glass }}} {{{id=58| G.wash() G /// An empty glass of size 10.0 }}} {{{id=59| G.fill("orange juice") G /// A glass of size 10.0 cl containing 10.0 cl of orange juice }}}

Here is the solution to the second exercice:

{{{id=60| class ContainerDish(AbstractDish): def __init__(self, size): assert size > 0 self._size = float(size) super(Glass, self).__init__() def content(self): return self._content def empty(self): self._content = float(0.0) def is_empty(self): return self._content == 0.0 /// }}} {{{id=61| class Glass(ContainerDish): def __repr__(self): if self._content == 0.0: return "An empty glass of size %s"%(self._size) else: return "A glass of size %s cl containing %s cl of %s"%( self._size, self._content, self._beverage) def beverage(self): return self._beverage def fill(self, beverage = "water"): if not self.is_clean(): raise ValueError, "Don't want to fill a dirty glass" self.make_dirty() self._content = self._size self._beverage = beverage def drink(self, amount): if amount <= 0.0: raise ValueError, "amount must be positive" elif amount > self._content: raise ValueError, "not enough beverage in the glass" else: self._content -= float(amount) def wash(self): self._content = float(0.0) self._beverage = None super(Glass, self).__wash__(self) /// }}}
  1. Let’s check that everything is working as expected:

Todo

Write demo and tests

That all folks !