Order in metaclass
Carlos Ribeiro
carribeiro at gmail.com
Wed Oct 13 14:07:32 EDT 2004
On Wed, 13 Oct 2004 18:18:02 +0200, Peter Otten <__peter__ at web.de> wrote:
> Carlos Ribeiro wrote:
>
> > executed (which count() can guarantee). There are two situations where
> > the simpler counter works, but the getframe hack doesn't:
> >
> > -- if I have a loop inside my class that is used to declare bunch of
> > attributes, all of them will have the same line number... but a
> > different sequence number if the simpler method is chosen.
> >
> > -- if a function is called that returns a bunch of attributes (not
> > common practice, but still possible). All attributes are at the same
> > line in this case. Example:
> >
> > class Foo:
> > a,b,c = myfunc(...)
>
> Do you have an example handy? I had so far no success breaking Bengt's code.
Ok. Just as an exercise -- at this point we're far from safe Python
land anyway, and it's not recommended to push it beyond this limit...
;-) Pseudo code only:
class Foo:
# loop that creates attributes out of a list
for name, val in list:
locals()[name] = val
or (actual code tested):
>>> class Bar:
... a,b,c,d = (1,2,3,4)
...
>>> vars(Bar)
{'a': 1, '__module__': '__main__', 'b': 2, 'd': 4, 'c': 3, '__doc__': None}
The ordering became (a,b,d,c); and the code would have no way to tell
that 'c' was supposed to be classified before 'd', because the
getframe trick would return the same line.
--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com
More information about the Python-list
mailing list