Re: Noobie python shell question
On Sun, 29 Nov 2009 22:03:09 -0500, tuxsun wrote: I've been working in the shell on and off all day, and need to see if a function I defined earlier is defined in the current shell I'm working in. Is there a shell command to get of list of functions I've defined? How about this: ## python Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. a,b,c = 1,2,3 def spam(): return None ... def eggs(): return None ... dir() ['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b', 'c', 'eggs', 'spam'] locals() {'a': 1, 'c': 3, 'b': 2, 'spam': , '__builtins__': '__builtin__' (built-in)>, 'eggs': , '__package__': None, '__name__': '__main__', '__doc__': None} [name for name in locals() if callable(locals()[name])] Traceback (most recent call last): File "", line 1, in RuntimeError: dictionary changed size during iteration [name for name in locals() if callable(locals()[name])] ['spam', 'eggs'] ## To avoid the harmless RuntimeError, define "name" before using it in the list comprehension (the final expression) -- e.g.: name = 1 -John -- http://mail.python.org/mailman/listinfo/python-list
Re: Noobie python shell question
Tim Chase wrote: (as an aside, is there a way to get a local/global variable from a string like one can fetch a variable from a class/object with getattr()? Something like getattr(magic_namespace_here, "hello") used in the above context? I know it can be done with eval(), but that's generally considered unsafe unless you vet your input thoroughly) I think you're looking for globals()["hello"],or of course magic_namespace=globals() DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Noobie python shell question
On Sun, Nov 29, 2009 at 7:53 PM, Tim Chase wrote: > (as an aside, is there a way to get a local/global variable from a string > like one can fetch a variable from a class/object with getattr()? Something > like getattr(magic_namespace_here, "hello") used in the above context? I > know it can be done with eval(), but that's generally considered unsafe > unless you vet your input thoroughly) locals()["hello"] and globals()["hello"], respectively Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Noobie python shell question
tuxsun wrote: I've been working in the shell on and off all day, and need to see if a function I defined earlier is defined in the current shell I'm working in. Is there a shell command to get of list of functions I've defined? TIA! P.S. If it makes a difference, I'm using the shell from within IDLE, but once in a while I will use the python shell in a Bash console. dir() is the answer to the question you ask. It'll display the entire global context. But usually there's a better way. If you think you previously defined a function def myfunc() just enter myfunc at the prompt (without parentheses). It should tell you it's a function, or undefined. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Noobie python shell question
tuxsun wrote: I've been working in the shell on and off all day, and need to see if a function I defined earlier is defined in the current shell I'm working in. Is there a shell command to get of list of functions I've defined? yesish...you can use dir() from the prompt to see the bound names in a given scope: >>> dir() ['__builtins__', '__doc__', '__name__'] >>> def hello(who='world'): ... print "Hello, %s" % who ... >>> dir() ['__builtins__', '__doc__', '__name__', 'hello'] >>> x = 42 >>> dir() ['__builtins__', '__doc__', '__name__', 'hello', 'x'] however AFAIK, there's no readily accessible way to get the *definition* of that function back (other than scrolling back through your buffer or readline history) and it takes a bit more work to determine whether it's a callable function, or some other data-type. >>> callable(x) False >>> callable(hello) True (as an aside, is there a way to get a local/global variable from a string like one can fetch a variable from a class/object with getattr()? Something like getattr(magic_namespace_here, "hello") used in the above context? I know it can be done with eval(), but that's generally considered unsafe unless you vet your input thoroughly) -tkc -- http://mail.python.org/mailman/listinfo/python-list
Noobie python shell question
I've been working in the shell on and off all day, and need to see if a function I defined earlier is defined in the current shell I'm working in. Is there a shell command to get of list of functions I've defined? TIA! P.S. If it makes a difference, I'm using the shell from within IDLE, but once in a while I will use the python shell in a Bash console. -- http://mail.python.org/mailman/listinfo/python-list