getattr/setattr q.

Steven Bethard steven.bethard at gmail.com
Tue Apr 3 01:00:14 EDT 2007


Paulo da Silva wrote:
> In a class C, I may do setattr(C,'x',10).
> 
> Is it possible to use getattr/setattr for variables not inside
> classes or something equivalent? I mean with the same result as
> exec("x=10").

If you're at the module level, you can do::

     globals()['x'] = 10

If you're inside a function, you probably want to look for another way 
of doing what you're doing.

What's the actual task you're trying to accomplish here?

STeVe



More information about the Python-list mailing list