David Guerizec <[EMAIL PROTECTED]> writes:
> Are you sure about that ? I thought a Singleton uses it's class name to
> reference shared member across instances, ie. Queue.cache = cache;
Yes. That's what I was suggesting you do if you wanted it to be a
Singleton. But as your script below indicates, you can also access
shared/class variables through 'self'. Your example __repr__()
successfully accesses the shared variable 'shared' through 'self'.
> and in fact I've made a little test script:
>
> #!/usr/bin/python
>
> class aClass:
> shared = 0
> local = 0
> def __init__(self, name):
> aClass.shared += 1
> self.name = name
>
> def inc(self):
> self.local += 1
>
> def __repr__(self):
> return '[%s]: shared: %s local: %s' %
> (self.name, self.shared, self.local)
>
>
> A = aClass('A')
> B = aClass('B')
> A.inc()
> print A
> print B
>
> # prints:
> # [A]: shared: 2 local: 1
> # [B]: shared: 2 local: 0
This is interesting. It appears that, while you can "read" shared
variables through 'self', you can't "write" them through 'self'.
Python creates a new instance variable when you try to write a shared
variable through self. I have been writing Python for almost 5 years
and was unaware of that distinction. Ya learn something new every
day!
> Did I understand what you're saying, or am I completly mistaken ?
I don't think you are mistaken. I do think the shared copies of the
variables in Queue, which are never used, are confusing, though.
> > 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.
>
> of course, since that doesn't seem to change anything, I can do that.
Exactly - they're never used but they do take up (a little) extra
memory and they lead a potential user of the class to believe that the
instances do share data.
Since your intention is that Queue *not* be a Singleton, I'd just
delete them.
I'm rather excited to see this restructuring. I've been wanting to
see some refactoring happen for some time, but TMDA has been changing
so quickly that we haven't really had the time to sit back and think
about all of the design issues. I definitely believe this is the
right direction for us to be able to grow in the future.
Tim
_________________________________________________
tmda-workers mailing list ([EMAIL PROTECTED])
http://tmda.net/lists/listinfo/tmda-workers