Getting the name of a variable as a string

前端 未结 23 2706
旧时难觅i
旧时难觅i 2020-11-22 00:19

This thread discusses how to get the name of a function as a string in Python: How to get a function name as a string?

How can I do the same for a variable? As oppose

23条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 00:52

    I don't believe this is possible. Consider the following example:

    >>> a = []
    >>> b = a
    >>> id(a)
    140031712435664
    >>> id(b)
    140031712435664
    

    The a and b point to the same object, but the object can't know what variables point to it.

提交回复
热议问题