Getting the name of a variable as a string

前端 未结 23 2683
旧时难觅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:48

    With Python 3.8 one can simply use f-string debugging feature:

    >>> foo = dict()
    >>> f'{foo=}'.split('=')[0]
    'foo' 
    

提交回复
热议问题