New inited instance of class?

Michele Simionato michele.simionato at poste.it
Mon Dec 8 17:58:12 CET 2003


"Francis Avila" <francisgavila at yahoo.com> wrote in message news:<vt6qfldliss882 at corp.supernews.com>...
> That strikes me as a very *bad* idea, because it's no longer obvious that
> the classes themselves are new-style, and the OP might develop a bad habit
> of using this to avoid typing '(object)' all the time.  (Or he might show us
> all for fools, if ':' becomes shorthand for '(object):'.)

I don't disagree.

> Aside from that, metaclasses are a more advanced and less-commonly-used
> feature of classes, and therefore probably more vulnerable to change.  I
> know it should work, but am uneasy that it might manifest some unexpected
> behavior (or bug).

Maybe you are worrying a bit too much here, since the __metaclass__=type
idiom is quite safe in practice. Of course, it can give surprises,
such as the following:

__metaclass__=type

class E: pass

raise E() # not the error you would expect

Nevertheless, you should derive exceptions from Exception, so this
does not happen in real live. The other caution is with certain usages
of special methods, but they are relatively rare.

On the other hand, custom metaclasses different from type can
give more than one surprise, so I would not suggest their use to
the causal programmer. BTW, also the experienced programmer can
live without metaclasses for 99% of tasks. But they may come
handy in special situations.

     Michele




More information about the Python-list mailing list