Equivalent to chr(eval(self.myhex)) without using eval?

Jeff Kowalczyk jtk at yahoo.com
Wed Dec 18 15:06:20 EST 2002


I have a class instance attribute which is a string representation of a hex value. I need
to get the chr() character back.

Is there a way using builtins to make this conversion *without* an eval() call?

>>> class Foo:
...  def __init__(self):
...   self.myhex = '0x2a'
...  def bar(self):
...   print chr(eval(self.myhex))
...
>>> foo = Foo()
>>> foo.bar()
*

I want to try something other than eval, because the above code is giving me trouble in
Zope, which could restrict the eval() method for obvious security reasons. Not sure if
that's the problem yet, but the above code gives an EOF parse error in Zope.

Thanks.chr(eval(self.myhex))











More information about the Python-list mailing list