[Tutor] Properties of an object

Alan Gauld alan.gauld at btinternet.com
Sat Jan 31 00:48:03 CET 2009


"Eike Welk" <eike.welk at gmx.net> wrote

>> In a rather consistent approach, Python does not provide any
>> standard way to simply define/describe/structure an object -- or a
>> class.
> The added flexibility of Python gives great powers to the 
> programmers
> of libraries.

It goves a lot of flexibility, whether that really is power I'm not 
sure.
Power is only good if used well and I've yet to see a convincing
case for that kind of power applied to objects in either Python
or Lisp (which also has that kind of ability).

Its a bit like C++ templates. Strousdtrup spends a couple of
chapters in his book on the evolution of C++ discussing clever
tricks you can do with templates, including writing a class that
inherits from itself! But that doesn't mean templates should be
used that way!

> There is for example a library for Java/C++ style data attributes in
> Python; Traits:
> http://code.enthought.com/projects/traits/docs/html/traits_user_manual/intro.html

I'll check that one out, I haven't noticed it before.

> Also pretty cool is the possibility to change what happens when you
> type the 'class' statement. You only have to define the global
> variable '__metaclass__', which must point to an alternative
> implementation of the class definition algorithm.


As I said beforer, be very careful with metacvlasses, they are
powerful and have valid uses but they make your head hurt!
(I have actually used metaclasses in real world code and they
are useful, but complex and therefore dangerous)

> You can even change the type of an object at runtime:

Again I've never really found a valid need for this (especially in 
Python)
but in some respects its like C++'s dynamic casts. Its most useful if
changing between objects within the same heirarchy. For example
a banking system with multiple types of Bank account. You might
want to convert a  savings account to a checking account. This
could be done using class type changes (But its usually done
through more conventional measures!)

I rather suspect the OP has run from the room screaming  by now! :-)
For anyone else still reading this and getting jumpy, don't panic
it's all extremely esoteric capability that in practice is hardly ever
used by normal programmers.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list