Home print just user's variables in interactive mode
Reply: 1

print just user's variables in interactive mode

ccsann Published in 2018-01-13 10:14:02Z

I am in the interactive mode of the python 3 interpreter.

I have created some variables of different data types. after some time I want to see a list of all variables that I have created. this can be done by using 'dir()'.

but the 'dir()' builtin function will print not just the variables that I have created, but also the special python builtin variables: ['annotations', 'builtins', 'doc', 'loader', 'name', 'package', 'spec']

of course this is and could be very helpful in many cases, but for a beginner it's a little bit irritating.

so, how could I print just and only my variables?

Mindful Reply to 2018-01-13 10:24:48Z

So I'm assuming that you intentionally left out the "__" that comes before and after Python's builtin functions/variables when you listed the extra results you were getting, and not that something else weird is going on. In that case, the easiest way to exclude them is something like this this:

[val for val in dir() if val.strip('__') == val]

Just don't define any variables that start or end with "__"

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299354 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO