[Twisted-Python] Why classes in twisted.protocols.basic use "static member" to store states?

Thomas Hervé therve at free.fr
Fri Mar 30 04:39:57 EDT 2007

Quoting ?? <littlesweetmelon at gmail.com>:

> Hi,
>    I'm a newbie. After went through the source code in
> twisted.protocols.basic, I found it's really strange that all classes
> in twisted.protocols.basic use "static member" to store states.
>    Take class StatefulStringProtocol as the example. Some Factory
> will creates a group of StatefulStringProtocol instances which share
> the static member "state". But why all instances MUST have the same
> state? In my opinion, instance A may be in the "handshaking" state,
> while instance B may be in the "data sending" state. The static member
> can not represent this case.
>    Therefore, I think protocols should use "member variable" rather
> that "static member":
> class StatefulStringProtocol:
> 	def __init__(self):
> 		self.state = 'init'
> ...
>    Could you plz give me some clue? I am a little confused. Most
> possibly, I misunderstanding the design about protocols-factory.

In fact, you're misunderstanding Python :). The fact that the variable is
defined as class member doesn't make shared between instances. You can 
see some
explanation here:


More information about the Twisted-Python mailing list