How best to dynamically define methods (and functions)?

  • Thread starter Kenneth McDonald
  • Start date
K

Kenneth McDonald

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
 
E

Erik Max Francis

Kenneth said:
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: ...).
 
E

Erik Max Francis

Erik said:
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 = ...
 
D

Diez B. Roggisch

Kenneth said:
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
 
B

Bjoern Schliessmann

Kenneth said:
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
 

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,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top