22
33.. image :: logo.png
44 :target: logo.png
5- :alt: python- varname
5+ :alt: varname
66
77
88`
1717.. image:: https://img.shields.io/pypi/pyversions/python-varname?style=flat-square
1818 :target: https://img.shields.io/pypi/pyversions/python-varname?style=flat-square
1919 :alt: PythonVers
20- <https://pypi.org/project/python-varname/> `_ `
21- .. image:: https://img.shields.io/travis/pwwang/python-varname?style=flat-square
22- :target: https://img.shields.io/travis/pwwang/python-varname?style=flat-square
23- :alt: Travis building
24- <https://travis-ci.org/pwwang/python-varname> `_ `
20+ <https://pypi.org/project/python-varname/> `_
21+ .. image :: https://img.shields.io/github/workflow/status/pwwang/python-varname/Build%20and%20Deploy?style=flat-square
22+ :target: https://img.shields.io/github/workflow/status/pwwang/python-varname/Build%20and%20Deploy?style=flat-square
23+ :alt: Building
24+
25+ `
26+ .. image:: https://img.shields.io/github/workflow/status/pwwang/python-varname/Build%20Docs?label=docs&style=flat-square
27+ :target: https://img.shields.io/github/workflow/status/pwwang/python-varname/Build%20Docs?label=docs&style=flat-square
28+ :alt: Docs and API
29+ <https://pwwang.github.io/python-varname/api/varname> `_ `
2530.. image:: https://img.shields.io/codacy/grade/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
2631 :target: https://img.shields.io/codacy/grade/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
2732 :alt: Codacy
2833 <https://app.codacy.com/manual/pwwang/python-varname/dashboard> `_ `
2934.. image:: https://img.shields.io/codacy/coverage/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
3035 :target: https://img.shields.io/codacy/coverage/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
3136 :alt: Codacy coverage
32- <https://app.codacy.com/manual/pwwang/python-varname/dashboard> `_ `
37+ <https://app.codacy.com/manual/pwwang/python-varname/dashboard> `_
38+ `
3339.. image:: https://img.shields.io/gitter/room/pwwang/python-varname?style=flat-square
3440 :target: https://img.shields.io/gitter/room/pwwang/python-varname?style=flat-square
3541 :alt: Chat on gitter
3642 <https://gitter.im/python-varname/community> `_
3743
3844Dark magics about variable names in python
3945
40- `Change Log <https://pwwang.github.io/python-varname/CHANGELOG/ >`_ | `API <https://pwwang.github.io/python-varname/api/varname/ >`_
46+ `Change Log <https://pwwang.github.io/python-varname/CHANGELOG/ >`_ | `API <https://pwwang.github.io/python-varname/api/varname >`_
4147
4248Installation
4349------------
@@ -100,8 +106,7 @@ Retrieving the variable names from inside a function call/class instantiation
100106 def function ():
101107 return varname()
102108
103- func = function()
104- # func == 'func'
109+ func = function() # func == 'func'
105110
106111 *
107112 ``varname `` calls being buried deeply
@@ -118,62 +123,53 @@ Retrieving the variable names from inside a function call/class instantiation
118123 def function2 ():
119124 return function1()
120125
121- func = function2()
122- # func == 'func'
126+ func = function2() # func == 'func'
123127
124128 *
125129 Retrieving instance name of a class
126130
127131 .. code-block :: python
128132
129- class Klass :
133+ class Foo :
130134 def __init__ (self ):
131135 self .id = varname()
132136
133137 def copy (self ):
134- # also able to fetch inside a member call
135- return varname()
138+ # also able to fetch inside a method call
139+ copied = Foo() # copied.id == 'copied'
140+ copied.id = varname() # assign id to whatever variable name
141+ return copied
136142
137- k = Klass()
138- # k.id == 'k'
143+ k = Foo() # k.id == 'k'
139144
140- k2 = k.copy()
141- # k2 == 'k2'
145+ k2 = k.copy() # k2.id == 'k2'
142146
143147 *
144148 Some unusual use
145149
146150 .. code-block :: python
147151
148- func = [function()]
149- # func == ['func']
152+ func = [function()] # func == ['func']
150153
151- func = [function(), function()]
152- # func == ['func', 'func']
154+ func = [function(), function()] # func == ['func', 'func']
153155
154- func = function(), function()
155- # func = ('func', 'func')
156+ func = function(), function() # func = ('func', 'func')
156157
157- func = func1 = function()
158- # func == func1 == 'func'
158+ func = func1 = function() # func == func1 == 'func'
159159 # a warning will be printed
160160 # since you may not want func1 to be 'func'
161161
162- x = func(y = func())
163- # x == 'x'
162+ x = func(y = func()) # x == 'x'
164163
165164 # get part of the name
166- func_abc = function()[- 3 :]
167- # func_abc == 'abc'
165+ func_abc = function()[- 3 :] # func_abc == 'abc'
168166
169167 # function alias supported now
170168 function2 = function
171- func = function2()
172- # func == 'func'
169+ func = function2() # func == 'func'
173170
174171 a = lambda : 0
175- a.b = function()
176- # a.b == 'b'
172+ a.b = function() # a.b == 'b'
177173
178174 # Since v0.1.3
179175 # We can ask varname to raise exceptions
@@ -213,20 +209,23 @@ Getting variable names directly
213209 from varname import varname, nameof
214210
215211 a = 1
216- aname = nameof(a)
217- # aname == 'a
212+ nameof(a) # 'a'
218213
219214 b = 2
220- aname, bname = nameof(a, b)
221- # aname == 'a', bname == 'b'
215+ nameof(a, b) # ('a', 'b')
222216
223217 def func ():
224218 return varname() + ' _suffix'
225219
226- f = func()
227- # f == 'f_suffix'
228- fname = nameof(f)
229- # fname == 'f'
220+ f = func() # f == 'f_suffix'
221+ nameof(f) # 'f'
222+
223+ # get full names of (chained) attribute calls
224+ func.a = func
225+ nameof(func.a, full = True ) # 'func.a'
226+
227+ func.a.b = 1
228+ nameof(func.a.b, full = True ) # 'func.a.b'
230229
231230 Detecting next immediate attribute name
232231^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -294,12 +293,12 @@ Injecting ``__varname__``
294293 Reliability and limitations
295294---------------------------
296295
297- ``python- varname `` is all depending on ``executing `` package to look for the node.
296+ ``varname `` is all depending on ``executing `` package to look for the node.
298297The node ``executing `` detects is ensured to be the correct one (see `this <https://github.com/alexmojaki/executing#is-it-reliable >`_\ ).
299298
300299It partially works with environments where other AST magics apply, including
301300``pytest ``\ , ``ipython ``\ , ``macropy ``\ , ``birdseye ``\ , ``reticulate `` with ``R ``\ , etc. Neither
302- ``executing `` nor ``python- varname `` is 100% working with those environments. Use
301+ ``executing `` nor ``varname `` is 100% working with those environments. Use
303302it at your own risk.
304303
305304For example:
0 commit comments