staticmethod and __call__

Bruce Eckel Bruce at EckelObjects.com
Thu Dec 6 17:03:56 EST 2001


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()

Output:
Traceback (most recent call last):
  File "test.py", line 18, in ?
    i.f()
AttributeError: ItemGenerator instance has no attribute 'f'

It doesn't seem to be making the __call__


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