[Tutor] Dynamically naming functions

Ed Singleton singletoned at gmail.com
Mon Mar 13 12:24:20 CET 2006


On 10/03/06, Alan Gauld <alan.gauld at freenet.co.uk> wrote:
> > How does one go about creating functions, classes, or callable objects
> > when you don't know their name in advance? (For example you want to
> > read their names in from a text file or database).
>
> First point, names of functions are no different to names of other things.
>
> def f(x):
>     y = blah....
>     return y
>
> is essentialy the same as saying
>
> f = lambda x: blah...
>
> lambda defines a nameless (aka anonymous) function and then assigns that
> function to the variable called f.
>
> Of course Pythons lambda construct is a bit brain dead so we can achieve
> the same result with just assigning an existing function to a new name
>
> def f(x):
>    y = nblah...
>    return y
>
> g = f
>
> Now g is a new name that refers to the function called f.

I have to say I assumed that if you then changed f, g would point to
the new f rather than the old f, but I tried it out and it seems
otherwise:

>>> def f(x):
...     print x
...
>>> f(1)
1
>>> a = f
>>> a(1)
1
>>> def f(x):
...     print x * 2
...
>>> f(1)
2
>>> a(1)
1
>>> b = f
>>> b(1)
2

Which is really cool, except that the names "a" and "b" need to be
decided at run time.

> And with nested function definitions we can write functions that
> return functions:
>
> def makeMultiplyAndAdd(constants):
>     def f(x):
>         return constant[0] * constant[1] + x
>     return f
>
>
> > I'd like to load these from a database (using SQLObject),
> > but I'm not sure how I can define the name of the function
> > from a field in a database (or read in from a text file).
>
> Same way as you would for any other kind of variable.
> This has been discussed many times and there is some trickery you
> can do using the built in dictionaries that Python uses for its namespaces.
> But the biggest proiblem with introducing new names at run time is:
> How does the existing code get to know about those names that
> didn't exist when the code was written? Ypou need to have the code
> go exploring for names, work out what kind ioopf value they hold etc...
> It all gets very messy and complicated and fault prone.
>
> So the normal way to do this is to use a dictionary.
> The dictionary is a collection of names with values asociated with them.
> Those values can be functions or classes or anything else.
>
> Now your code can access the new names by using standard
> dictionary iterators etc. All you need are some conventions around
> how many parameters the functions have, their types etc.
>
> > I'd also like to be able to do this in CherryPy/TurboGears
> > so that I can create a dynamic site structure based on fields
> > in a database.
>
> Dynamic site structure shouldn't need dynamic creation of functions
> although the structure might need to be dynamically loaded into a
> data structure in the code. It might also be a parameter of the functions.

Doesn't CherryPy/TurboGears require Classes and Functions for it's
data structures?  (Or parameters passed to them, of course).

I had been thinking that I would load my whole site at startup, but I
guess it would probably be better to write a function that takes the
parameters and looks up the first to see if it exists as a page, then
looks to see if the second is a child of the first etc.  But this
really feels like something CherryPy should be doing for me.

> But as always remember that dynamic anything usually means
> much harder to maintain. All of your backup/archiving tools and error
> reporting tools etc will need to understand your dynamic structure too.
> Dynamic sounds like fun but usually means grief and is only justified
> as a last resort IMHO!

For website, I can't really see how I can not have a dynamic
structure.  There's no way I'm writing a function for each "folder". 
I do take your point though, however you often find that it's easy to
maintain something dynamic than huge amounts of more static stuff (as
in the difference between hundreds of static web pages and using a cms
of some kind).

> But if you must go dynamic Python is as good a language to do it
> in as you'll get.

That's certainly been my experience so far..

Thanks

Ed


More information about the Tutor mailing list