Determine function name from within that function (without using traceback)
Python doesn't have a feature to access the function or its name within the function itself. It has been proposed but rejected. If you don't want to play with the stack yourself, you should either use "bar"
or bar.__name__
depending on context.
The given rejection notice is:
This PEP is rejected. It is not clear how it should be implemented or what the precise semantics should be in edge cases, and there aren't enough important use cases given. response has been lukewarm at best.
get current function name inside that function using python
You probably want inspect.getframeinfo(frame).function
:
import inspect
def whoami():
frame = inspect.currentframe()
return inspect.getframeinfo(frame).function
def foo():
print(whoami())
foo()
prints
whoami
Determine function name within that function
as.character(match.call()[[1]])
Demo:
my_fun <- function(){
as.character(match.call()[[1]])
}
my_fun()
# [1] "my_fun"
foo_bar <- function(){
as.character(match.call()[[1]])
}
foo_bar()
# [1] "foo_bar"
ballyhoo <- function(){
foo_bar()
}
ballyhoo()
# [1] "foo_bar"
tom_foolery <- foo_bar
tom_foolery()
# [1] "tom_foolery"
How to get a function name as a string?
my_function.__name__
Using __name__
is the preferred method as it applies uniformly. Unlike func_name
, it works on built-in functions as well:
>>> import time
>>> time.time.func_name
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'builtin_function_or_method' object has no attribute 'func_name'
>>> time.time.__name__
'time'
Also the double underscores indicate to the reader this is a special attribute. As a bonus, classes and modules have a __name__
attribute too, so you only have remember one special name.
how to get function's name from within the function (or kind of self reference to the function)?
Perhaps you should decorate each function that you're calling with an onlyonce
decorator? That would be more pythonic. A proof of concept follows.
called = set()
def onlyonce(fn):
def decorated(*largs, **kargs):
if fn not in called:
called.add(fn)
print "Calling"
fn(*largs, **kargs)
else:
print "Already called"
return decorated
@onlyonce
def test_function():
print "I am getting called now"
test_function()
test_function()
test_function()
test_function()
Also, functions are "immutable" and can be stored as dictionary keys. You don't have to rely on the names. This might be an advantage (or a disadvantage) depending on your use.
How to get Azure Function name from within a function using C# script (csx)
The ExecutionContext
class has a property FunctionName
(docs)
You can inject the class like this:
[FunctionName("HttpTriggered")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
HttpRequest req,
ExecutionContext executionContext)
{
var name = executionContext.FunctionName;
...
}
The output will be
HttpTriggered
Get function name from inside itself
var test2 = {
foo: function() {
}
};
You aren't giving the function a name. You are assigning the foo
property of test2
to an anonymous function.
arguments.callee.name
only works when functions are declared using the function foo(){}
syntax.
This should work:
var test2 = {
foo: function foo() {
console.log(arguments.callee.name); // "foo"
}
};
How to determine function name from inside a function
You can use ${FUNCNAME[0]}
in bash
to get the function name.
Related Topics
JavaScript Swap Array Elements
How to Create an Https Server in Node.Js
Get Unique Selector of Element in Jquery
How to Use JavaScript to Change the Form Action
Angularjs: Better Way to Watch for Height Change
Why My Show Hide Button Needs Double-Click on First Time
Html5 Audio Tag on Safari Has a Delay
How to Validate Date with Format "Mm/Dd/Yyyy" in JavaScript
Array VS. Object Efficiency in JavaScript
How Does _Proto_ Differ from Constructor.Prototype
Defer Execution for Es6 Template Literals
How to Use JavaScript to Read Local Text File and Read Line by Line