staticmethod and __call__

Neil Schemenauer nas at python.ca
Fri Dec 7 11:06:56 EST 2001


Bruce Eckel wrote:
> I seem to have trouble getting __call__ to behave as a static
> method:
> 
> class Item:
>   def f(): print 'An Item'
> 
> Item.a = 'a'
> Item.b = 'b'
> Item.c = 'c'
> 
> 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)]
> for i in items:
>   i.f()

This is some strange looking code.  Perhaps you could explain what
you're trying to achieve and a more "Pythonic" solution can be devised.

  Neil




More information about the Python-list mailing list