Callable modules?

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Wed Jul 24 17:28:58 EDT 2002


Alex Martelli <aleax at aleax.it> wrote:
>How is it more convenient to pass, say, object X, rather than
>to pass bound method (e.g.) X.primary?  Take your example:

Well, here's one example:

>>> class A: 
...    def __call__(self, x): return x+self.x
... 
>>> a = A()
>>> a.x = 3
>>> a(2)
5
>>> a.x = 4
>>> a(2)
6
>>> 
>>> class B:
...    def call(self, x): return x+self.x
... 
>>> b = B()
>>> b.x = 3
>>> b.call(2)
5
>>> b.x = 4
>>> b.call(2)
6
>>> 

If you take b as the object, you need to use explict call.  If you take
b.call as object, you can't do other things to the object.  Callable objects
can be viewed as parameterized functions.  Regarding whether the parameters
can be modified later, it is similar to functions with attributes, but not
similar to bound methods.

Huaiyu



More information about the Python-list mailing list