Question on statically nested scopes

Bayzuldin Timur bayt at max.net.ua
Fri Aug 30 17:15:11 CEST 2002


> Manus Hand wrote:
> > In my code, I use a very handy trick:
> >
> > class Whatever:
> >     def someFunction(self, **params):
> >         vars().update(locals())
> >
> > This means that whatever named variables I pass into "someFunction"
> > become attributes of the object (accessible with self.varName, etc.).
>
> What do you think PEP227 (statically nested scopes) has to do
> with this whole issue?
>
> And you probably meant 'vars(self).update(params)'.
>
> But anyway, you could do
>
>     def someFunction(self, **params):
>         for key,value in params:
>             setattr(self, key, value)
> and
>
>     def someFunction(self, **params):
>         self.__dict__.update(locals())
>
> and don't have to worry much :-)
>
> regards,
>
>     holger

Python 2.2 (#1, Aug 26 2002, 04:40:34)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>Class Foo:
...	def Some(self, **params):
...		vars(self).update(locals().values()[1])
...
>>>s = Foo()
>>>s.Some(a=1, b=2, c=3, d='Hello')
>>>s.a
1
>>>s.d
'Hello'
>>>




More information about the Python-list mailing list