staticmethod and __call__

Bruce Eckel Bruce at EckelObjects.com
Fri Dec 7 10:04:54 EST 2001


I suppose a generator could be thought of as a special case of a
factory, but it's generally considered a different kind of entity.


*********** REPLY SEPARATOR  ***********

On 12/7/01 at 2:16 PM Luigi e Viviana Ballabio wrote:

>Bruce,
>
>>  class ItemGenerator:
>>    import random
>>    rgen = random.Random()
>>    items = [j for j in vars(Item).values() if isinstance(j,
Item)]
>>    def __call__():
>>      return ItemGenerator.rgen.choice(ItemGenerator.items)
>>    __call__ = staticmethod(__call__)
>
>>  items = [ItemGenerator() for i in range(5)]
>
>apart from the __call__-not-being-overridden issue:
>
>since you are trying to Think in Patterns, and since what you are 
>after is some kind of random Item factory, it might make more
sense 
>to name the above correspondingly, as in:
>
>class Item:
>     whatever
>
>class ItemFactory:
>     import random
>     etc
>     def create():
>         return ItemFactory.rgen.choice(ItemFactory.items)
>     create = staticmethod(create)
>
>items = [ItemFactory.create() for i in range(5)]
>
>principle-of-least-surprisely yours,
>					Luigi
>
>--



Most current information can be found at:
http://www.mindview.net/Etc/notes.html
===================
Bruce Eckel    http://www.BruceEckel.com
Contains free electronic books: "Thinking in Java 2e" & "Thinking
in C++ 2e"
Please subscribe to my free newsletter -- just send any email to:
join-eckel-oo-programming at earth.lyris.net
My schedule can be found at:
http://www.mindview.net/Calendar
===================






More information about the Python-list mailing list