'How do I pass a value into a python function without calling?

I was unable to find a reasonable way to create a variable which calls a function requiring parameters.

Here is a simplified version of my code. I would like ‘print_hello’ to print ‘hello’ when it is called, and not when it is defined.

print_hello = print(‘hello’)

When I define ‘print_hello’, it calls print(‘hello’). When I call ‘print_hello’, it gives me an error. How do I fix this?



Solution 1:[1]

Just define print_hello as a lambda function

>>> print_hello = lambda: print('hello')
>>> print_hello()
hello

To delay execution, you'll have to wrap the call to print in another function. A lambda is less code than defining another function.

Note: that pep08 recommends using a def function rather than a lambda when assigning to a variable. See here. So @Sheldores answer is probably the way to go.

Solution 2:[2]

If you just want a function that does precisely what you describe, Sheldore's answer is the simplest way to go (and more Pythonic that using a named lambda).

An alternative approach is to make a partial application of the function with functools.partial, which allows you to pass additional arguments at call time:

from functools import partial

print_hello = partial(print, "hello")

print_hello()  # Prints "hello" to stdout

print_hello(file=sys.stderr)  # Prints "hello" to stderr

print_hello("world")  # Prints "hello world" to stdout

Solution 3:[3]

You need to define a function. In python a function is defined using def as shown in a simple example for your purpose below. You then call the function using the function name and (), for instance print_hello().

def print_hello(): # <--- Does not accept an argument 
    print('hello')

print_hello()    # <--- No argument is passed
# hello

Another example to give you more idea on how to pass an argument to the function. You can define a variable that contains the string you want to print, let's say to_print and then pass this as an argument to your function during calling it. While explaining more details is out of the scope of this answer, the two examples I gave should get you started. For more details, you can refer to the official docs here

def print_hello(to_print): # <--- Accepts an argument 
        print(to_print)

to_print = "hello"
print_hello(to_print) # <--- Argument is passed
# hello

Solution 4:[4]

You could use a lambda expression:

print_hello = lambda: print('hello')

Or an actual function definition:

def print_hello(): print('hello')

Or functools.partial (this is different in that you can still use other arguments for print whereas you lose that functionality with the others unless specified in the definitions)

from functools import partial
print_hello = partial(print, 'hello')

To use any of these:

print_hello()
#'hello'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3
Solution 4