Python meta object question

Gordon McMillan gmcm at hypernet.com
Wed Dec 1 15:35:52 EST 1999


Mickael Remond wrote:

> In a Bytes Magazine article (1997) I read that :
> 
>      For a programming language, Python is flexible. Classes and
>      method refer ences in Python are treated as first-class
>      objects. That is, new methods and member variables can be
>      added to a class at any time, and all existing and future
>      instances of classes are affected by these changes. 

[snip]

> I did not find any clue on how to leverage these features. Are
> they out of date ?

Byte is not referring to anything fancy, like metaclasses, or 
changing the base class(es). They are simply alluding to the 
fact that changing an attribute in a class object, will (unless 
hidden by an instance attribute of the same name) be seen by 
all instances (or derived-class instances).

class X:
  pass

class Y(X):
  pass

y = Y()
X.a = 42
assert y.a == 42

Trying to swap out X or Y in y's structure is a far more difficult 
and dangerous task.

- Gordon




More information about the Python-list mailing list