4

In python, you can type dir() to get a list of the items in the global namespace, but the list is ugly, and it includes both public and private objects. I want to write a function (say pretty) that pretties it up. My function will live inside a module.

When I call pretty(dir()) I wind up pretty printing the objects in the module. Same for pretty(sorted(globals())). How can I get the list of the names of the objects in the global namespace (from within a module), so I can do my beautification?

I'm working in python3, if that matters.

EDIT: The first two answers that came back had to do with my controlling code in the module containing pretty. That's not what I want to do. I'm aiming at an experience like the following.

>>> foo = create_something()
>>> bar = create_something_else()
>>> create_lots_more_stuff()
>>> # Hmmmm I'd like to see a list of all the things I have created in my interactive python session.
>>> pretty(dir())
foo     bar
baz     qux
etc     etc_etc
1
  • 1
    globals() gives you the global namespace, which is the objects in the module Commented Sep 23, 2019 at 21:46

1 Answer 1

4

Rather than write a function to filter the dir() results, use the attributes __all__ and/or __dir__ to let your module publicly export the namespace explicitly, as described in PEP 0562.

# lib.py
deprecated_names = ["old_function", ...]
__all__ = ["new_function_one", "new_function_two", ...]

def new_function_one(arg, other):
   ...

def new_function_two(arg, other):
    ...

def __dir__():
    return sorted(__all__ + deprecated_names)

Usage:

# main.py
import lib

dir(lib)  # prints ["new_function_one", "new_function_two", "old_function", ...]

Note: The module level __dir__ hook requires Python 3.7+.


Edit: Perhaps you will be interested in the who and whos magic commands provided in an IPython interactive session. Demo shown below.

>>> who
Interactive namespace is empty.
>>> x = 123
>>> def foo():
...     pass
... 
... 
>>> who
foo  x   
>>> whos
Variable   Type        Data/Info
--------------------------------
foo        function    <function foo at 0x109b59158>
x          int         123
Sign up to request clarification or add additional context in comments.

7 Comments

Unless I'm missing something, this allows me to tell the rest of the world what's in my module. I want my module to see what's in the rest of the world.
How is that any different from looking in locals() or globals()?
@wim the OP seems to be confused about the nature of the global namespace.
Yes, the OP is confused, and he appreciates your help alleviating his confusion. One thing that might still be unclear is that the call to 'globals()' will be coming from inside the module, so IIUC it won't return the "globals" from the global namespace, but from the namespace of the module.
What difference is it? Usually when people talk about a global namespace, they are talking about the outermost namespace of a module. If you want any more help you'll have to clearly distinguish which global namespace you are referring to and how it differs from the module namespace.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.