By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 2,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

eval function not working how i want it dag namn

P: n/a
hi everybody
I've written this function to make a list of all of an objects
attributes and methods (not for any reason, I'm just learning)

def list_members(obj)
l = dir(obj)
return map(lambda x : eval('obj.'+x), l)

but I get an error saying that obj isn't defined. As I understand it
eval has access to the current local namespace, which does contain
object, so I'm not sure whats going wrong.

ps Im sure there are better ways of doing the above which doesn't call
the eval function, and while I'd be glad to know what it is, I'd still
like to understand why my way isnt working

thanks

Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
robcarlton wrote:
I've written this function to make a list of all of an objects
attributes and methods (not for any reason, I'm just learning)

def list_members(obj)
l = dir(obj)
return map(lambda x : eval('obj.'+x), l)


That works fine for me with Python 2.4.

This is the best way to do it:

def list_members(obj):
return [getattr(obj, name) for name in dir(obj)]

Although personally I would prefer to have this information in dict
form, so i'd use:

return dict((name, getattr(obj, name)) for name in dir(obj))

For objects defined in CPython, you can use obj.__dict__, but this is
somewhat hacky, and I'd avoid it.
--
Michael Hoffman
Jul 19 '05 #2

P: n/a
How about using the vars builtin?

Michael Hoffman schrieb:
robcarlton wrote:
I've written this function to make a list of all of an objects
attributes and methods (not for any reason, I'm just learning)

def list_members(obj)
l = dir(obj)
return map(lambda x : eval('obj.'+x), l)

That works fine for me with Python 2.4.

This is the best way to do it:

def list_members(obj):
return [getattr(obj, name) for name in dir(obj)]

Although personally I would prefer to have this information in dict
form, so i'd use:

return dict((name, getattr(obj, name)) for name in dir(obj))

For objects defined in CPython, you can use obj.__dict__, but this is
somewhat hacky, and I'd avoid it.


--
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634

Jul 19 '05 #3

P: n/a
robcarlton wrote:
hi everybody
I've written this function to make a list of all of an objects
attributes and methods (not for any reason, I'm just learning)

def list_members(obj)
l = dir(obj)
return map(lambda x : eval('obj.'+x), l)

but I get an error saying that obj isn't defined. As I understand it
eval has access to the current local namespace, which does contain
object, so I'm not sure whats going wrong.

ps Im sure there are better ways of doing the above which doesn't call
the eval function, and while I'd be glad to know what it is, I'd still
like to understand why my way isnt working


It would work if obj were in the local namespace like so:
def f(obj="x"): .... return eval("obj")
.... f() 'x'

but with the lambda you are introducing a nested namespace.
def f(obj="x"): .... return (lambda: eval("obj"))()
.... f() Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in f
File "<stdin>", line 2, in <lambda>
File "<string>", line 0, in ?
NameError: name 'obj' is not defined

You are doing the equivalent to
def f(obj="x"): .... def g(): # no obj in g()'s namespace
.... return eval("obj")
.... return g()
.... f() Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in f
File "<stdin>", line 3, in g
File "<string>", line 0, in ?
NameError: name 'obj' is not defined

only in a convoluted way. Let's make f()'s local variable obj visible in
g(), too:
def f(obj="x"): .... def g():
.... obj # now you can see me
.... return eval("obj")
.... return g()
.... f() 'x'

Here's another way that is viable with lambda, too.
def f(obj="x"): .... return (lambda obj=obj: eval("obj"))()
.... f() 'x'

I'm sure you can fix your list_members() accordingly. Note that rebinding
obj between the definition and call of g() will affect the result only in
the first of the last two examples.

And now for something completely different:
def list_members(obj): .... return [getattr(obj, name) for name in dir(obj)]
.... import os
list_members(os)[:5] [73, 78, 65, 74, 68]


Peter

Jul 19 '05 #4

P: n/a
Michael Hoffman wrote:
def list_members(obj)
l*=*dir(obj)
return*map(lambda*x*:*eval('obj.'+x),*l)


That works fine for me with Python 2.4.


Python 2.4 (#6, Jan 30 2005, 11:14:08)
[GCC 3.3.3 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
def lm(obj): .... l = dir(obj)
.... return map(lambda x: eval("obj." + x), l)
.... class X: .... pass
.... x = X()
x.question = "Are you sure?"
lm(x) Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in lm
File "<stdin>", line 3, in <lambda>
File "<string>", line 0, in ?
NameError: name 'obj' is not defined


Peter

Jul 19 '05 #5

P: n/a
Peter Otten wrote:
Michael Hoffman wrote:
def list_members(obj)
l = dir(obj)
return map(lambda x : eval('obj.'+x), l)


That works fine for me with Python 2.4.

x.question = "Are you sure?"


I should clarify. It works fine for me when I've already globally
assigned obj to something else. D'oh!
--
Michael Hoffman
Jul 19 '05 #6

P: n/a
thanks. I'll use the getattr function now, and I think I understand
where I went wrong with eval. I was thinking in Lisp where the lexical
scope would mean that obj is defined

Jul 19 '05 #7

P: n/a
robcarlton wrote:
thanks. I'll use the getattr function now, and I think I understand
where I went wrong with eval. I was thinking in Lisp where the lexical
scope would mean that obj is defined


The full story is actually more subtle. The name 'obj'
*is* accessible from a nested scope if you do something
like

def f(obj):
def g():
print obj
g()

But the bytecode compiler has to do extra work to make
a name in an intermediate scope accessible to an inner
scope, and it only does this if it sees a reference to
the name in the inner scope. In your code, the reference
is invisible at compile time, so the compiler misses it,
and the run-time evaluation fails.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.