Python function name as variable
The trick is to use globals(): Show
will be equivalent to
found at: George Sakkis https://bytes.com/topic/python/answers/792283-calling-variable-function-name The following is a useful application of the above questioning I needed right now (that's why I came here): apply special functions to URLs depending on their nature:
I used to write
and so on - my list has 19 members by now and keeps growing. While investigating the subject and developing, the function code had been quite naturally part of the main function making it soon horrible to read, so relocating the working code into functions was a great relief already. This clumsy code above can be substituted by:
This way the main code stays simple and legible no matter how long the list of conditions may grow. Those functions corresponding to the condition labels have to be declared conventionally, of course, depending on the nature of the type of the URL in question:
Test:
Or, to model it closer to the above scenario:
Test:
Result: Further elaboration on the problem now just amounts to augment the condition list one by one and write the corresponding functions depending on the argument. The above mechanism will pick the right one thereafter. In Python there are three things that can be considered being name of the function: The original name of the code blockIt’s stored in the This attribute is readonly and can’t be changed. So the only way to create function with the custom name in runtime I’m aware of is (There is also more low-level way to do this that.) The immutability of The __name__ attribute of the function objectIt’s also aliased to You can modify it (
Here is the FUNCTIONS With Name of the referenceOne more thing that can be called function name (though it hardly is) is the name of a variable or an attribute where reference to that function is stored. If you create function with Obviously you can create more than one reference to the same function and all that references can have different names. The only way all that three things are connected to each other is the Output: File "my.py", line 13, in Pydoc: FUNCTIONS This article was originally posted as an answer to my own Stack Overflow question. I thank other people for comments and answers, they helped me to organize my thoughts and knowledge. Can we use a function name as a variable name?The best suggestion is to discontinue the use of function names as variable names completely. However, in certain applications in which this is not possible, place the relevant code in a separate script and then invoke the script from the function.
Can variable name be same as function name Python?Bottom line: you can't have two things simultaneously with the same name, be it a function, an integer, or any other object in Python. Just use a different name.
How do you call a function variable in Python?To use functions in Python, you write the function name (or the variable that points to the function object) followed by parentheses (to call the function). If that function accepts arguments (as most functions do), then you'll pass the arguments inside the parentheses as you call the function.
What is the __ name __ variable in Python?The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script. Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.
|