This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Closed 8 years ago.
I receive a string, for example "save". And i have a method save with paramethers.
How can i convert the string save in a call to save().
I tried with eval and exec.
Edit: Solved here --> Calling a function of a module from a string with the function's name in Python
def some_method(self):
save_method = getattr(self, 'save')
save_method() # save()
Related
This question already has answers here:
Why does typing a variable (or expression) print the value to stdout?
(2 answers)
Closed 3 years ago.
When I import the datetime module for example, and create a datetime.datetime object, thus that this datetime.datetime object is the value of the expression, it returns
datetime.datetime(*values inserted*)
But this is not the _str__() return value of datetime.datetime.
How is the output of the interactive console defined to behave?
Is there a specification or comprehensible explanation?
It shows the value of __repr__().
This question already has answers here:
How to restore a builtin that I overwrote by accident?
(3 answers)
Closed 3 years ago.
Redefined builtin function len() in spyder like len = 100;
Now when I am trying to find the length of the string it says "'int' object is not callable"
How to undo this change or correct this?
Since len is an inbuilt python function, you should avoid using that as a variable. try prefixing your 'len' with '_' and use '_len = 10' instead of just 'len'.
This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Use a string to call function in Python [duplicate]
(3 answers)
Closed 4 years ago.
I have this simple function:
def fu():
return "great"
I need to call it by using a string,
So I tried this:
print(exec("fu()"))
But the the output I got was:
None
How do i fix it?
As in comments says you can not use exec for this purpose.
but eval will do what you want, full doc here:
>> eval('fu()')
"great"
Note that using eval is not the best practice.
There is a better way to access this function with globals or locals based on where you define your function, and I think it's better to use this instead of eval:
>> globals()['fu']()
"great"
This question already has answers here:
Simpler way to create dictionary of separate variables?
(27 answers)
How can you print a variable name in python? [duplicate]
(8 answers)
Closed 8 years ago.
Can a Python instance get the name of the variable which is used to access the object?
This example code shows what I need:
foo=MyClass()
foo.get_name() --> 'foo'
bar=foo
bar.get_name() --> 'bar'
I know that this is black magic and not clean code. I just want to know if it is possible.
I know that bar.__name__ returns the name, but I need it inside an own method.
How can get_name() be implemented?
This is not a duplicate of questions which answer is __name__
This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Closed 9 years ago.
I got this:
tu = ("func1", "func2", "func3")
And with the operation I am looking for I would get this for the first string:
moduleA.func1()
I know how to concatenate strings, but is there a way to join into a callable string?
getattr(moduleA, 'func1')() == moduleA.func1()
You should use getattr builtin function. Try:
getattr(moduleA, 'func1')()
If you mean get a function or method on a class or module, all entities (including classes, modules, functions, and methods) are objects, so you can do a func = getattr(thing 'func1') to get the function, then func() to call it.