Mix-In Class Methods At Run-Time

digitalorganics at gmail.com digitalorganics at gmail.com
Mon Jun 26 15:16:59 CEST 2006

Bruno Desthuilliers wrote:
> 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.

Gotcha, thanks.

> > 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...

Like, Oh My God! *claps hand to mouth*  lol   You humor me. Yes, eight
more keystrokes. I follow the general rule of, if I'm going to put in
extra effort, I'd like to know why. So you see, it's not so much an
adversion to the eight keystrokes (multiplied by however many classes I
have mind you), but to not knowing why I should use them. If I don't
care for descriptors or metaclasses, I don't see why I should feel
compelled to use them. And when I decide I want/need these features, I
can put the eight keystroke in at that time. No biggy. :-) Thanks

> > 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