How to list all the variables that I defined in Python, except for the imported variables?

I know that there are several ways to list the entire variable, for example locals (), globals (), dir (). But they also list variables imported from other modules, and make a very long list, which is hard to find variables defined by me. So, how should I list all the variables defined by me, better with their values?

Here is an example:

import numpy
a=1
b=2
dir()

Then the result:

['In',
 'Out',
 '_',
 '_1',
 '_2',
 '_3',
 '_4',
 '__',
 '___',
 '__builtin__',
 '__builtins__',
 '__doc__',
 '__name__',
 '__package__',
 '_dh',
 '_i',
 '_i1',
 '_i2',
 '_i3',
 '_i4',
 '_i5',
 '_ih',
 '_ii',
 '_iii',
 '_oh',
 '_sh',
 'a',
 'b',
 'exit',
 'get_ipython',
 'numpy',
 'quit']

But I want to see only the variables defined in this module, i.e. a and b. How should I do it?

+4
source share
1 answer

Save the result locals()at the top of your module after import.

, locals(), .

0

Source: https://habr.com/ru/post/1693009/


All Articles