[PySide] complicated dictionaries

Christian Tismer tismer at stackless.com
Fri Apr 26 16:51:58 CEST 2013


On 25.04.13 01:58, John Ehresman wrote:
> On 4/24/13 7:40 PM, Aaron Richiger wrote:
>> For people still interested in this tutorial: It goes towards meta
>> programming. It allows you defining many different classes with very
>> little code (but only as long as the classes exactly fit into the
>> limitations of this template).
> Yes, I can kind of see how it generates classes.  It's probably better
> to do something like this via metaclasses.  I just get scared when I see
> code that uses locals() to set things in the local namespace ;).
>

Just saw this advanced tutorial example.
I agree, this is all about Python "tricks" to save a lot of typing,
with the drawback that such implementations are rarely general-purpose
and hard to understand.

Using such descriptive tuples for attributes to be generated becomes
quite common if you are dealing with legacy file formats ore database
structures which all follow a certain layout pattern.

The code can pretty easily be converted into using metaclasses. This
way it can get a bit more understandable. In any case, the necessity
to fiddle with __dict__, getters and setters and so on remains.

Such code should get a lot of good comments every few lines, what they
are for and how they work, with references to other documentation and so on.
Otherwise the code is only understandable by people who had written 
something
similar, before. (Yes, I confess :-) )

And although by the "metaclass" idiom this technique pretends to be
advanced, it still needs much knowledge about Python to do them
right, to let inheritance work consistently, so I highly recommend to avoid
them unless you have a real use case (like many similar classes where
the error-prone code repetition is more painful that remembering the
short but twisted meta hack).

If this example stays, it should be re-written, better documented, and avoid
making the reader feel dumb. Instead he should be warned and pointed at
further reading, for instance
http://jakevdp.github.io/blog/2012/12/01/a-primer-on-python-metaclasses/
contains a slow and step-wise introduction. (Python 2.x)

Btw., the use of locals() is the only way to get access to the dictionary
of a class during its construction; it can even be helpful for introspection
in metaclasses. But you may never use it for assignment.
See http://docs.python.org/2/library/functions.html#locals

-- 
Christian Tismer             :^)   <mailto:tismer at stackless.com>
Software Consulting          :     Have a break! Take a ride on Python's
Karl-Liebknecht-Str. 121     :    *Starship* http://starship.python.net/
14482 Potsdam                :     PGP key -> http://pgp.uni-mainz.de
phone +49 173 24 18 776  fax +49 (30) 700143-0023
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
       whom do you want to sponsor today?   http://www.stackless.com/




More information about the PySide mailing list