10 examples of 'python get current function name' in Python

Every line of 'python get current function name' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
25def current_function(frame):
26 """
27 Get reference to currently running function from inspect/trace stack frame.
28
29 Parameters
30 ----------
31 frame : stack frame
32 Stack frame obtained via trace or inspect
33
34 Returns
35 -------
36 fnc : function reference
37 Currently running function
38 """
39
40 if frame is None:
41 return None
42
43 code = frame.f_code
44 # Attempting to extract the function reference for these calls appears
45 # to be problematic
46 if code.co_name == '__del__' or code.co_name == '_remove' or \
47 code.co_name == '_removeHandlerRef':
48 return None
49
50 try:
51 # Solution follows suggestion at http://stackoverflow.com/a/37099372
52 lst = [referer for referer in gc.get_referrers(code)
53 if getattr(referer, "__code__", None) is code and
54 inspect.getclosurevars(referer).nonlocals.items() <=
55 frame.f_locals.items()]
56 if lst:
57 return lst[0]
58 else:
59 return None
60 except ValueError:
61 # inspect.getclosurevars can fail with ValueError: Cell is empty
62 return None
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

115def func_name(
116 level: Optional[int] = 1,
117 parent: Optional[Callable] = None) -> str:
118 """ Return the name of the function that is calling this function. """
119 frame = inspect.currentframe()
120 # Go back a number of frames (usually 1).
121 backlevel = level or 1
122 while backlevel > 0:
123 frame = frame.f_back
124 backlevel -= 1
125 if parent:
126 func = '{}.{}'.format(parent.__class__.__name__, frame.f_code.co_name)
127 return func
128
129 return frame.f_code.co_name
25def _caller_module_name():
26 # Move up the stack twice: we want the module name of the
27 # function/module that called the function calling
28 # _caller_module_name
29 caller_module = inspect.getmodule(inspect.currentframe().f_back.f_back)
30 if caller_module is None:
31 return "<stdin>"
32 return caller_module.__name__
135def _get_function(self, name=None):
136 if name is None:
137 name = self._func_combo.get_function()
138
139 if self._ins is not None and hasattr(self._ins, name):
140 return getattr(self._ins, name)
141 else:
142 return None
191def getFunctionName(inDecorator=False):
192 try:
193 if inDecorator:
194 return sys._getframe(2).f_code.co_name
195 return sys._getframe(1).f_code.co_name
196 except Exception:
197 return None
84def get_script_name():
85 """Return the name of the top-level script."""
86 return os.path.splitext(scriptinfo()["name"])[0]
185def get_func_name(func):
186 """Return function name"""
187 if PY2:
188 # Python 2
189 return func.func_name
190 else:
191 # Python 3
192 return func.__name__
199def current_temporary_name(self):
200 """Return the name of the current temporary variable.
201
202 This must be in sync with the one during symbol table building.
203 """
204 name = "_[%d]" % (self.temporary_name_counter,)
205 self.temporary_name_counter += 1
206 assert self.scope.lookup(name) != symtable.SCOPE_UNKNOWN
207 return name
154def get_current_method_and_class(current_line_index, current_buffer):
155 class_regex, class_name = re.compile(r"^class (?P<class_name>.+)\("), False
156 method_regex, method_name = re.compile(r"def (?P<method_name>.+)\("), False
157 for line in range(current_line_index - 1, -1, -1):
158 if class_regex.search(current_buffer[line]) is not None and not class_name:
159 class_name = class_regex.search(current_buffer[line])
160 class_name = class_name.group(1)
161 if method_regex.search(current_buffer[line]) is not None and not method_name and not class_name:
162 method_name = method_regex.search(current_buffer[line])
163 method_name = method_name.group(1)
164 return (class_name, method_name)
208@property
209def function_name(self):
210 return self._function_name

Related snippets