Re: getattr/setattr q.

2007-04-03 Thread Paulo da Silva
> Yes, but you shouldn't unless you really need to. You're better off > rethinking your algorithm. I need it but inside a class. The idea is to pass an instance of a class (think of something like a record but with some methods inside) with "fields", whose names are not known in advance, to ano

Re: getattr/setattr q.

2007-04-03 Thread Steve Holden
Steven Bethard wrote: > Steve Holden wrote: >> You don't need setattr/getattr if you know in advance the name of the >> attribute you need to access and you can get a reference to the object >> whose attribute it is. So: >> >> >>> x = "Hello, Paulo" >> >>> import sys >> >>> sys.modules['__main

Re: getattr/setattr q.

2007-04-03 Thread Steven Bethard
Steve Holden wrote: > You don't need setattr/getattr if you know in advance the name of the > attribute you need to access and you can get a reference to the object > whose attribute it is. So: > > >>> x = "Hello, Paulo" > >>> import sys > >>> sys.modules['__main__'].x > 'Hello, Paulo' a.k.a

Re: getattr/setattr q.

2007-04-03 Thread Steve Holden
Paulo da Silva wrote: > Steven Bethard escreveu: >> Paulo da Silva wrote: > ... > >> 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 yo

Re: getattr/setattr q.

2007-04-03 Thread Paulo da Silva
Steven Bethard escreveu: > Paulo da Silva wrote: ... > 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?

Re: getattr/setattr q.

2007-04-03 Thread Paulo da Silva
7stud escreveu: > On Apr 2, 10:08 pm, Paulo da Silva <[EMAIL PROTECTED]> wrote: >> Is it possible to use getattr/setattr for variables not inside >> classes...? > > What does the python documentation say about the definition of > setattr()? > I didn't read the full python documentation, yet! I ho

Re: getattr/setattr q.

2007-04-02 Thread Steven D'Aprano
On Tue, 03 Apr 2007 05:08:42 +0100, Paulo da Silva wrote: > Hi! > > 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"). Yes, but you shouldn't unless you re

Re: getattr/setattr q.

2007-04-02 Thread 7stud
On Apr 2, 10:08 pm, Paulo da Silva <[EMAIL PROTECTED]> wrote: > Is it possible to use getattr/setattr for variables not inside > classes...? What does the python documentation say about the definition of setattr()? -- http://mail.python.org/mailman/listinfo/python-list

Re: getattr/setattr q.

2007-04-02 Thread Ben Finney
Paulo da Silva <[EMAIL PROTECTED]> writes: > In a class C, I may do setattr(C,'x',10). That would set an attribute on the class C, shared by all instances of that class. If you want to set an attribute on an instance, you need to do so on the instance object:: >>> class Foo(object): ...

Re: getattr/setattr q.

2007-04-02 Thread Steven Bethard
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 i

getattr/setattr q.

2007-04-02 Thread Paulo da Silva
Hi! 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"). Thanks. -- http://mail.python.org/mailman/listinfo/python-list