Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all Python built-in private variables such as __file__, __name__

I want to know all Python built-in private variables such as __file__, __name__, and their purpose.

but I don't see the document of all Python built-in private variables in www.python.org.

I have know dir and vars.

So, how to find them?

like image 340
Aleeee Avatar asked Jul 12 '14 03:07

Aleeee


People also ask

What is the __ name __ variable in Python?

The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script. Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.

How do you access a private variable in Python?

However, inside the class (myClass) we can access the private variables. In the hello() method, the __privateVar variable can be accessed (as shown above: “Private Variable value: 27”). So from the above example, we can understand that all the variables and the methods inside the class are public by the method.

What is __ module __ in Python?

The __module__ property is intended for retrieving the module where the function was defined, either to read the source code or sometimes to re-import it in a script.

Are there private variables in Python?

In Python, there is no existence of “Private” instance variables that cannot be accessed except inside an object.


2 Answers

What do you say about:

>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'f']
>>> [ i for i in dir() if i.startswith("__") and i.endswith("__")]
['__builtins__', '__doc__', '__name__', '__package__']

You can define an auxiliary function:

>>> def getprivates(obj):
        return [i for i in dir(obj) if i.startswith("__") and i.endswith("__")]

and apply to any object reference even to dir() itself:

>>> getprivates(dir())
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__']
like image 183
0x90 Avatar answered Nov 14 '22 23:11

0x90


The hidden attributes are sometimes referred to as magic methods (for objects) and for reference, I would check out the Python docs on the data model, which are fairly comprehensive and likely cover all of the attributes you're looking to find.

After you've learned the hidden attributes, you may know what you want to get, but hidden attributes may vary by implementation, so to abstract that away, use the inspect module:

import inspect

To get a lot of information:

inspect.getmembers(inspect) 

To get the file and a bit more information on a module:

>>> inspect.getfile(inspect)
'/usr/lib/python2.7/inspect.pyc'
>>> inspect.getmoduleinfo(inspect.getfile(inspect))
ModuleInfo(name='inspect', suffix='.pyc', mode='rb', module_type=2)
like image 27
Russia Must Remove Putin Avatar answered Nov 14 '22 21:11

Russia Must Remove Putin