[Twisted-Python] What's the purpose of __?

Attila Nagy bra at fsn.hu
Wed Feb 16 10:17:51 EST 2011


  On 02/15/2011 11:03 PM, Phil Christensen wrote:
> On Feb 15, 2011, at 5:36 AM, Attila Nagy wrote:
>> Hi,
>>
>> I'm trying to do an LMTP implementation based on smtp.py and came to the
>> issue of class private variables with double underscores.
>> Examples:
>> http://twistedmatrix.com/trac/browser/trunk/twisted/mail/smtp.py#L746
>> http://twistedmatrix.com/trac/browser/trunk/twisted/mail/smtp.py#L815
>> and a lot of occurrences in this file.
>>
>> This makes me a problem, because I override some functions in a class,
>> named LMTP, so these cannot work together.
>>
>> What's the rationale of using these, instead of static names?
> I've got no idea of the rationale for using double-underscore notation, but why does that prevent you from subclassing it? Just add the class name to it for access:
>
> class A:
>      def __init__(self):
>          self.__test = 'test'
>
> a = A()
>
> print a._A__test
>
>
I can make it work, but I have to hardcode variable names like the above 
and I don't understand why is this needed. It's so ugly.

BTW, this is the situation:

class SMTP:
     def something(self):
         self.__inheader = True

class LMTP(SMTP)	
     def other(self):
         if self.__inheader == True:

and I get other from SMTP, and make some little tweaks. This way, I have to rename all variables in LMTP's functions from __stuff to _SMTP_stuff.





More information about the Twisted-Python mailing list