Noobie python shell question

John Posner jjposner at optimum.net
Mon Nov 30 18:58:36 CET 2009


On Sun, 29 Nov 2009 22:03:09 -0500, tuxsun <tuxsun1 at gmail.com> 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': <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):
   File "<stdin>", line 1, in <module>
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



More information about the Python-list mailing list