class methods ?
Edward Welbourne
eddyw at lsls4p.lsl
Wed Jan 12 10:58:18 EST 2000
Not in 1.3 (what I have available at work to test), and I doubt it in
1.5 also. Basically, if a class has an attribute with a callable value,
the only way you can call it is by calling it as a method of an
instance, so it has to have a first argument for that purpose, even if
this is to be ignored.
You can, of course, cheat - using introspection. In python 1.3, for
instance,
>>> class dummy:
... def __init__(self):
... try: count = dummy.invoked['__init__']
... except KeyError: count = 0
... dummy.invoked['__init__'] = 1 + count
... def printer(): print dummy.invoked
... invoked = {}
...
>>> dummy.printer()
TypeError: unbound method must be called with class instance 1st argument
>>> dummy.printer.im_func()
{}
>>> dummy()
<dummy instance at aeed8>
>>> dummy.printer.im_func()
{'__init__': 1}
though I think the introspection name (.im_func) is different in 1.5
(and you could use dummy.invoked.get('__init__', 0) in 1.5 to obtain
count, instead of needing the try...except).
On the other hand, I have a plan which will make it easy to have class
methods (among other tasty goodies) in python 2, if I can persuade the
Great, the Good and, most of all, the Guido in a fortnight's time, but I
have to finish turning it into something somewhat more intelligible ...
sadly, writing for humans isn't my forte. More news by the end of
Sunday, I promise (I hate deadlines, but they're good for productivity).
Eddy.
More information about the Python-list
mailing list