Do pythons like sugar?

Andrew Dalke adalke at mindspring.com
Thu Jan 9 19:21:54 EST 2003


> Although I too am a bit curious what Andrew's trick is, I'm guessing it
> involves manipulating namespaces in an "interesting" way.  Not the sort
> of thing to be encouraged.  He knows that if he told you, you'd probably
> use it and then regret it later on when you realized its implications,
> or that using it would prevent you seeing a better solution.

I posted it elsewhere.
  =====================
In other words "DO NOT USE IN REAL CODE - I WILL NOT BE
HELD RESPONSIBLE IF YOU DEPLOY WITH THIS.  FOR NOVELTY
USE ONLY."

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

					Andrew





More information about the Python-list mailing list