@@ -20,8 +20,7 @@ pip install -U varname
20
20
- Retrieving names of variables a function/class call is assigned to from inside it, using ` varname ` .
21
21
- Retrieving variable names directly, using ` nameof `
22
22
- Detecting next immediate attribute name, using ` will `
23
- - Fetching argument names/sources passed to a function using ` argname2 `
24
- (` argname ` is superseded by ` argname2 ` )
23
+ - Fetching argument names/sources passed to a function using ` argname `
25
24
26
25
- Other helper APIs (built based on core features):
27
26
@@ -144,7 +143,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
144
143
def func ():
145
144
return varname(multi_vars = True )
146
145
147
- a = func() # a == ('a', )
146
+ a = func() # a == ('a',)
148
147
a, b = func() # (a, b) == ('a', 'b')
149
148
[a, b] = func() # (a, b) == ('a', 'b')
150
149
@@ -155,14 +154,15 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
155
154
- Some unusual use
156
155
157
156
```python
158
- def function ():
159
- return varname()
157
+ def function (** kwargs ):
158
+ return varname(strict = False )
160
159
161
- func = func1 = function() # func == func1 == 'func'
160
+ func = func1 = function() # func == func1 == 'func1'
161
+ # if varname < 0.8: func == func1 == 'func'
162
162
# a warning will be shown
163
- # since you may not want func1 to be 'func '
163
+ # since you may not want func to be 'func1 '
164
164
165
- x = func (y = func ()) # x == 'x'
165
+ x = function (y = function ()) # x == 'x'
166
166
167
167
# get part of the name
168
168
func_abc = function()[- 3 :] # func_abc == 'abc'
@@ -173,16 +173,6 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
173
173
174
174
a = lambda : 0
175
175
a.b = function() # a.b == 'b'
176
-
177
- # Since v0.1.3
178
- # We can ask varname to raise exceptions
179
- # if it fails to detect the variable name
180
- def get_name (raise_exc ):
181
- return varname(raise_exc = raise_exc)
182
-
183
- a = {}
184
- a[' b' ] = get_name(True ) # VarnameRetrievingError
185
- a[' b' ] = get_name(False ) # None
186
176
```
187
177
188
178
# ## The decorator way to register `__varname__` to functions/classes
@@ -272,29 +262,33 @@ awesome.permit() # AttributeError: Should do something with AwesomeClass object
272
262
awesome.permit().do() == ' I am doing!'
273
263
```
274
264
275
- ### Fetching argument names/sources using ` argname2 `
265
+ ### Fetching argument names/sources using ` argname `
276
266
``` python
277
- from varname import argname2
267
+ from varname import argname
278
268
279
269
def func (a , b = 1 ):
280
- print (argname2 (' a' ))
270
+ print (argname (' a' ))
281
271
282
272
x = y = z = 2
283
273
func(x) # prints: x
284
274
275
+
285
276
def func2 (a , b = 1 ):
286
- print (argname2 (' a' , ' b' ))
277
+ print (argname (' a' , ' b' ))
287
278
func2(y, b = x) # prints: ('y', 'x')
288
279
280
+
289
281
# allow expressions
290
282
def func3 (a , b = 1 ):
291
- print (argname2 (' a' , ' b' , vars_only = False ))
283
+ print (argname (' a' , ' b' , vars_only = False ))
292
284
func3(x+ y, y+ x) # prints: ('x+y', 'y+x')
293
285
286
+
294
287
# positional and keyword arguments
295
288
def func4 (* args , ** kwargs ):
296
- print (argname2 (' args[1]' , ' kwargs["c" ]' ))
289
+ print (argname (' args[1]' , ' kwargs[c ]' ))
297
290
func4(y, x, c = z) # prints: ('x', 'z')
291
+
298
292
```
299
293
300
294
### Value wrapper
@@ -321,19 +315,22 @@ mydict = values_to_dict(foo, bar)
321
315
from varname.helpers import debug
322
316
323
317
a = ' value'
324
- b = object ()
325
- debug(a) # DEBUG: a='value'
326
- debug(b) # DEBUG: b=<object object at 0x2b70580e5f20>
318
+ b = [' val' ]
319
+ debug(a)
320
+ # "DEBUG: a='value'\n"
321
+ debug(b)
322
+ # "DEBUG: b=['val']\n"
327
323
debug(a, b)
328
- # DEBUG: a='value'
329
- # DEBUG: b=<object object at 0x2b70580e5f20>
324
+ # "DEBUG: a='value'\nDEBUG: b=['val']\n"
330
325
debug(a, b, merge = True )
331
- # DEBUG: a='value', b=<object object at 0x2b70580e5f20>
332
- debug(a, repr = False , prefix = ' ' ) # a=value
326
+ # "DEBUG: a='value', b=['val']\n"
327
+ debug(a, repr = False , prefix = ' ' )
328
+ # 'a=value\n'
333
329
# also debug an expression
334
- debug(a+ a) # DEBUG: a+a='valuevalue'
330
+ debug(a+ a)
331
+ # "DEBUG: a+a='valuevalue'\n"
335
332
# If you want to disable it:
336
- debug(a+ a, vars_only = True ) # error
333
+ debug(a+ a, vars_only = True ) # ImproperUseError
337
334
```
338
335
339
336
## Reliability and limitations
0 commit comments