callable classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Duncan Smith

    callable classes

    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

  • anton muhin

    #2
    Re: callable classes

    Duncan Smith wrote:[color=blue]
    > 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
    >[/color]

    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.

    Comment

    • Michael Hudson

      #3
      Re: callable classes

      "Duncan Smith" <buzzard@urubu. freeserve.co.uk > writes:
      [color=blue]
      > 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?[/color]

      It's hard (for me at least) to see what you mean. Post some code?
      [color=blue]
      > In what circumstances are callable classes particularly useful?[/color]

      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

      --
      Exam invigilation - it doesn't come much harder than that, esp if
      the book you're reading turns out to be worse than expected.
      -- Dirk Bruere, sci.physics.res earch

      Comment

      • Hung Jung Lu

        #4
        Re: callable classes

        "Duncan Smith" <buzzard@urubu. freeserve.co.uk > wrote in message news:<bshtlg$m1 r$1@news6.svr.p ol.co.uk>...[color=blue]
        > 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?[/color]

        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.
        [color=blue]
        > In what circumstances are callable classes particularly useful? (Maybe I
        > haven't racked my brains long enough.)[/color]

        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

        Comment

        • Duncan Smith

          #5
          Re: callable classes


          "anton muhin" <antonmuhin@ram bler.ru> wrote in message
          news:bsi06a$d53 2h$1@ID-217427.news.uni-berlin.de...[color=blue]
          > Duncan Smith wrote:[color=green]
          > > I recently started to rewrite a class and decided to take a bunch of[/color][/color]
          related[color=blue][color=green]
          > > methods and put them in a separate callable class. (They're all what I
          > > would call pointwise operations on pairs of instances, they raise[/color][/color]
          exceptions[color=blue][color=green]
          > > for identical reasons, and they all return a new instance). It seemed[/color][/color]
          like[color=blue][color=green]
          > > 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[/color][/color]
          I[color=blue][color=green]
          > > haven't racked my brains long enough.)
          > >
          > > Cheers. TIA.
          > >
          > > Duncan
          > >[/color]
          >
          > 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.[/color]

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

          Duncan

          Comment

          • Terry Reedy

            #6
            Re: callable classes


            "Duncan Smith" <buzzard@urubu. freeserve.co.uk > wrote in message
            news:bskkig$sf6 $1@news7.svr.po l.co.uk...[color=blue][color=green]
            > > What do you mean by 'callable class'? Classes are callables. Ot you[/color][/color]
            mean[color=blue][color=green]
            > > a class that defines __call__ method? In this case it's really[/color][/color]
            useful.[color=blue][color=green]
            > >
            > > regards,
            > > anton.[/color]
            >
            > Yes, I meant a class that defines a __call__ method. Cheers.[/color]

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

            TJR


            Comment

            • John Roth

              #7
              Re: callable classes


              "Duncan Smith" <buzzard@urubu. freeserve.co.uk > wrote in message
              news:bshtlg$m1r $1@news6.svr.po l.co.uk...[color=blue]
              > I recently started to rewrite a class and decided to take a bunch of[/color]
              related[color=blue]
              > methods and put them in a separate callable class. (They're all what I
              > would call pointwise operations on pairs of instances, they raise[/color]
              exceptions[color=blue]
              > for identical reasons, and they all return a new instance). It seemed[/color]
              like[color=blue]
              > 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.)[/color]

              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
              [color=blue]
              >
              > Cheers. TIA.
              >
              > Duncan
              >[/color]


              Comment

              • Duncan Smith

                #8
                Re: callable classes


                "Terry Reedy" <tjreedy@udel.e du> wrote in message
                news:_bydnV23fr iKY3CiRVn-gQ@comcast.com. ..[color=blue]
                >
                > "Duncan Smith" <buzzard@urubu. freeserve.co.uk > wrote in message
                > news:bskkig$sf6 $1@news7.svr.po l.co.uk...[color=green][color=darkred]
                > > > What do you mean by 'callable class'? Classes are callables. Ot you[/color][/color]
                > mean[color=green][color=darkred]
                > > > a class that defines __call__ method? In this case it's really[/color][/color]
                > useful.[color=green][color=darkred]
                > > >
                > > > regards,
                > > > anton.[/color]
                > >
                > > Yes, I meant a class that defines a __call__ method. Cheers.[/color]
                >
                > Which makes the *instances* callable, so you meant 'class with callable
                > instances'.
                >
                > TJR
                >[/color]

                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_opera tion = pointwise_opera tion()

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

                def __mul__(self, other): return pointwise_opera tion.multiply(s elf, 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

                Comment

                Working...