Is there a way to call a function through a variable? [duplicate] - python

This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Closed 2 years ago.
I'm really new to Python but I'm trying to do something definitely above my skill level.
Is it possible to calla a function though a variable? For example,
def one():
print("this is a function")
progress = "one"
progress()
with the intention of calling the function "one()".
Is this possible?

Just take off the double quotes around "one" and you should be good.
def one():
print("this is a function")
progress = one
progress()

Related

Python: What does assigning multiple variables to another variable mean [duplicate]

This question already has answers here:
How Python assign multiple variables at one line works?
(1 answer)
What does it mean to unpack in python?
(5 answers)
Closed 2 years ago.
I am Python newbie. Trying to understand a particular statement where a function parameter is assigned to two variables. The following is code snippet from pyscard library for Smart Cards:
# a simple card observer that prints inserted/removed cards
class PrintObserver(CardObserver):
def update(self, observable, actions):
(addedcards, removedcards) = actions
for card in addedcards:
print("+Inserted: ", toHexString(card.atr))
for card in removedcards:
print("-Removed: ", toHexString(card.atr))
I want to know what does the statement mean:
(addedcards, removedcards) = actions
Thanks.

Python: Executing a function when it is stored in a variable [duplicate]

This question already has answers here:
Assigning a function to a variable
(7 answers)
Closed 4 years ago.
I'm trying to activate a function which is stored inside a variable.
I've tried to use "lambda:" like this:
def test():
print("this works")
var = test()
lambda: var
It doesn't work. Is there any way to do that without doing anything complex? If not I don't mind hearing the complex way.
Edit:
When I posted this I meant that I wanted parameters in the function for example if you use:
def test(thing):
print(thing)
var = test
var()
Sorry for the confusion.
You use the parentheses to call the function. When you assign, you don't need the parentheses.
>>> def test():
... print("this works")
...
>>> var = test
>>> var()
this works

How to call a function given a string of that function's name? [duplicate]

This question already has answers here:
Python string to attribute
(3 answers)
Calling a function of a module by using its name (a string)
(18 answers)
Closed 5 years ago.
I have a file allthetests.py with a number of small functions defined like this:
def test1():
##do some stuff
if (everything_is_awesome):
return({"status":PASS, "data":"all the data"})
def test2():
##do some different stuff
if (everything_is_bad):
return({"status":FAIL, "data":"no data"})
Then I have another bunch of files which are descriptions of nodes and get imported as dicts like this
{"node_type":"action",
"title":"things",
"test":"test2"}
Finally I have a third file main.py which is in charge of everything.
In this third file, I want to import allthetests.py, load the node descriptions and then call allthetests.test1() or allthetests.test2() or whatever that case is. I imagine it might look something like this but I'm not really sure where to go from here...
import allthetests
cur_node = load_node()
## Doesn't work but...
return_val = allthetests.cur_node['test']()

How to get the result of a function activated through a tk.Button? [duplicate]

This question already has answers here:
Python Tkinter Return
(2 answers)
Closed 8 months ago.
I'm currently working on a GUI project on Python (3.6) using tkinter (8.6).
Following this question I'm wondering how to get back the result of someFunction :
def someFunction(event):
do stuff ..
return(otherStuff)
canvas.bind('<Button-1>',lambda event: someFunction(event))
Thank you in advance :) !
The return values of callback functions like your someFunction are ignored. Rather than using return, have the callback save the value somewhere (in a global variable or an attribute of some kind of object). Or have your function pass the computed value as an argument to some other function that will do something with it.

How to pprint and automatically include the the variable name [duplicate]

This question already has answers here:
How to print original variable's name in Python after it was returned from a function?
(13 answers)
Simpler way to create dictionary of separate variables?
(27 answers)
Closed 8 years ago.
For Python 2.7 I have a little utility for debugging:
def printvar(label, var):
print "%s:\n%s" % (label, pformat(var))
Often I call it like printvar("foo", foo).
Is it possible to simplify that a tiny bit and only pass in the variable, like printvar(foo), but still have it print the name of the variable? Using introspection or something?
You can't do that, but you can approximate the reverse: pass in only the label, and have it grab the actual value. Here's a quick version:
def printvar(label):
for frame in inspect.getouterframes(inspect.currentframe())[1:]:
local_vars = frame[0].f_locals
if label in local_vars:
print "%s:\n%s" % (label, frame[0].f_locals[label])
return
raise NameError("No such variable")
Then you can do this:
def foo():
x = 2
printvar('x')
>>> foo()
x:
2
This is, of course, very hacky business: the code looks through the local-variable namespaces of every stack frame in its call chain. Normally this kind of trickery is to be avoided, but for debugging purposes it can be useful in a pinch.

Categories

Resources