generating method names 'dynamically'

Bengt Richter bokr at oz.net
Fri Jan 27 06:24:31 EST 2006


On Fri, 27 Jan 2006 01:20:52 +0100, Daniel Nogradi <nogradi at gmail.com> wrote:

>> > Is it possible to have method names of a class generated somehow
>> dynamically?
>> >
>> > More precisely, at the time of writing a program that contains a class
>> > definition I don't know what the names of its callable methods should
>> > be. I have entries stored in a database that are changing from time to
>> > time and I always want to be able to call (from another program) those
>> > method names which are at the moment present in the database.
>>
>> Sounds somehow more like all you need is to learn about __getattr__ and
>> maybe __call__ instead of actually generating methods.  In other words,
>> don't generate anything, just intercept attempts to call things that
>> were produced by accessing the attributes of an object.
>>
>> Whether that would work or not depends on things you haven't said.  The
>> above spec is a little unclear, given among other things that you don't
>> "call method names", that the code in the methods you would presumably
>> want to call has to exist somewhere and you haven't described what that
>> code would be, and I can't tell what those database "entries" are really
>> all about other than that they somehow refer to the names of things that
>> you think you want to call as methods. :-)
>>
>> Perhaps an example is in order...
>>
>
>Thanks for all the replies, it became clear that I need to look into
>getattr, __getattr__ and __call__. I'll do that, but since you asked,
>here is the thing I would like to do in a little more detail:
>
>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:
While the program is running? Or between runs of the program?
Either you will need to query the database for each pseudo-method call,
or you will be able to prebuild all the methods at startup or anything between.

>
>Alice 25
>Bob  24
>
>----------- program1.py ----------------
>
>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 =3D klass()
>
>---------- program2.py ------------------
>
>import program1
>
># This should print 'Hello, my name is Bob and I'm 24'
>program1.inst.Bob()
>
># This should print 'Hi, I'm 25 and I'm Alice'
>program1.inst.Alice()
Those two messages differ by more than (name, age) content.
Where is the different text coming from, if you are building
these responses totally dynamically?

>
># This should print an error message, since there is no
># field in the database with name=3DJohn
>program1.inst.John()

I suppose you want the klass to have more methods than
just the dynamically built/simulated ones? Otherwise, why
not just define a function that takes a name and goes to the
data base to get the data and then prints the message. E.g.,
    program1.print_the_message('John')
instead of
    program1.inst.John()
No class needed.
OTOH, if you are going to use a class, you might want to name it
capitalized (more conventional) and derive from object, or subclass
from something else if it makes sense. I.e. class Klass(object): ...

What kind of "database" are you accessing? An RDBMS? A CSV text file?
A directory full of named single-line files (ugh), or?

How often will you access the data? Are you the only one?
Requirements, requirements ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list