Mix-In Class Methods At Run-Time

Bruno Desthuilliers onurb at xiludom.gro
Mon Jun 26 10:51:50 CEST 2006


digitalorganics at gmail.com wrote:
(snip)

> Two additional questions though: 1) is there a way for a function to
> get a reference to its caller automatically (as in, without the caller
> having to pass it in)?

It's possible with sys._getframe() and a decorator - but consider it a
hack.

> and 2) what's the reason to use newstyle classes
> versus the old?

All this is explained on python.org (there's a menu entry for this in
the documentation menu). AFAICT, newstyle classes can do whatever
oldstyle classes did, *and much more* (descriptors and usable
metaclasses) - and they are somewhat faster too. So - compatibility with
older Python versions (< 2.2 IIRC) set aside -, there's just no reason
to use oldstyle classes.

>  In order to create the dynamic class "NewClass" in the
> code above I called type() but that requires at least one new style
> class as a base. Thus, I had to have at least one of my animals inherit
> from "object" and this seemed a nuisance since

OMG, eight more keystrokes - talk about a nuisance...

> I don't at this point
> know what the benefit of "newstyle" classes is.

See it the other way round :  the *only* benefit of oldstyle classes is
compatibility with pre-2.2 Python versions.

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list