Noobie python shell question

T

tuxsun

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.
 
T

Tim Chase

tuxsun said:
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.
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
 
D

Dave Angel

tuxsun said:
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
 
C

Chris Rebert

On Sun, Nov 29, 2009 at 7:53 PM, Tim Chase
(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
 
D

Dave Angel

Tim said:
<snip>

(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
 
J

John Posner

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 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.
....
....
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b', 'c',
'eggs', 'spam']
{'a': 1, 'c': 3, 'b': 2, 'spam': <function spam at 0x00BC73B0>,
'__builtins__': <module
'__builtin__' (built-in)>, 'eggs': <function eggs at 0x00BC72B0>,
'__package__': None,
'__name__': '__main__', '__doc__': None}
[name for name in locals() if callable(locals()[name])]
Traceback (most recent call last):
[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
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top