bound vs unbound functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Tobis

    #1

    bound vs unbound functions

    I'm trying to do metaprogramming . I'm sure I've got this all wrong
    wrong wrong, but somehow my approach hasn't yet hit a brick wall.

    Anyway, I'd like to dynamically add a method to an instance at
    instantiation time. Something like

    ######
    In [71]: class quux(object):
    .....: def __init__(self,s tuff):
    .....: template = "def foo(self,b): print b + %s" % stuff
    .....: exec(template)
    .....: self.bazz = foo
    .....:


    In [72]: q = quux(5)

    In [73]: q.bazz(4)
    ---------------------------------------------------------------------------
    TypeError Traceback (most recent call
    last)

    /Users/tobis/PyNSol/<console>

    TypeError: foo() takes exactly 2 arguments (1 given)

    In [74]: q.bazz("not much",4)
    9
    ########

    So the straightforward question is why, even though bazz is a method of
    class quux, it doesn't have that extra call parameter 'self'. Is this a
    problem? If I actually need a reference to self is it OK to do:

    In [76]: q.bazz(q,4)

    ?

    The more vague question is why do people despise 'exec', and how should
    I do this sort of thing instead?

    mt

    PS - any idea how to get past google's stupid formatting these days? I
    thought they were supposed to like python, but they just ignore leading
    blanks.

  • Steven Bethard

    #2
    Re: bound vs unbound functions

    Michael Tobis wrote:[color=blue]
    > Anyway, I'd like to dynamically add a method to an instance at
    > instantiation time. Something like[/color]

    Nearly identical question from yesterday and a series of answers:



    Steve

    Comment

    • Terry Reedy

      #3
      {Spam?} Re: bound vs unbound functions


      "Michael Tobis" <mt@3planes.com > wrote in message
      news:1107040729 .535936.42230@c 13g2000cwb.goog legroups.com...
      [color=blue]
      > I'd like to dynamically add a method to an instance at
      > instantiation time.[/color]

      No can do. A method is function that is an attribute of a class, even if
      accessed via an instance. A function added to an instance as an attribute
      of the instance remains a function. It is instance-specific data that
      methods and other code can use, just like other instance data, for
      instance-specific effects. Two common examples:

      class composer:
      # skip obvious init
      def __call__(self, x): return self.outer(self .inner(x))

      sincos = composer(math.s in, math.cos)
      # yes, this can also be done with composer function with nested scope

      class memoizer: # posted several times
      # skip init again
      def __call__(self,x ):
      # return previously computed self.memodict[x] if it exists
      # or calculate, store, and return self.func(x)

      \> PS - any idea how to get past google's stupid formatting these days? I[color=blue]
      > thought they were supposed to like python, but they just ignore leading
      > blanks.[/color]

      Don't use google to post on clp. Go to news.gmane.org instead.

      Terry J. Reedy



      Comment

      • Kent Johnson

        #4
        Re: bound vs unbound functions

        Michael Tobis wrote:[color=blue]
        > I'm trying to do metaprogramming . I'm sure I've got this all wrong
        > wrong wrong, but somehow my approach hasn't yet hit a brick wall.
        >
        > Anyway, I'd like to dynamically add a method to an instance at
        > instantiation time. Something like
        >
        > ######
        > In [71]: class quux(object):
        > ....: def __init__(self,s tuff):
        > ....: template = "def foo(self,b): print b + %s" % stuff
        > ....: exec(template)
        > ....: self.bazz = foo
        > ....:
        >
        >
        > In [72]: q = quux(5)
        >
        > In [73]: q.bazz(4)
        > ---------------------------------------------------------------------------
        > TypeError Traceback (most recent call
        > last)
        >
        > /Users/tobis/PyNSol/<console>
        >
        > TypeError: foo() takes exactly 2 arguments (1 given)
        >
        > In [74]: q.bazz("not much",4)
        > 9
        > ########[/color]

        The thread Steve quoted suggests using new.instancemet hod():

        import new

        class quux(object):
        def __init__(self,s tuff):
        template = "def foo(self,b): print b + %s" % stuff
        exec(template)
        self.bazz = new.instancemet hod(foo, self, quux)


        There is no need for exec; you can define foo() directly as a nested function:

        class quux(object):
        def __init__(self,s tuff):
        def foo(self,b):
        print b + stuff
        self.bazz = new.instancemet hod(foo, self, quux)


        Of course for this simple example you can just remember stuff as an attribute:

        class quux(object):
        def __init__(self,s tuff):
        self.stuff = stuff

        def bazz(self, b):
        print b + self.stuff

        Kent
        [color=blue]
        >
        > So the straightforward question is why, even though bazz is a method of
        > class quux, it doesn't have that extra call parameter 'self'. Is this a
        > problem? If I actually need a reference to self is it OK to do:
        >
        > In [76]: q.bazz(q,4)
        >
        > ?
        >
        > The more vague question is why do people despise 'exec', and how should
        > I do this sort of thing instead?[/color]

        Comment

        • Terry Reedy

          #5
          Test of comp.lang.pytho n to python-list to gmane pathway.

          Feel free to ignore this. Some system falsely labeled this post (sent via
          gmane) as possible spam. I am sending it back in the opposite direction to
          see what transpires that way. Sorry for the disturbance. tjr

          "Terry Reedy" <tjreedy@udel.e du> wrote in message
          news:cthdth$fp0 $1@sea.gmane.or g...[color=blue]
          >
          > "Michael Tobis" <mt@3planes.com > wrote in message
          > news:1107040729 .535936.42230@c 13g2000cwb.goog legroups.com...
          >[color=green]
          >> I'd like to dynamically add a method to an instance at
          >> instantiation time.[/color]
          >
          > No can do. A method is function that is an attribute of a class, even if
          > accessed via an instance. A function added to an instance as an
          > attribute of the instance remains a function. It is instance-specific
          > data that methods and other code can use, just like other instance data,
          > for instance-specific effects. Two common examples:
          >
          > class composer:
          > # skip obvious init
          > def __call__(self, x): return self.outer(self .inner(x))
          >
          > sincos = composer(math.s in, math.cos)
          > # yes, this can also be done with composer function with nested scope
          >
          > class memoizer: # posted several times
          > # skip init again
          > def __call__(self,x ):
          > # return previously computed self.memodict[x] if it exists
          > # or calculate, store, and return self.func(x)
          >
          > \> PS - any idea how to get past google's stupid formatting these days? I[color=green]
          >> thought they were supposed to like python, but they just ignore leading
          >> blanks.[/color]
          >
          > Don't use google to post on clp. Go to news.gmane.org instead.
          >
          > Terry J. Reedy
          >
          >
          >
          > --
          > http://mail.python.org/mailman/listinfo/python-list
          >[/color]


          Comment

          Working...