callable classes

D

Duncan Smith

I recently started to rewrite a class and decided to take a bunch of related
methods and put them in a separate callable class. (They're all what I
would call pointwise operations on pairs of instances, they raise exceptions
for identical reasons, and they all return a new instance). It seemed like
a good idea at the time, but now I'm not so sure. So my very general
questions are:

Is this a reasonable use for a callable class?

In what circumstances are callable classes particularly useful? (Maybe I
haven't racked my brains long enough.)

Cheers. TIA.

Duncan
 
A

anton muhin

Duncan said:
I recently started to rewrite a class and decided to take a bunch of related
methods and put them in a separate callable class. (They're all what I
would call pointwise operations on pairs of instances, they raise exceptions
for identical reasons, and they all return a new instance). It seemed like
a good idea at the time, but now I'm not so sure. So my very general
questions are:

Is this a reasonable use for a callable class?

In what circumstances are callable classes particularly useful? (Maybe I
haven't racked my brains long enough.)

Cheers. TIA.

Duncan

What do you mean by 'callable class'? Classes are callables. Ot you mean
a class that defines __call__ method? In this case it's really useful.

regards,
anton.
 
M

Michael Hudson

Duncan Smith said:
I recently started to rewrite a class and decided to take a bunch of related
methods and put them in a separate callable class. (They're all what I
would call pointwise operations on pairs of instances, they raise exceptions
for identical reasons, and they all return a new instance). It seemed like
a good idea at the time, but now I'm not so sure. So my very general
questions are:

Is this a reasonable use for a callable class?

It's hard (for me at least) to see what you mean. Post some code?
In what circumstances are callable classes particularly useful?

Well, I guess you could say when you have a callable with state. I
must admit I prefer to use a bound method in these circumstances, but
I'm not really sure why.

Cheers,
mwh
 
H

Hung Jung Lu

Duncan Smith said:
I recently started to rewrite a class and decided to take a bunch of related
methods and put them in a separate callable class. (They're all what I
would call pointwise operations on pairs of instances, they raise exceptions
for identical reasons, and they all return a new instance). It seemed like
a good idea at the time, but now I'm not so sure. So my very general
questions are:

Is this a reasonable use for a callable class?

Sure. As long as the children callable objects/classes share similar
features that can be factored out to a parent object/class. But one
problem is that Python overloads with () operator with class
constructor.
In what circumstances are callable classes particularly useful? (Maybe I
haven't racked my brains long enough.)

Your case is the representative case. :) The problem isn't with your
line of thought. It is with Python, which has two drawbacks: (a) it's
class-based OOP, instead of prototype-based as languages like Self or
Io. So, usage of classes as objects becomes somewhat cumbersome. (b)
The () operator is overloaded with class constructor call.

The easiest way out is not to use the () operator directly. It is less
confusing if you give it a name, like:

result = my_child_method.process(x1, x2)

Also, it is often easier to separate instances from classes. For your
problem, I would do something like:

class parent(object):
g = "y = y + 4"
def process(self, x, y):
exec self.g
return self.f(x) + y
def f(self, x):
return x

class childA(parent):
g = "y = y + 7"
def process(self, x, y):
result = parent.process(self, x, y)
return result + 1
def f(self, x):
return 3*x
childA = childA() # this converts childA into an object

class childB(parent):
def process(self, x, y):
self.g = "y = y + %d" % x
result = parent.process(self, x, y)
return result + 2
childB = childB() # this converts childB into an object

print childA.process(1, 2) # prints 13
print childB.process(1, 2) # prints 6

This example shows that you have several ways to customize the
behavior of the children methods. They could be made more specific if
you describe your problem more in detail. But I guess you can figure
out the rest on your own.

regards,

Hung Jung
 
D

Duncan Smith

anton muhin said:
What do you mean by 'callable class'? Classes are callables. Ot you mean
a class that defines __call__ method? In this case it's really useful.

regards,
anton.

Yes, I meant a class that defines a __call__ method. Cheers.

Duncan
 
T

Terry Reedy

Duncan Smith said:
Yes, I meant a class that defines a __call__ method. Cheers.

Which makes the *instances* callable, so you meant 'class with callable
instances'.

TJR
 
J

John Roth

Duncan Smith said:
I recently started to rewrite a class and decided to take a bunch of related
methods and put them in a separate callable class. (They're all what I
would call pointwise operations on pairs of instances, they raise exceptions
for identical reasons, and they all return a new instance). It seemed like
a good idea at the time, but now I'm not so sure. So my very general
questions are:

Is this a reasonable use for a callable class?

In what circumstances are callable classes particularly useful? (Maybe I
haven't racked my brains long enough.)

Function Objects (which is what the GoF patterns book calls
these things) are useful whenever you need a function that
requires long term persistant parameter storage. You can
initialize one with the parameters, and then use it many times
without the various callers having to remember the parameters
and pass them to it each time.

Another use is controlling visibility. In this use, it's somewhat
similar to a proxy, except that you don't have to remember the
name of the method to call; you just call the object.

John Roth
 
D

Duncan Smith

Terry Reedy said:
Which makes the *instances* callable, so you meant 'class with callable
instances'.

TJR

Yes. From what I can gather from reverse engineering other's code is that
the approach I am investigating is to create a single (global) callable
instance when the module is imported. Something like,

pointwise_operation = pointwise_operation()

Then in the class where I originally had the pointwise methods I need
something like, e.g.,

def __mul__(self, other): return pointwise_operation.multiply(self, other)

I'm just not clear what the advantages / disadvantages are over just leaving
all the pointwise operation methods in the original class. BTW it first
occurred to me to do this after reading Numeric's MA.py.

Duncan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top