Do pythons like sugar?

andy andy at eastonwest.co.uk
Fri Jan 10 00:07:32 CET 2003


On Thursday 09 Jan 2003 6:15 pm, Andrew Dalke wrote:
> In other words "DO NOT USE IN REAL CODE - I WILL NOT BE
> HELD RESPONSIBLE IF YOU DEPLOY WITH THIS.  FOR NOVELTY
> USE ONLY."

Oooh.  

>
>  >>> class Foo:
>
> ...     eggs = 1
> ...     def doit(self):
> ...             self.spam = 5
> ...             self.tomato = 3
> ...             for x in dir(self): exec "%s = self.%s" % (x,x)
> ...             for k, v in locals().items():
> ...                     print k, "==", v
> ...             print
> ...             print "the calculation yields", tomato ** spam
> ...

Ahhh.

>  >>> Foo().doit()
>
> doit == <bound method Foo.doit of <__main__.Foo instance at 0x815a284>>
> tomato == 3
> __module__ == __main__
> spam == 5
> x == tomato
> self == <__main__.Foo instance at 0x815a284>
> eggs == 1
> __doc__ == None
>
> the calculation yields 243

I can see why one shouldn't use this... kind of 'implementation' and 'version' 
specific! 

Maybe a candidate for the next PEP... not?

Anyway, I sort of like the explicit self reference - makes things clearer in 
the long run.

-andyj





More information about the Python-list mailing list