Understanding properties

Alex Martelli aleax at aleax.it
Sat Jul 6 16:56:58 EDT 2002


merman wrote:
        ...
>> default a class is a "classic" one, implementing exactly the
>> same semantics as in Python 2.1 and older, ensuring no major code
>> breakage in porting from 2.1 to 2.2 but constraining the applicability
>> of the new features to classic classes.
> 
> Thanks Alex.
> 
> The last part of your description is a little bit unclear for me - I'm a
> newbie.
> Perhaps I will understand it in one year ;-).

And perhaps in a year classic classes will have little more than
historical interest.  So, I only left the last part, and I now try
to clarify it.  If it's still "a little bit unclear" for you,
maybe you can at least tell me WHICH parts are unclear, so I
enumerate things in reasonably small bytes in the following.

In Python 2.2, there are two kinds of classes: classic and new-style.

In Python 2.1, there was only one kind, equivalent to today's classic.

The reason for continued existence of classic classes in Python
2.2 is backwards compatibility.

Backwards compatibility means your existing code doesn't break just
because you upgrade your Python to a new release.

Python cares about backwards compatibility.  There's a lot of Python
code out there, not a little of it in applications that are crucial
to the firms deploying them.

For backwards compatibility, a class for which you don't say
whether it's classic or new-style must be classic in Python 2.2.

For backwards compatibility, a classic class in Python 2.2 must
behave much as it did in 2.1.

A program construct's "behavior" is also more specifically and
precisely called its "semantics".

The semantics of classic classes can't fully support properties
without breaking backwards compatibility.

Therefore, to use properties fully, in Python 2.2., you must
ensure your classes are new-style.

There are several ways to require that a class be new-style.

If in the class body is the binding (assignment) statement:
    __metaclass__ = type
the class is new-style.

Otherwise, if the class inherit from a new-style class, it's
also new-style.

object, and all inheritable built-ins such as list, dict, etc,
are new-style.

A class without bases and without a __metaclass_ binding in
the class body is new-style if, and only if, a __metaclass__
global variable is defined in the module when the class
statement is executed, and its value is type.

Inheriting from object is the simple and most common way
to make a class new-style, though, as we've just seen, it's
far from being the only one.


OK -- that's a lot of stuff, so I'd better stop now.  It is,
I hope, at least clearer?  Not quite sure why a beginner would
worry about the last little detail of WHY a certain language
construct is necessary (most beginners I've known just accept
it as an article of faith, for most constructs), but clearly
you do, since you've already posted more than once about this
subject, and I hope the above does help.


Alex




More information about the Python-list mailing list