Accessing global namespace

Jeff Epler jepler at unpythonic.net
Mon Oct 6 22:50:49 EDT 2003


On Tue, Oct 07, 2003 at 02:32:04AM +0000, Dave Benjamin wrote:
> 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?
[snipped]

I'd say you're better off writing something like this:

    def attr_reader(name):
        def get(self):
            return getattr(self, "_" + name)
        return property(get)

    class Benjamin(object):
        _a = 42
        a = attr_reader('a')

    t = Benjamin()
    print t.a

Jeff





More information about the Python-list mailing list