How best to dynamically define methods (and functions)?

Discussion in 'Python' started by Kenneth McDonald, Sep 2, 2007.

  1. I can see an obvious but hacky way to define a Python function at
    runtime. I can't see any obvious way to add a method to a class at
    runtime (though I'm sure one could do just about anything by digging
    into the metaclass stuff, which I will do if needed). But pointers to
    cleaner or easier existing ways to do this would be most appreciated.

    In case it's of interest in the context of the question, I need to
    define a largish set of functions (and similar methods) that define a
    XML-type markup language. Most of these functions will just be of the form

    def fun(...):
    return Node('fun', ...)

    so it'd definitely be nice to just create most of them automatically,
    and only do the special cases by hand.


    Many thanks,
    Ken
    Kenneth McDonald, Sep 2, 2007
    #1
    1. Advertising

  2. Kenneth McDonald wrote:

    > I can see an obvious but hacky way to define a Python function at
    > runtime. I can't see any obvious way to add a method to a class at
    > runtime (though I'm sure one could do just about anything by digging
    > into the metaclass stuff, which I will do if needed). But pointers to
    > cleaner or easier existing ways to do this would be most appreciated.
    >
    > In case it's of interest in the context of the question, I need to
    > define a largish set of functions (and similar methods) that define a
    > XML-type markup language. Most of these functions will just be of the form
    >
    > def fun(...):
    > return Node('fun', ...)
    >
    > so it'd definitely be nice to just create most of them automatically,
    > and only do the special cases by hand.


    Something like::

    method = ['fun', ...]
    for method in methods:
    setattr(MyClass, method, lambda *x: Node(method, *x))

    The first argument here will be the implicit self, if you don't want
    that, strip off the first argument (or use lambda self, *x: ...).

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
    I like young girls. Their stories are shorter.
    -- Thomas McGuane
    Erik Max Francis, Sep 2, 2007
    #2
    1. Advertising

  3. Erik Max Francis wrote:

    > Something like::
    >
    > method = ['fun', ...]
    > for method in methods:
    > setattr(MyClass, method, lambda *x: Node(method, *x))


    Err, that first line was supposed to be methods = ...

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
    I like young girls. Their stories are shorter.
    -- Thomas McGuane
    Erik Max Francis, Sep 2, 2007
    #3
  4. Kenneth McDonald schrieb:
    > I can see an obvious but hacky way to define a Python function at
    > runtime. I can't see any obvious way to add a method to a class at
    > runtime (though I'm sure one could do just about anything by digging
    > into the metaclass stuff, which I will do if needed). But pointers to
    > cleaner or easier existing ways to do this would be most appreciated.
    >
    > In case it's of interest in the context of the question, I need to
    > define a largish set of functions (and similar methods) that define a
    > XML-type markup language. Most of these functions will just be of the form
    >
    > def fun(...):
    > return Node('fun', ...)
    >
    > so it'd definitely be nice to just create most of them automatically,
    > and only do the special cases by hand.


    Then don't do it that way, but use __getattr__. It will exactly do what
    you want:


    class Foo(object):
    def __getattr__(self, name):
    return Node(name, ....)


    def some_node(self):
    ... # hand coded stuff


    Diez
    Diez B. Roggisch, Sep 2, 2007
    #4
  5. Kenneth McDonald wrote:

    > I can see an obvious but hacky way to define a Python function at
    > runtime.


    What way is this? All Python function definitions in your code are
    executed at runtime.

    > In case it's of interest in the context of the question, I need to
    > define a largish set of functions (and similar methods) that
    > define a XML-type markup language. Most of these functions will
    > just be of the form
    >
    > def fun(...):
    > return Node('fun', ...)
    >
    > so it'd definitely be nice to just create most of them
    > automatically, and only do the special cases by hand.


    This looks cumbersome to me. If you reworked the interface (perhaps
    using dicts or a generic function) it might get clearer.

    Regards,


    Björn

    --
    BOFH excuse #241:

    _Rosin_ core solder? But...
    Bjoern Schliessmann, Sep 2, 2007
    #5
    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. Xiangliang Meng
    Replies:
    1
    Views:
    1,595
    Victor Bazarov
    Jun 21, 2004
  2. vizcayno
    Replies:
    13
    Views:
    452
    Frank Millman
    Jan 9, 2007
  3. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    460
    Brian Takita
    Jan 23, 2006
  4. Kenneth McDonald
    Replies:
    5
    Views:
    314
    Kenneth McDonald
    Sep 26, 2008
  5. Replies:
    17
    Views:
    152
    Thomas 'PointedEars' Lahn
    Mar 2, 2008
Loading...

Share This Page