5.6. Object Orientation

We have already encountered some of Python's object-orientation features in the discussion of the list and dictionary built-in classes.

In this section, we talk about declaring classes and creating objects. We also take a look at how inheritance works in Python.

Classes are created with the class keyword. The code below creates a trivial class foo that has a class variable x.

CD-ROM reference=5092.txt
>>> class foo:
... x = 1
>>> print foo.x
1

Objects can be created from the class foo in a syntax reminiscent of a function call:

CD-ROM reference=5093.txt
>>> class foo:
... x = 1
...
>>> f = foo()
>>> print f.x
1

Classes can contain methods. Methods are defined in a syntax similar to that for functions. The big difference ...

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.