2
2
3
3
.. image :: logo.png
4
4
:target: logo.png
5
- :alt: python- varname
5
+ :alt: varname
6
6
7
7
8
8
`
17
17
.. image:: https://img.shields.io/pypi/pyversions/python-varname?style=flat-square
18
18
:target: https://img.shields.io/pypi/pyversions/python-varname?style=flat-square
19
19
: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> `_ `
25
30
.. image:: https://img.shields.io/codacy/grade/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
26
31
:target: https://img.shields.io/codacy/grade/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
27
32
:alt: Codacy
28
33
<https://app.codacy.com/manual/pwwang/python-varname/dashboard> `_ `
29
34
.. image:: https://img.shields.io/codacy/coverage/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
30
35
:target: https://img.shields.io/codacy/coverage/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
31
36
:alt: Codacy coverage
32
- <https://app.codacy.com/manual/pwwang/python-varname/dashboard> `_ `
37
+ <https://app.codacy.com/manual/pwwang/python-varname/dashboard> `_
38
+ `
33
39
.. image:: https://img.shields.io/gitter/room/pwwang/python-varname?style=flat-square
34
40
:target: https://img.shields.io/gitter/room/pwwang/python-varname?style=flat-square
35
41
:alt: Chat on gitter
36
42
<https://gitter.im/python-varname/community> `_
37
43
38
44
Dark magics about variable names in python
39
45
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 >`_
41
47
42
48
Installation
43
49
------------
@@ -100,8 +106,7 @@ Retrieving the variable names from inside a function call/class instantiation
100
106
def function ():
101
107
return varname()
102
108
103
- func = function()
104
- # func == 'func'
109
+ func = function() # func == 'func'
105
110
106
111
*
107
112
``varname `` calls being buried deeply
@@ -118,62 +123,53 @@ Retrieving the variable names from inside a function call/class instantiation
118
123
def function2 ():
119
124
return function1()
120
125
121
- func = function2()
122
- # func == 'func'
126
+ func = function2() # func == 'func'
123
127
124
128
*
125
129
Retrieving instance name of a class
126
130
127
131
.. code-block :: python
128
132
129
- class Klass :
133
+ class Foo :
130
134
def __init__ (self ):
131
135
self .id = varname()
132
136
133
137
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
136
142
137
- k = Klass()
138
- # k.id == 'k'
143
+ k = Foo() # k.id == 'k'
139
144
140
- k2 = k.copy()
141
- # k2 == 'k2'
145
+ k2 = k.copy() # k2.id == 'k2'
142
146
143
147
*
144
148
Some unusual use
145
149
146
150
.. code-block :: python
147
151
148
- func = [function()]
149
- # func == ['func']
152
+ func = [function()] # func == ['func']
150
153
151
- func = [function(), function()]
152
- # func == ['func', 'func']
154
+ func = [function(), function()] # func == ['func', 'func']
153
155
154
- func = function(), function()
155
- # func = ('func', 'func')
156
+ func = function(), function() # func = ('func', 'func')
156
157
157
- func = func1 = function()
158
- # func == func1 == 'func'
158
+ func = func1 = function() # func == func1 == 'func'
159
159
# a warning will be printed
160
160
# since you may not want func1 to be 'func'
161
161
162
- x = func(y = func())
163
- # x == 'x'
162
+ x = func(y = func()) # x == 'x'
164
163
165
164
# get part of the name
166
- func_abc = function()[- 3 :]
167
- # func_abc == 'abc'
165
+ func_abc = function()[- 3 :] # func_abc == 'abc'
168
166
169
167
# function alias supported now
170
168
function2 = function
171
- func = function2()
172
- # func == 'func'
169
+ func = function2() # func == 'func'
173
170
174
171
a = lambda : 0
175
- a.b = function()
176
- # a.b == 'b'
172
+ a.b = function() # a.b == 'b'
177
173
178
174
# Since v0.1.3
179
175
# We can ask varname to raise exceptions
@@ -213,20 +209,23 @@ Getting variable names directly
213
209
from varname import varname, nameof
214
210
215
211
a = 1
216
- aname = nameof(a)
217
- # aname == 'a
212
+ nameof(a) # 'a'
218
213
219
214
b = 2
220
- aname, bname = nameof(a, b)
221
- # aname == 'a', bname == 'b'
215
+ nameof(a, b) # ('a', 'b')
222
216
223
217
def func ():
224
218
return varname() + ' _suffix'
225
219
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'
230
229
231
230
Detecting next immediate attribute name
232
231
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -294,12 +293,12 @@ Injecting ``__varname__``
294
293
Reliability and limitations
295
294
---------------------------
296
295
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.
298
297
The node ``executing `` detects is ensured to be the correct one (see `this <https://github.com/alexmojaki/executing#is-it-reliable >`_\ ).
299
298
300
299
It partially works with environments where other AST magics apply, including
301
300
``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
303
302
it at your own risk.
304
303
305
304
For example:
0 commit comments