Accessing global namespace

Dave Benjamin dave at 3dex.com
Mon Oct 6 22:32:04 EDT 2003


Carl Banks wrote:
 > [...snip...]
> I stand by what I said.  Using exec for anything other than explicitly
> asking the user for Python code is wrong, incorrect, and evil, with
> very few exceptions.  Even if it looks harmless.  Anyone who does
> that, or advises that, should be fired.

Ought I to be fired for writing something like this?

#!/usr/bin/env python

def attr_reader(name):
     return '''\
def get_%s(self):
     return self._%s
%s = property(get_%s)
''' % (name, name, name, name)

class Testes:
     _a = 42
     exec attr_reader('a')

t = Testes()
print t.a

Output: 42

Dave





More information about the Python-list mailing list