RE: run time code generation in python

Discussion in 'Python' started by Robert Brewer, Oct 9, 2003.

  1. "Carlo v. Dango" <> wrote in
    news:Xns940F929A63B436020206@172.16.0.1:

    >
    > Hello there. I have found a need to runtime generate a method and
    > instert it into an object instance. The code is a simple forwarding
    > mechanism like
    >
    > def foo(self, *args, **kwargs):
    > self.i.foo(*args, **kwargs)
    >
    > method.. however, it is only at runtime that I know the name "foo" so
    > I cannot gerenate such a method any sooner.


    Duncan Booth wrote:
    > You should be aware that if you store a function in an instance at

    runtime,
    > the magic binding to 'self' won't happen. You either need to store the


    > function in the class, or leave out the self parameter if you want to
    > insert it into an instance.


    You can obtain the magic binding to self with the "new" module's
    instancemethod() function.

    cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/198078


    Robert Brewer
    MIS
    Amor Ministries
     
    Robert Brewer, Oct 9, 2003
    #1
    1. Advertising

  2. On Thu, 9 Oct 2003 11:08:36 -0700, Robert Brewer <> wrote:

    > "Carlo v. Dango" <> wrote in
    > news:Xns940F929A63B436020206@172.16.0.1:
    >
    >>
    >> Hello there. I have found a need to runtime generate a method and
    >> instert it into an object instance. The code is a simple forwarding
    >> mechanism like



    > You can obtain the magic binding to self with the "new" module's
    > instancemethod() function.
    >
    > cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/198078


    sounds great! many thanks, will have a look at it. I assume that such a
    representation uses less memory than adding a function (an object, right?)
    to an instance...

    -carlo


    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
     
    Carlo v. Dango, Oct 16, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlo v. Dango

    run time code generation in python

    Carlo v. Dango, Oct 9, 2003, in forum: Python
    Replies:
    6
    Views:
    376
    Bengt Richter
    Oct 12, 2003
  2. Ian Bicking

    Re: run time code generation in python

    Ian Bicking, Oct 9, 2003, in forum: Python
    Replies:
    1
    Views:
    431
    Carlo v. Dango
    Oct 9, 2003
  3. flamesrock
    Replies:
    8
    Views:
    473
    Hendrik van Rooyen
    Nov 24, 2006
  4. Pierre Yves
    Replies:
    2
    Views:
    489
    Pierre Yves
    Jan 10, 2008
  5. John W. Long

    HTML Generation (Next Generation CGI)

    John W. Long, Nov 22, 2003, in forum: Ruby
    Replies:
    4
    Views:
    344
    John W. Long
    Nov 24, 2003
Loading...

Share This Page