[Tutor] Ok, take one step back and let you look
K P
teroc@zianet.com
Thu, 30 Sep 1999 15:12:49 -0500
Hmmm, I can see the possiblities in other areas. Thanks for the
'snippet'. Maybe a small example would refine what I am seeking:
Quick example: 3 books: red, blue, green. All instances of class
Book. 3 glasses: yellow, purple, aqua, all instances of class
Glassware. In my inventory, I would like them to appear thus:
Item Qty
Book 3
Glass 3
Not like this, which is currently the case:
Item Qty
Blue 1
Green 1
Red 1
yellow 1
purple 1
aqua 1
I'm not too concerned with having tons of instances of a particular
class/object. Just how they would be tracked in the inventory, and
also displayed(on screen). If I am still not being very clear with this,
I shall try harder to clarify myself :)
Ken
[big snip]
>
> It sounds like you want some form of "semaphore" when creating class
> instances: a certain number of instances of that class can be
> created, but beyond that the semaphore "blocks" (raises an exception
> in this case). Here is a mix-in you can look at and see how to work
> into your code.
>
> class InstanceLimiter:
> class InstanceLimiterError(Exception):
> pass
> _Instance_Limiter_count = 10
> def new_instance(self):
> klass = self.__class__
> if klass._Instance_Limiter_count > 0:
> klass._Instance_Limiter_count = klass._Instance_Limiter_count - 1
> else:
> raise self.InstanceLimiterError, \
> 'too many instances of %s' % klass.__name__
> def remove_instance(self):
> klass = self.__class__
> klass._Instance_Limiter_count = klass._Instance_Limiter_count + 1
>
> Then you can work it into a class like:
>
> class Person(InstanceLimiter):
> """A person under population control."""
> _Instance_Limiter_count = 1000
> def __init__(self, name, location, birthdate):
> self.new_instance()
> ...
> def __del__(self):
> self.remove_instance()
> ...
>
> Now only one thousand instances of Person can exist.
>
> I'm not sure if this is what you want, if not maybe you can use the
> techniques in other places.
>
> -Arcege
>
>
> --
> ------------------------------------------------------------------------
> | Michael P. Reilly, Release Engineer | Email: arcege@shore.net |
> | Salem, Mass. USA 01970 | |
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor