In Python, everything is an object. What is the nature of variables? Explore it in an example:
>>> foo = 1
>>> dir(foo)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', ...]
>>> dir(1)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', ...]
Here, both calls to dir()
list the value’s (the int object for 1
) properties and methods.
>>> id(foo)
4376812816
>>> id(1)
4376812816
And the calls to id()
show that the object is the same in both cases.
There does not appear to be a way to get information about the variable itself, instead of the object it references. Is there anything in Python to get information about the variable foo
instead of the object it references?