Order in metaclass

Carlos Ribeiro carribeiro at gmail.com
Wed Oct 13 17:16:35 EDT 2004


On Wed, 13 Oct 2004 22:03:47 +0200, Peter Otten <__peter__ at web.de> wrote:
> > 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.
> 
> No, it doesn't:

:-P  Oh well, I just missed that Nicolas was using co_names, instead
of line numbering magic as I did in my first tests. :-) Seems he was a
little bit more clever than I was :-)
 
> >>> class Bar:
> ...     a, b, c, d = "xyzt"
> ...     print sys._getframe().f_code.co_names
> ...
> ('__name__', '__module__', 'a', 'b', 'c', 'd', 'sys', '_getframe', 'f_code',
> 'co_names')
> 
> whereas
> 
> >>> vars(Bar).keys()
> ['a', '__module__', 'b', 'd', 'c', '__doc__']
> 
> Of course there are other ways to garble the co_names order:
> 
> >>> class Any:
> ...     def __getattr__(self, name): return name
> ...
> >>> any = Any()
> >>> class Bar:
> ...     x = any.d
> ...     a, b, c, d = "xyzt"
> ...     print sys._getframe().f_code.co_names
> ...
> ('__name__', '__module__', 'any', 'd', 'x', 'a', 'b', 'c', 'sys',
> '_getframe', 'f_code', 'co_names')
> 
> All very useless/interesting stuff :-)

Useless, in a sense, yes. But we learned a lot (or at least, I did).
So it was not totally useless...

-- 
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