building embedded classes as in C/C++

Mitchell Morris mmorris at
Wed Aug 4 03:02:19 CEST 1999

On Tue, 03 Aug 1999 17:12:41 -0700, Andy Beall <beall at> wrote:
>I'd like to build an embedded structure (Python class) much like I do in
>C if possible.  When I do the following, multiple instances of my final
>class overwrite each others sub-class values!
>class Point:
>	x = 0
>	y = 0
>	z = 0
    ^^^^^^ no mention of "self" anywhere
>class Object:
>	pos = Point()
>	ang = Point()
    ^^^^^^ no mention of "self" here either
>obj1 = Object()
>obj2 = Object()
>obj1.ang.x = 123
>print obj2.ang.x    <<-- AND I FIND IT CONTAINS OBJ1's X VALUE


(N.B. C++ code in this posting is untested. Caveat emptor)

Well ... not exactly. Your Python code is more analogous to:
class Point {
	static int x;
	static int y;
	static int z;
	Point() {}    // <=== Note no mention of "this->anything" here
int Point::x = 0;
int Point::y = 0;
int Point::z = 0;

That is, if you don't attach the variables to the object being instantiated,
they're considered "class statics". In C++ you have to explicitly declare
variables as class statics, whereas Python makes them so by default. The
correct Python code is more likely:

class Point:
	def __init__(self):
		self.x = 0
		self.y = 0
		self.z = 0

class Object:
	def __init__(self):
		self.pos = Point()
		self.ang = Point()

obj1 = Object()
obj2 = Object()
obj1.ang.x = 123
print obj2.ang.x

Note the parallel concepts of using the explicit class method argument "self"
in Python and the C++ implicit arg "this".

>I've found that writing something like:
>class Object(Point):
>	...
>works, but then I don't have a wrapper around the (x, y, z) values
>anymore and I 
>don't know how to bring in Point more than once. Is it possible to do
>this in Python.  I've looked in the tutorial and Mark Lutz's book to no
>Andy Beall

Of course it is ... everything is possible in Python, since it is Turing
complete. On a different philosophical note, there are two concepts which
can be expressed best by inheritance (according to the conventional C++
wisdom which I'll parrot to the best of my ability).

The most common notion is "is-a", modeled in C++ by public inheritance.
For example, "class Penguin(Bird)" conceptually represents the notion that a
penguin is a bird and thus could be expected to do anything a bird can do and
respond to any input that other birds would respond to.

The other common notion expressed by inheritance is "is-implemented-by".
For example, "class TCPSocket(File)" conceptually means that a TCPSocket
works exactly like a file, so we'll be stealing his methods directly instead
of writing our own. In C++ this would likely be accomplished through
private inheritance.

Extra credit if you can think of an example that matches protected

Since your example classes "Object" and "Point" actually model neither 
"is-a" nor "is-implemented-by", you may take as a first approximation that
inheritance isn't going to work for you here.

hoping-some-of-this-helps-ly y'rs,

(Dang, now you've got me hyphenating!)

More information about the Python-list mailing list