Appendix C. Python Versus C++

This appendix briefly summarizes some of the differences between Python and C++ classes. Python’s class system can be thought of as a subset of C++’s. Although the comparison to Modula 3 may be closer, C++ is the dominant OOP language today. But in Python, things are intentionally simpler -- classes are simply objects with attached attributes that may have links to other class objects. They support generation of multiple instances, customization by attribute inheritance, and operator overloading, but the object model in Python is comparatively uncluttered. Here are some specific differences between Python and C++:

Attributes

There is no real distinction between data members and methods in Python; both simply designate named attributes of instances or classes, bound to functions or other kinds of objects. Attributes are names attached to objects, and accessed by qualification: object.attribute. Methods are merely class attributes assigned to functions normally created with nested def statements; members are just attribute names assigned to other kinds of objects.

Class object generation

Class statements create class objects and assign them to a name. Statements that assign names within a class statement generate class attributes, and classes inherit attributes from all other classes listed in their class statement header line (multiple inheritance is supported; this is discussed in a moment).

Instance object creation

Calling a class object as ...

Get Programming Python, Second Edition 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.