[Tutor] exec "self.abc=22" ?
spir
denis.spir at free.fr
Mon Feb 16 21:20:11 CET 2009
Le Mon, 16 Feb 2009 12:01:24 -0800,
Marc Tompkins <marc.tompkins at gmail.com> a écrit :
> On Mon, Feb 16, 2009 at 11:58 AM, Wayne Watson <sierra_mtnview at sbcglobal.net
> > wrote:
>
> > Python doesn't like the code in the Subject (unqualified exec not allowed
> > in function). but easily likes self.abc="22". However, I'd like to assemble
> > the assignment as a string, as shown in Subject, and execute it. Is there a
> > way to do this?
> > --
> >
> > varName = "abc"
> varValue = 22
> setattr(self, varName, varValue)
>>> exec("name='22'")
>>> name
'22'
>>> exec("name=\"22\"")
>>> name
'22'
------
la vida e estranya
More information about the Tutor
mailing list