evalstr

Michael Chermside mcherm at destiny.com
Wed Jul 11 15:17:53 EDT 2001


>>> class Eval:
def __getitem__(self, key):
		return eval(key)

	
>>> foo = 3.0
>>> spam = 5.0
>>> print "%(foo/spam)s" % Eval()
0.6
>>> print "%(foo/spam).55f" % Eval()
0.5999999999999999800000000000000000000000000000000000000
>>>

Now THAT'S a wierd-but-cool hack! Very powerful and flexible, and
simple enough to use. It should definitely be added to the cookbook. 
It does, unfortunately, have one significant limitation: the expression
cannot contain the character ")", which is one you'd like to use in
expressions.

-- Michael Chermside






More information about the Python-list mailing list