11
2- python-varname
3- ==============
2+
3+ .. image :: logo.png
4+ :target: logo.png
5+ :alt: python-varname
6+
47
58`
69.. image:: https://img.shields.io/pypi/v/python-varname?style=flat-square
@@ -37,146 +40,121 @@ Installation
3740
3841 pip install python-varname
3942
40- Usage
41- -----
42-
43- Retrieving the variable name inside a function
44- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
43+ Features
44+ --------
4545
46- .. code-block :: python
4746
48- from varname import varname
49- def function ():
50- return varname()
47+ * Fetching variable names from inside the function/class call
48+ * Fetching variable names directly (added in ``v0.1.2 ``\ )
49+ * A value wrapper to store the variable name that a value is assigned to (added in ``v0.1.1 ``\ )
50+ * Detecting next immediate attribute name (added in ``v0.1.4 ``\ )
51+ * Shortcut for ``collections.namedtuple `` (added in ``v0.1.6 ``\ )
52+ * Injecting ``__varname__ `` to objects (added in ``v0.1.7 ``\ )
5153
52- func = function()
53- # func == 'func'
54+ Usage
55+ -----
5456
55- # available calls to retrieve
56- func = function(
57- # ...
58- )
57+ Retrieving the variable names from inside a function call/class instantiation
58+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5959
60- func = \
61- function()
6260
63- func = function \
64- ()
61+ *
62+ From insdie a function call
6563
66- func = (function
67- ())
64+ .. code-block :: python
6865
69- ``varname `` calls being buried deeply
70- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66+ from varname import varname
67+ def function ():
68+ return varname()
7169
72- .. code-block :: python
70+ func = function()
71+ # func == 'func'
7372
74- def function ():
75- # I know that at which stack this will be called
76- return varname(caller = 3 )
73+ *
74+ ``varname `` calls being buried deeply
7775
78- def function1 ():
79- return function()
76+ .. code-block :: python
8077
81- def function2 ():
82- return function1()
78+ def function ():
79+ # I know that at which stack this will be called
80+ return varname(caller = 3 )
8381
84- func = function2()
85- # func == 'func'
82+ def function1 ():
83+ return function()
8684
87- Retrieving instance name of a class object
88- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85+ def function2 ():
86+ return function1()
8987
90- .. code-block :: python
88+ func = function2()
89+ # func == 'func'
9190
92- class Klass :
93- def __init__ (self ):
94- self .id = varname()
95- def copy (self ):
96- return varname()
91+ *
92+ Retrieving instance name of a class
9793
98- k = Klass()
99- # k.id == 'k'
94+ .. code-block :: python
10095
101- k2 = k.copy()
102- # k2 == 'k2'
96+ class Klass :
97+ def __init__ (self ):
98+ self .id = varname()
10399
104- ``varname `` calls being buried deeply for classes
105- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100+ def copy (self ):
101+ # also able to fetch inside a member call
102+ return varname()
106103
107- .. code-block :: python
104+ k = Klass()
105+ # k.id == 'k'
108106
109- class Klass :
110- def __init__ (self ):
111- self .id = self .some_internal()
107+ k2 = k.copy()
108+ # k2 == 'k2'
112109
113- def some_internal ( self ):
114- return varname( caller = 2 )
110+ *
111+ Some unusual use
115112
116- def copy (self ):
117- return self .copy_id()
113+ .. code-block :: python
118114
119- def copy_id ( self ):
120- return self .copy_id_internal()
115+ func = [function()]
116+ # func == ['func']
121117
122- def copy_id_internal ( self ):
123- return varname( caller = 3 )
118+ func = [function(), function()]
119+ # func == ['func', 'func']
124120
125- k = Klass ()
126- # k.id == 'k'
121+ func = function(), function ()
122+ # func = ('func', 'func')
127123
128- k2 = k.copy()
129- # k2 == 'k2'
124+ func = func1 = function()
125+ # func == func1 == 'func'
126+ # a warning will be printed
127+ # since you may not want func1 to be 'func'
130128
131- Some unusual use
132- ----------------
129+ x = func( y = func())
130+ # x == 'x'
133131
134- .. code-block :: python
132+ # get part of the name
133+ func_abc = function()[- 3 :]
134+ # func_abc == 'abc'
135135
136- func = [function()]
137- # func == ['func']
136+ # function alias supported now
137+ function2 = function
138+ func = function2()
139+ # func == 'func'
138140
139- func = [function(), function()]
140- # func == ['func', 'func']
141+ # Since v0.1.3
142+ # We can ask varname to raise exceptions
143+ # if it fails to detect the variable name
141144
142- func = function(), function()
143- # func = ('func', 'func')
145+ from varname import VarnameRetrievingError
146+ def get_name ():
147+ try :
148+ # if raise_exc is False
149+ # "var_<index>" will be returned
150+ return varname(raise_exc = True )
151+ except VarnameRetrieveingError:
152+ return None
144153
145- func = func1 = function()
146- # func == func1 == 'func'
147- # a warning will be printed
154+ a.b = get_name() # None
148155
149- x = func(
150- y = func()
151- )
152- # x == 'x'
153-
154- # get part of the name
155- func_abc = function()[- 3 :]
156- # func_abc == 'abc'
157-
158- # function alias supported now
159- function2 = function
160- func = function2()
161- # func == 'func'
162-
163- # Since v0.1.3
164- # We can ask varname to raise exceptions
165- # if it fails to detect the variable name
166-
167- from varname import VarnameRetrievingError
168- def get_name ():
169- try :
170- # if raise_exc is False
171- # "var_<index>" will be returned
172- return varname(raise_exc = True )
173- except VarnameRetrieveingError:
174- return None
175-
176- a.b = get_name() # None
177-
178- A value wrapper (added in v0.1.1)
179- ---------------------------------
156+ Value wrapper
157+ ^^^^^^^^^^^^^
180158
181159.. code-block :: python
182160
@@ -195,8 +173,8 @@ A value wrapper (added in v0.1.1)
195173 mydict = values_to_dict(foo, bar)
196174 # {'foo': True, 'bar': False}
197175
198- Getting variable names directly (added in v0.1.2)
199- -------------------------------------------------
176+ Getting variable names directly
177+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
200178
201179.. code-block :: python
202180
@@ -218,8 +196,8 @@ Getting variable names directly (added in v0.1.2)
218196 fname = nameof(f)
219197 # fname == 'f'
220198
221- Detecting next immediate attribute name (added in `` v0.1.4 `` \ )
222- -----------------------------------------------------------------
199+ Detecting next immediate attribute name
200+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
223201
224202.. code-block :: python
225203
@@ -241,12 +219,12 @@ Detecting next immediate attribute name (added in ``v0.1.4``\ )
241219 return ' I am doing!'
242220
243221 awesome = AwesomeClass()
244- awesome.do() # AttributeError: Should do something with AwesomeClass object
245- awesome.permit() # AttributeError: You don't have permission to do
222+ awesome.do() # AttributeError: You don't have permission to do
223+ awesome.permit() # AttributeError: Should do something with AwesomeClass object
246224 awesome.permit().do() == ' I am doing!'
247225
248- Shortcut for ``collections.namedtuple `` (addedin `` v0.1.6 `` \ )
249- --------------------------------------------------------------------
226+ Shortcut for ``collections.namedtuple ``
227+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
250228
251229.. code-block :: python
252230
@@ -258,13 +236,36 @@ Shortcut for ``collections.namedtuple`` (addedin ``v0.1.6``\ )
258236 from varname import namedtuple
259237 Name = namedtuple([' first' , ' last' ])
260238
239+ Injecting ``__varname__ ``
240+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
241+
242+ .. code-block :: python
243+
244+ from varname import inject
245+
246+ class MyList (list ):
247+ pass
248+
249+ a = inject(MyList())
250+ b = inject(MyList())
251+
252+ a.__varname__ == ' a'
253+ b.__varname__ == ' b'
254+
255+ a == b
256+
257+ # other methods not affected
258+ a.append(1 )
259+ b.append(1 )
260+ a == b
261+
261262 Limitations
262263-----------
263264
264265
265266* Working in ``ipython REPL `` but not in standard ``python console ``
266267* You have to know at which stack the function/class will be called
267- * For performance, since inspection is involved, better cache the name
268+ * Not working with `` reticulate `` from `` R `` since it cuts stacks to the most recent one.
268269* ``nameof `` cannot be used in statements in ``pytest ``
269270 .. code-block ::
270271
0 commit comments