This question already has answers here:
Can one function have multiple names?
(3 answers)
Closed 5 years ago.
I wold like to use same method but two different names.
For example:
def func(a):
print a
def func2(a):
print a
n= "yes"
func(n)
func2(n)
answer should be:
"yes"
"yes"
Would there be any way I can do:
def fun(a) or func2(a):
print a
or something like this?
Python functions are just objects, you can assign one to another name:
def fun(a):
print a
func2 = fun
Now the names func2 and fun reference the same function object, you can call it through either name.
def func(a):
print("a")
n= "yes"
def fun(a):
func(a)
func(n)
Related
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 3 years ago.
I want to create functions (def $name:) where name comes from array=['str1', 'str2', ...].
I tried the code above and already searched on stackoverflow and google but did not find any solution.
array = ['String1', 'String2', ...]
def array[0]:
code1
def array[1]:
code2
String1()
String2()
You can't define a function using a variable. But it turns out that you can rebind functions variable names.
See this post:
How to use a variable as function name in Python
Example of how to do that:
one = 'one'
two = 'two'
three = 'three'
l = [one, two, three]
def some_stuff():
print("i am sure some stuff")
for item in l:
def _f():
some_stuff()
globals()[item] = _f
del _f
one()
two()
three()
This question already has answers here:
How do I get a result (output) from a function? How can I use the result later?
(4 answers)
Closed 6 months ago.
If I had a function that I made:
def a():
n = 2*2
How could I access n out of the function without calling a?
You cannot. You will need to define the variable outside of the function, or call the function and return it.
You need to return it, so do:
def a():
n = 2*2
return n
print(a())
Output:
4
You can also do print, but return is better, check this: What is the formal difference between "print" and "return"?
This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 6 years ago.
I am trying to make a simple function that accepts 2 parameters, and adds them together using "+".
def do_plus (a,b):
a=3
b=7
result = a + b
print (result)
However, I get no value returned, the function is executed but no output is shown.
You're missing the indentation.
a=3
b=7
def do_plus (a,b):
result =a+b
print (result)
# and you have to call the function:
do_plus(a,b)
You probably want to separate logic from input/output, as so:
def do_plus(a, b):
result = a + b
return result
res = do_plus(3, 7)
print(res)
try this:
def do_plus (a,b):
print=a+b
do_plus(3, 7)
you can call your function "do_plus" passing parameters and print wath the function return
Attention to the "spaces" before result is important in python the identation of script
It's hard to tell from your code because the indentation is off, but a simple addition function can be something like:
def addition(a, b):
return a + b
You are accepting parameters a and b, but then assigning them values 7 and 3, so that no matter what, it will return 10.
This question already has answers here:
Python function as a function argument?
(10 answers)
Closed 6 years ago.
def example(function):
if input() == "Hello there!":
#at this point I want to call the function entered in the tuples
an example of what I mean:
def example(function):
if input() == "Hello there!":
#do the function here
def Printer(What_to_print):
print(What_to_print + "Just an example")
example(Printer)
Is this possibe and are there drawbacks in doing this?
Yes. It is possible.
def example(function):
if input() == "Hello there!":
function("Hello there!") # invoke it!
Actually you can pass def functions and lambda functions as parameters and invoke them by () syntax.
In python, functions are objects like any other common types, like ints and strs. Therefore, there is no problem with a function that receives another function as an argument.
>>> def pr(): print ('yay')
>>> def func(f): f()
>>> isinstance(pr, object)
True
>>> isinstance(int, object)
True
>>> func(pr)
yay
>>>
def example(function, what_to_print):
if raw_input() == "Hello there!":
function(what_to_print)
def printer(what_to_print):
print(what_to_print + "Just an example")
example(printer, "")
This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Closed 8 years ago.
s = "func"
Now suppose there is function called func.
How can i call in Python 2.7 call func when the function name is given as a string?
The safest way to do this:
In [492]: def fun():
.....: print("Yep, I was called")
.....:
In [493]: locals()['fun']()
Yep, I was called
Depending on the context you might want to use globals() instead.
Alternatively you might want to setup something like this:
def spam():
print("spam spam spam spam spam on eggs")
def voom():
print("four million volts")
def flesh_wound():
print("'Tis but a scratch")
functions = {'spam': spam,
'voom': voom,
'something completely different': flesh_wound,
}
try:
functions[raw_input("What function should I call?")]()
except KeyError:
print("I'm sorry, I don't know that function")
You can also pass arguments into your function a la:
def knights_who_say(saying):
print("We are the knights who say {}".format(saying))
functions['knights_who_say'] = knights_who_say
function = raw_input("What is your function? ")
if function == 'knights_who_say':
saying = raw_input("What is your saying? ")
functions[function](saying)
else:
functions[function]()
def func():
print("hello")
s = "func"
eval(s)()
In [7]: s = "func"
In [8]: eval(s)()
hello
Not recommended! Just showing you how.
you could use exec. Not recommended but doable.
s = "func()"
exec s
You can execute a function by passing a string:
exec(s + '()')