dynamic path to a method ???

Alex Martelli aleax at aleax.it
Fri Jul 5 16:50:13 EDT 2002


Roger Ineichen wrote:
        ...
> class (instance) = 'personManager'

I'll have to assume this is classInstance or something like that, because
the syntax is unlikely to make sense.

> method = 'addPerson(firstname='', lastname='')'

This is equivalent to:

method = 'addPerson(firstname=, lastname=)'

so I'm not sure why you're indicating the extra quotes -- maybe you
thought they would be somehow preserved?  They aren't -- each single
quote closes a string (and the immediately following one then opens
another string literal and Python juxtaposes the adjacent literals).  Maybe
you mean:

method = "addPerson(firstname='', lastname='')"

???

> attribute1, firstname = 'max'
> attribute2, lastname = 'miller'

These will fail -- when you assign something to two names in this
way, the 'something' needs to be a sequence of exactly two items.


> how can I construct a dynamic path like:
> personManager.addPerson( firstname='max', lastname='miller')

You can easily construct this string by string manipulation.  But it's
the wrong approach, because then executing such a constructed
statement is fraught with dangers -- any error, any malice on the
user's part, and you could be reformatting your hard disk or the like.

Given all the doubts and strangeness in the foregoing I'm not going
into more details at this point, but no matter what, one point IS quite
crucial: don't build up statements or expressions for the purpose of
using exec or eval on them unconstrainedly.  You'll be a far happier
person if you avoid this trap.


Alex




More information about the Python-list mailing list