__import__ with dict values

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Mar 13 20:32:40 CET 2009

En Fri, 13 Mar 2009 17:12:49 -0200, alex goretoy  
<aleksandr.goretoy at gmail.com> escribió:

> wow, ok, thank you Gabriel, I wasn't aware of x,'y',z
> This is what I decided to go with for now in one of my classes, but  
> another
> class will need a modified version of this, as mentioned x,'y',z
>         B=_brush()
>         list( ( self.__setattr__(x.replace("b_",""),getattr(B,x))  for x  
> in
> dir(B) if x.startswith("b_") ) )

__special__ methods are an implementation detail that you should not use  
explicitely; instead of obj.__setattr__(name, value) use setattr(obj,  
name, value).

And building a list of None just to discard it isn't good style (that is:  
don't use a generator
expression / list comprehension just by its side effect if you don't want  
the resulting list object)

So I'd write the above as:

for name in dir(B):
   if name.startswith("b_"):
     setattr(self, name[2:], getattr(B, name))

Gabriel Genellina

More information about the Python-list mailing list