Anomaly in creating class methods

Alex Martelli aleax at mail.comcast.net
Fri Nov 4 07:46:11 CET 2005


venk <venkatasubramanian at gmail.com> wrote:

> Cool,
>          i got it now... accessing thru attribute reference always
> returns a bound or unbound method... so, D.f is an unbound method

Right.  Specifically, accessing through a (newstyle) class or instance
always calls the __get__ method of a descriptor [[oldstyle classes and
instances are harder to pin down -- avoid them in new code!!!]]; every
function is a descriptor, so...

> whereas i want the "function" of the unbound method... ok,.... this
> example and the nice explanations definitively thought me about
> function, bound method (for which the function is just an attribute
> accessed by im_func) and unbound method...

Great!

>   Also, calling classmethod on an unbound method instead of a function
> is a strict no... no...
>   ok... nice concept...

Glad you like it.

> But, i am at loss when i come to clearly understanding the difference
> between new-style classes and classic classes.... (or should i post it
> as new topic?).....

"Classic" classes (old-style) exist (and are the default) only for
LEGACY purposes; they exhibit a few problematic quirks.  New-style
classes are more regular, fully predictable, easy to explain and to
understand.  In new code, use new-style classes only (make sure every
class includes 'object' among its ancestors -- that's the simplest way).

And, sure, feel free to open another thread if you desire more
information about the several small differences between the new-style
object model and the old-style, legacy one.


Alex



More information about the Python-list mailing list