Shed my a light :)

T

TheSaint

Hi,
I using eval for quite strange reason, as long as I don't know a different
way to implement.

An example:

actions= ('print', 'sum', 'divide', 'myfunction')
parameters=(5, 'nothing',5.63, object)

for routines in actions:
routines(parameters)

I'd like to note that actions are string or string expressions of the program
functions or python itself, so I've in my program something like:

for nn in actions:
eval('cp.%s' %nn)

Where cp is an instance.

So I'm asking here whether exist a way that these string become functions
inside my program, without using eval()
 
C

Chris

Hi,
I using eval for quite strange reason, as long as I don't know a different
way to implement.

An example:

actions= ('print', 'sum', 'divide', 'myfunction')
parameters=(5, 'nothing',5.63, object)

for routines in actions:
     routines(parameters)

I'd like to note that actions are string or string expressions of the program
functions or python itself, so I've in my program something like:

for nn in actions:
       eval('cp.%s' %nn)

Where cp is an instance.

So I'm asking here whether exist a way that these string become functions
inside my program, without using eval()

help(getattr)
Help on built-in function getattr in module __builtin__:

getattr(...)
getattr(object, name[, default]) -> value

Get a named attribute from an object; getattr(x, 'y') is
equivalent to x.y.
When a default argument is given, it is returned when the
attribute doesn't
exist; without it, an exception is raised in that case.


for nn in actions:
func = getattr(cp, nn)
if callable(func):
func(parameters)

or alternatively

for nn in actions:
getattr(cp, nn)(parameters)

Hope that helps.
 
T

TheSaint

8< 8<
getattr(...)
getattr(object, name[, default]) -> value 8< 8<
for nn in actions:
func = getattr(cp, nn)
if callable(func):
func(parameters)
I got the point of Duncan and I should remain on evail() because the
evaluation is made on a construct of string expression, which give me the
final name of the function I want to call.
I've tried on Pyshell and clearly said the object str is not callable.

Some of those string are functions inside the module, so I was expecting a
sequence of calls according the passed in functions names, but they *must*
be processed as a python statements ;(
 
P

Paul Melis

TheSaint said:
8< 8<
getattr(...)
getattr(object, name[, default]) -> value

8< 8<
for nn in actions:
func = getattr(cp, nn)
if callable(func):
func(parameters)

I got the point of Duncan and I should remain on evail() because the
evaluation is made on a construct of string expression, which give me the
final name of the function I want to call.

Why do you still need eval? Can't you just construct the string
describing the function to call and then use getattr() to get the actual
function object to call?
I've tried on Pyshell and clearly said the object str is not callable.

Well no, but I don't think anyone suggested that it would be.
The name of a function to call and the function itself are different
things. getattr() helps you to get from the first to the latter.
Some of those string are functions inside the module, so I was expecting a
sequence of calls according the passed in functions names, but they *must*
be processed as a python statements ;(

This doesn't exactly make sense, as what you want isn't really clear...

Paul
 
T

TheSaint

This doesn't exactly make sense, as what you want isn't really clear....
Sorry, I'm bad to express my toughts even I my nature language :)
I'll give a go to getattr() and see whether the results come in my taste :)
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top