@@ -20,8 +20,7 @@ pip install -U varname
2020 - Retrieving names of variables a function/class call is assigned to from inside it, using ` varname ` .
2121 - Retrieving variable names directly, using ` nameof `
2222 - 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 `
2524
2625- Other helper APIs (built based on core features):
2726
@@ -144,7 +143,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
144143 def func ():
145144 return varname(multi_vars = True )
146145
147- a = func() # a == ('a', )
146+ a = func() # a == ('a',)
148147 a, b = func() # (a, b) == ('a', 'b')
149148 [a, b] = func() # (a, b) == ('a', 'b')
150149
@@ -155,14 +154,15 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
155154- Some unusual use
156155
157156 ```python
158- def function ():
159- return varname()
157+ def function (** kwargs ):
158+ return varname(strict = False )
160159
161- func = func1 = function() # func == func1 == 'func'
160+ func = func1 = function() # func == func1 == 'func1'
161+ # if varname < 0.8: func == func1 == 'func'
162162 # 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 '
164164
165- x = func (y = func ()) # x == 'x'
165+ x = function (y = function ()) # x == 'x'
166166
167167 # get part of the name
168168 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
173173
174174 a = lambda : 0
175175 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
186176 ```
187177
188178# ## The decorator way to register `__varname__` to functions/classes
@@ -272,29 +262,33 @@ awesome.permit() # AttributeError: Should do something with AwesomeClass object
272262awesome.permit().do() == ' I am doing!'
273263```
274264
275- ### Fetching argument names/sources using ` argname2 `
265+ ### Fetching argument names/sources using ` argname `
276266``` python
277- from varname import argname2
267+ from varname import argname
278268
279269def func (a , b = 1 ):
280- print (argname2 (' a' ))
270+ print (argname (' a' ))
281271
282272x = y = z = 2
283273func(x) # prints: x
284274
275+
285276def func2 (a , b = 1 ):
286- print (argname2 (' a' , ' b' ))
277+ print (argname (' a' , ' b' ))
287278func2(y, b = x) # prints: ('y', 'x')
288279
280+
289281# allow expressions
290282def func3 (a , b = 1 ):
291- print (argname2 (' a' , ' b' , vars_only = False ))
283+ print (argname (' a' , ' b' , vars_only = False ))
292284func3(x+ y, y+ x) # prints: ('x+y', 'y+x')
293285
286+
294287# positional and keyword arguments
295288def func4 (* args , ** kwargs ):
296- print (argname2 (' args[1]' , ' kwargs["c" ]' ))
289+ print (argname (' args[1]' , ' kwargs[c ]' ))
297290func4(y, x, c = z) # prints: ('x', 'z')
291+
298292```
299293
300294### Value wrapper
@@ -321,19 +315,22 @@ mydict = values_to_dict(foo, bar)
321315from varname.helpers import debug
322316
323317a = ' 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"
327323debug(a, b)
328- # DEBUG: a='value'
329- # DEBUG: b=<object object at 0x2b70580e5f20>
324+ # "DEBUG: a='value'\nDEBUG: b=['val']\n"
330325debug(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'
333329# also debug an expression
334- debug(a+ a) # DEBUG: a+a='valuevalue'
330+ debug(a+ a)
331+ # "DEBUG: a+a='valuevalue'\n"
335332# If you want to disable it:
336- debug(a+ a, vars_only = True ) # error
333+ debug(a+ a, vars_only = True ) # ImproperUseError
337334```
338335
339336## Reliability and limitations
0 commit comments