generating method names 'dynamically'

Daniel Nogradi nogradi at
Thu Jan 26 19:53:17 EST 2006

> > My database has 1 table with 2 fields, one called 'name' and the other
> > one called 'age', let's suppose it has the following content, but this
> > content keeps changing:
> >
> > Alice 25
> > Bob  24
> >
> > ----------- ----------------
> >
> > class klass:
> >
> >      # do the stuff with getattr using the database
> >      # but in a way that after the database changes
> >      # I don't need to rewrite this part
> >
> >
> > inst = klass()
> >
> > ---------- ------------------
> >
> > import program1
> >
> > # This should print 'Hello, my name is Bob and I'm 24'
> > program1.inst.Bob()
> Where is the code that should print that?

The code should go into class klass: in place of the comments, if I
would know what to write there I wouldn't be asking the question :)
Sorry, if that was not clear.

> The code has to be defined,
> and is presumably outside your database, so *that* is the method you
> need to call, not something generated dynamically at runtime.
> > # This should print 'Hi, I'm 25 and I'm Alice'
> > program1.inst.Alice()
> That actually prints something different... did you intentionally change
> the order?  In other words, is the code that generates that output for
> Alice() actually different than the code for Bob() (and if so, where
> would you define it?) or did you just write the example imprecisely?

Sorry, I just wanted to illustrate that the output should depend on
the method called.
But maybe it's imprecise and misleading so let's forget about the
difference in ordering.

> Ultimately, I think the question here is "why do you want to do that"?
> Normally you would just make Bob and Alice parameters, as in:
> program1.inst.output("Alice")  # prints whatever you wanted above
> program1.inst.output("Bob") # prints whatever you wanted above, assuming
> the differences in the two outputs was inadvertent
> program1.inst.output("John") # raise exception

Well, I would normally do what you suggest, using parameters, but in
the example at hand I have to have the method names as variables and
the reason is that the whole thing will be run by apache using
mod_python and the publisher handler. There a URL is mapped to the 'Bob' method of the
file and I want to be able to have URL's with different
names. I know I can solve this problem with parameters and functions
and using the GET http method, but I would like to have pretty URL's
without & and ? signs. I didn't want to ask this on the mod_python
list because after all it's a pure python question.

So I try again, hopefully it will be clearer this time:

--------- database content -----------

Alice 25
Bob  24

--------- -------------

class klass:

     # My question is what I should write here
     # assuming that I can query the database
     # using the names of the fields (name and age)
     # but without knowing in advance what will be
     # the result of the query.
     # The desired result of the code that should
     # come here (which I don't know, but asking you guys)
     # will be described below.

inst = klass()

--------- -----------

import program1

# The code in klass above should be such that the following
# line should print 'Hello my name is Bob and I'm 24.'

# The code in klass above should be such that the following
# line should print 'Hello my name is Alice and I'm 25.'

# The code in klass above should be such that the following
# line should print 'There was an error.'


I hope it's clear now that what I need is variable method names, the
wording 'generating' was probably also misleading and imprecise.

Thank you very much in any case.

More information about the Python-list mailing list