__import__ with dict values

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Mar 14 00:56:59 EDT 2009


En Sat, 14 Mar 2009 00:31:55 -0200, Lie Ryan <lie.1296 at gmail.com> escribió:
> Gabriel Genellina wrote:
>> En Fri, 13 Mar 2009 17:12:49 -0200, alex goretoy  
>> <aleksandr.goretoy at gmail.com> escribió:
>>>         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).
>
> Except if you want to override the special methods like in operator  
> overloading.

Perhaps I should have written "you should not *call* __special__ methods  
explicitely".
Even if one overrides, e.g., __setitem__, the right way to use it (or  
call, or invoke, or operate) is still a[x] = y
I've seen plenty of code spelling it a.__setitem__(x, y) instead. It is  
usually slower, exposes an implementation detail, is far less clear, it's  
not exactly the same thing, prevents the interpreter from doing many  
optimizations...
I don't get *why* someone would like to write that. Does it look "cool"?  
Is it some kind of "Look, ma, I know those hidden names!" syndrome? Is it  
contagious?

-- 
Gabriel Genellina




More information about the Python-list mailing list