Convert string to function name in python [duplicate] - python

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 2 years ago.
I have many functions with the same prefix. I want to use a combination of strings to invoke the function.
def func_plus_one(v):
return v+1
def func_plus_two(v):
return v+2
a='plus_one'
b='plus_two'
So how can I use 'func_'+a and 'func_'+b to use the function?

If the functions are in the same module as the code needing to reference them, use the globals() of the module. You could call the function indicated by a using:
globals()['func_' + a](x)
If they are in another module, use getattr
getattr(some_module, func_' + a)(x)

Related

Execute a string in Python inside a function so that the variables defined can be used [duplicate]

This question already has answers here:
How to get local variables updated, when using the `exec` call?
(3 answers)
Closed 1 year ago.
Given a string (e.g. "lista=[1,2,3]") I would like to be able to use the variable lista.
exec() does the work outside a function, but when used inside a function the variables cannot be used in that same function. I guess it has something to do with local and global variables but I don't really understand the problem.
For example,
def funcion(texto):
exec(texto)
print(lista)
funcion("lista = [3,4,5]")
Gives the error: NameError: name 'lista' is not defined.
add globals
def funcion(texto):
exec(texto, globals())
print(lista)
funcion("lista = [3,4,5]")

Python variable variables in "main" [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 4 years ago.
I want to automatically define variables using a dynamic string, similar to ${$a} in PHP.
As I do this in a plain script I do not have objects and therefore can not apply setattr or am I wrong?
How do I dynamically define variables?
You can store the variables you want in a dictionary. That's basically what php does, only in Python you can specify which dictionary to use and how to handle it.
You can modify local or global dictionary:
>>> locals()['xxx'] = 'Hi'
>>> xxx
'Hi'
>>> globals()['yyy'] = 'Hello'
>>> yyy
'Hello'

Python dot notation method attributes VS other methods [duplicate]

This question already has answers here:
Why isn't the 'len' function inherited by dictionaries and lists in Python
(7 answers)
Why does Python code use len() function instead of a length method?
(7 answers)
In Python, when should I use a function instead of a method?
(5 answers)
Difference between len() and .__len__()?
(5 answers)
Closed 4 years ago.
In Python, why are some built-in functions called using brackets with the method name before the object, e.g.
print("foobar")
bool("foobar")
...
While others are built-in method attributes, called with a dot behind the object, e.g.
"foobar".capitalize()
Specifically I'm interested to learn if there is a general principle behind this instead of just common practice and memorization. In cases where you can't quite remember whether it was capitalize("foobar") or "foobar".capitalize(), how do you know?

is it possible to use a variable to name another variable? [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 5 years ago.
here is what I want to do:
b=1
har1=42
print(f"har{b}")
I want it to display 42
I have tried a few things, but I haven't managed to get it to work
Use either globals() or locals() and construct the key string.
b=1
har1=42
globals()['har'+str(b)]
def f():
b=1
har1=42
print(locals()['har'+str(b)])
f()

Identity function in python [duplicate]

This question already has answers here:
Is there a builtin identity function in python?
(10 answers)
Closed 7 years ago.
In functional programming is sometimes useful to have an identity function.
Is there a built-in or a function defined in some module that does this?
The identity function can be simply defined as:
identity = lambda x: x
I'm not aware of this function defined in any module, but it could be a good fit for functools.

Categories

Resources