[Twisted-Python] What's the purpose of __?
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? Thanks,
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 -phil
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.
In case you haven't seen it, here's what the python tutorial says about
double-underscore:
http://docs.python.org/tutorial/classes.html#private-variables
Jason
On Tue, Feb 15, 2011 at 5:36 AM, Attila Nagy
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?
Thanks,
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- Jason Rennie Research Scientist, ITA Software 617-714-2645 http://www.itasoftware.com/
Hi, I know what it does, I just don't get why it was used in smtp.py. But you are right, I should have mentioned that in the subject (adding ... in smtp.py). On 02/15/2011 11:33 PM, Jason Rennie wrote:
In case you haven't seen it, here's what the python tutorial says about double-underscore:
http://docs.python.org/tutorial/classes.html#private-variables
Jason
On Tue, Feb 15, 2011 at 5:36 AM, Attila Nagy
mailto:bra@fsn.hu> 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?
Thanks,
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com mailto:Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- Jason Rennie Research Scientist, ITA Software 617-714-2645 http://www.itasoftware.com/
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On 03:19 pm, bra@fsn.hu wrote:
Hi,
I know what it does, I just don't get why it was used in smtp.py. But you are right, I should have mentioned that in the subject (adding ... in smtp.py).
The original author's intent is long since lost at this point. However, a safe guess is that these attributes are not intended to be exposed to users of the code (such as your LMTP subclass). If you think there are good reasons for the attributes to be public instead, then file a ticket explaining why, attach a patch (don't forget unit tests), and add the "review" keyword (http://twistedmatrix.com/trac/wiki/ContributingToTwistedLabs). Because Twisted aims for a very high degree of backwards compatibility in its public interfaces, some care will need to go into this change. The meaning of the attributes will need documentation and unit tests. State transitions need to be considered carefully. Consequences of the attributes being changed by user code need to be taken into account. Perhaps the best place to start is to discuss what functionality becomes possible to implement if the "__inbody" protocol state tracking attribute becomes public. When does an application care about this protocol-level transition? When would it ever be legitimate to change its value outside of the base protocol implementation? Jean-Paul
participants (4)
-
Attila Nagy
-
exarkun@twistedmatrix.com
-
Jason Rennie
-
Phil Christensen