Unified Type/class

Alex Martelli aleax at aleax.it
Fri Jan 25 12:15:26 EST 2002


"Vincent Foley" <vince at news.videotron.ca> wrote in message
news:Olg48.7695$7U5.551678 at weber.videotron.net...
> I'm no expert on the Python language, or OO programming, but it's
> possible in the language Ruby <www.ruby-lang.org> to add new methods to
> existing classes.  For example, it is possible to had a rot13 method to
> the String class.  Is this one of the things this Python type and class
> unification renders possible?

Python allows a class to forbid addition of attributes to its instances.

The classes in turn are instances of a metaclass.  Python allows a
metaclass to forbid addition of attributes to its instances (classes).

The metaclass of type str uses this ability and thus stops you from
adding methods to the str class object.  (The same metaclass, when
used for your own class statements, does allow such additions).

You may code your own metaclasses as you wish, but (in the general
case) may not be able to switch a class's metaclass on the fly.


Alex






More information about the Python-list mailing list