Noobie python shell question
Tim Chase
python.list at tim.thechases.com
Sun Nov 29 22:53:30 EST 2009
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
More information about the Python-list
mailing list