-4

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?

AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • 1
    A variable doesn't have properties. The value it points to has properties. Variables are merely named placeholders in an algorithm, not physical entities. – deceze Aug 08 '17 at 10:22
  • I'm not quite sure what you're after here. Do you want to find a Python object from its ID? Are you trying to find a list of names which refer to a particular ID? – ymbirtt Aug 08 '17 at 10:22
  • "what is the nature of variables or the names or the references?" - what kind of answer are you expecting to this question? – Craicerjack Aug 08 '17 at 10:22
  • What "properties" are you talking about here? Give some examples what you expect to get. – tobias_k Aug 08 '17 at 10:22
  • 1
    I'd suggest you read https://nedbatchelder.com/text/names.html; an identifier isn't a thing in itself, merely a "tag" on a value. – jonrsharpe Aug 08 '17 at 10:23
  • name of the variable on left hand of assignment. @Craicerjack – AbstProcDo Aug 08 '17 at 10:27
  • You already know the name of the `foo` variable. It's typed right there in the code. You don't need to _get_ it. What are you _actually_ trying to do? – khelwood Aug 08 '17 at 10:33

3 Answers3

3

Python’s variables are essentially just names for objects. These names are just labels for objects, so the only “property” of a name would be which object it refers to, which is exactly what you get by using a name.

That being said, names to belong to something: A scope. Names are valid within the scope they are defined in, so if you can access that scope, it is possible to see the name. You can use locals() to access the current local scope:

>>> foo = 1
>>> locals()
{'foo': 1, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__name__': '__main__', '__doc__': None, '__builtins__': <module 'builtins' (built-in)>, '__package__': None, '__spec__': None}

As you can see, there is the name foo in that scope, with the value it is referring to. But that’s really all there is. Apart from the name, the object they reference, and the scope they exist in, Python variables do not have any more properties. And they are not “objects” in the sense of Python objects, so they are an exception from the simplified statement “everything is an object”.

For more information on names, see “Facts and myths about Python names and values” by Ned Batchelder.

poke
  • 369,085
  • 72
  • 557
  • 602
1

id shows value's id and tell nothing about variable itself? Use which function can get its properties?

Object ids are opaque: there is no (documented, reliable) way to go from an id to the object's properties. The primary use case for id() is to check whether two names refer to the same object.

The fact that id(foo) happens to be the same as id(1) is an implementation detail. It does not hold in general:

>>> foo = 1234
>>> id(foo)
140491179040280
>>> id(1234)
140491179040808

(Note how the two numbers are different.)

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

I will answer you with 2 posts: Yes, in python everything is an object

and id are like memory adresses in C. If you want only get your properties declared in the object, for me, the best choice is using sets:

class A():
    a = 2 

set(dir(A())) - set(dir(object))
#set(['a', '__module__'])
mandrewcito
  • 170
  • 8