@@ -21,7 +21,6 @@ Note if you use `python < 3.8`, install `varname < 0.11`
21
21
- Core features:
22
22
23
23
- Retrieving names of variables a function/class call is assigned to from inside it, using ` varname ` .
24
- - Retrieving variable names directly, using ` nameof `
25
24
- Detecting next immediate attribute name, using ` will `
26
25
- Fetching argument names/sources passed to a function using ` argname `
27
26
@@ -245,31 +244,6 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
245
244
# foo.__varname__ == 'foo'
246
245
```
247
246
248
- # ## Getting variable names directly using `nameof`
249
-
250
- ```python
251
- from varname import varname, nameof
252
-
253
- a = 1
254
- nameof(a) # 'a'
255
-
256
- b = 2
257
- nameof(a, b) # ('a', 'b')
258
-
259
- def func ():
260
- return varname() + ' _suffix'
261
-
262
- f = func() # f == 'f_suffix'
263
- nameof(f) # 'f'
264
-
265
- # get full names of (chained) attribute calls
266
- func.a = func
267
- nameof(func.a, vars_only = False ) # 'func.a'
268
-
269
- func.a.b = 1
270
- nameof(func.a.b, vars_only = False ) # 'func.a.b'
271
- ```
272
-
273
247
# ## Detecting next immediate attribute name
274
248
275
249
```python
@@ -415,22 +389,27 @@ obj.argnames # ['1', '2']
415
389
` varname ` is all depending on ` executing ` package to look for the node.
416
390
The node ` executing ` detects is ensured to be the correct one (see [ this] [ 19 ] ).
417
391
418
- It partially works with environments where other AST magics apply, including
419
- ` pytest ` , ` ipython ` , ` macropy ` , ` birdseye ` , ` reticulate ` with ` R ` , etc. Neither
392
+ It partially works with environments where other AST magics apply, including [ ` exec ` ] [ 24 ] function,
393
+ [ ` macropy ` ] [ 21 ] , [ ` birdseye ` ] [ 22 ] , [ ` reticulate ` ] [ 23 ] with ` R ` , etc. Neither
420
394
` executing ` nor ` varname ` is 100% working with those environments. Use
421
395
it at your own risk.
422
396
423
397
For example:
424
398
425
- - This will not work with ` pytest ` :
399
+ - This will not work:
426
400
427
401
``` python
402
+ from varname import argname
403
+
404
+ def getname (x ):
405
+ print (argname(" x" ))
406
+
428
407
a = 1
429
- assert nameof(a) == ' a ' # pytest manipulated the ast here
408
+ exec ( " getname(a) " ) # Cannot retrieve the node where the function is called.
430
409
431
- # do this instead
432
- name_a = nameof(a)
433
- assert name_a == ' a '
410
+ # # instead
411
+ # from varname.helpers import exec_code
412
+ # exec_code("getname(a)")
434
413
```
435
414
436
415
[ 1 ] : https://github.com/pwwang/python-varname
@@ -452,3 +431,7 @@ For example:
452
431
[ 17 ] : https://img.shields.io/pypi/dm/varname?style=flat-square
453
432
[ 19 ] : https://github.com/alexmojaki/executing#is-it-reliable
454
433
[ 20 ] : https://stackoverflow.com/a/59364138/5088165
434
+ [ 21 ] : https://github.com/lihaoyi/macropy
435
+ [ 22 ] : https://github.com/alexmojaki/birdseye
436
+ [ 23 ] : https://rstudio.github.io/reticulate/
437
+ [ 24 ] : https://docs.python.org/3/library/functions.html#exec
0 commit comments