David Guerizec <[EMAIL PROTECTED]> writes:
> class Queue:
> """A simple pending queue."""
> msgs = []
> cache = None
> command_recipient = None
> descending = None
> dispose = None
> dispose_def = None
> older = None
> summary = None
> terse = None
> threshold = None
> verbose = 1
> younger = None
> pretend = None
>
> stdout = None
>
> def __init__( self,
> msgs = [],
> cache = None,
> command_recipient = None,
> descending = None,
> dispose = None,
> older = None,
> summary = None,
> terse = None,
> threshold = None,
> verbose = 1,
> younger = None,
> pretend = None ):
>
> self.msgs = msgs
> self.cache = cache
> self.command_recipient = command_recipient
> self.descending = descending
> self.dispose_def = dispose
> self.older = older
> self.summary = summary
> self.terse = terse
> self.threshold = threshold
> self.verbose = verbose
> self.younger = younger
> self.pretend = pretend
> self.wantedstdin = 0
This class appears to have been written as a Singleton; that is, it is
not possible to instantiate more than one instance at a time without
each instance walking all over the other instances' data.
I'm curious if
1) this is intentional? If it is, may I suggest the use of a couple
of OOP and Python conventions to better convey this to the user of
the code?
First, name the class with a leading underscore to indicate that it
should not be imported (and therefore cannot be instantiated by
other modules). Then define a module level "global" and a
"creator" function in Pending. That function would instantiate the
class if it has not yet been created, storing it in the global, and
simply return that single instance if it already has been created.
Something like:
theOneQueue = None
def getQueue(...):
global theOneQueue
if theOneQueue is None:
theOneQueue = _Queue(...)
return theOneQueue
Second, since the variables are all class variables and not
instance variables, would you consider accessing them through the
class name (_Queue.msgs, etc.)? This is another clue to the code
user that there are not copies of each variable per instance.
Python allows either class name or 'self' access to class variables
because of its name search algorithm, but using the class name is a
strong visual clue.
2) it's not intentional, would you delete the class variable
definitions at the beginning of the class, which will cause Python
to assume that any variables accessed through 'self' are instance
variables? That will make the class capable of multiple
instantiations as well as clear up any potential threading issues
in the future.
Any thoughts you have on your class design would be welcome here,
especially in the case that I'm missing something. I like what you've
done.
Tim
_________________________________________________
tmda-workers mailing list ([EMAIL PROTECTED])
http://tmda.net/lists/listinfo/tmda-workers