4

Whenever I assign a variable to a function, it runs the function. As a result, if I were to have a print statement in the function, it would print the text even though all I want to do is assign but not run the function. I don't believe this is the case in many other programming languages such as C++, so is there a core concept that I'm missing here?

def function(x):
    print("Text From Function")
    return 3*x

y = function(2)

I expect there to be no output but the actual output is: Text From Function

Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33
Loop
  • 169
  • 1
  • 5
  • 4
    y = function. if you put the brackets, you called/ran it – Paritosh Singh Jul 27 '19 at 20:14
  • Gotcha. That makes perfect sense. In that case, if I wanted to call the function and I needed to define a value of x, I would do something like y(some value here), right? – Loop Jul 27 '19 at 20:17
  • If you're trying to assign a function to a variable with `y = function(2)`, what's the `2` doing there? Did you want to assign a function *with arguments* to `y`? – Aran-Fey Jul 27 '19 at 20:17
  • 1
    @Loop why don't you try running it? (and the answer is yes) – Paritosh Singh Jul 27 '19 at 20:19
  • @Aran-Fey I think I figured it out, but in the hypothetical situation that I wanted to assign a function with arguments, is it possible? – Loop Jul 27 '19 at 20:19
  • In Python, functions are objects. In this case, `function` is already a _name_ (i.e., variable, in a sense) for the thing the function does. Assigning `y = function` is the right way to go about this. But really, that just aliases `function` with `y`. – brentertainer Jul 27 '19 at 20:20
  • [Yes.](https://stackoverflow.com/questions/277922/python-argument-binders) – Aran-Fey Jul 27 '19 at 20:20
  • 1
    yes, but not exactly directly. it's a concept called partial functions. essentially you define a different function acting as a wrapper around this one, with an argument already ready to go. – Paritosh Singh Jul 27 '19 at 20:20
  • If you want to call `y()` without parameters, but you want it to be equivalent to `function(2)` such that the parameter is pre-assigned, this is called [currying](https://www.geeksforgeeks.org/currying-function-in-python/). Use `y = lambda : function(2)` – Trevin Avery Jul 27 '19 at 20:21
  • y = lamda: function(2) – Liran Funaro Jul 27 '19 at 20:21

3 Answers3

5

If you have function a and want to assign it to variable y you simply do:

def a():
  print("hello")
y = a
y()

In this case running y() will print "hello". If you use parenthesis after a function it will call it and return whatever the function returns, not the function itself.

Anto
  • 66
  • 3
3

Going from the comments by @ParitoshSingh, @LiranFunaro, and @TrevinAvery, you either want to use a lambda or functools.partial to assign a function with prepopulated arguments to a new name.

import functools

def function(x):
    print("Text From Function")
    return 3*x

y1 = lambda: function(2)
y2 = functools.partial(function, 2)

These are then invoked with y1() and y2().

brentertainer
  • 2,118
  • 1
  • 6
  • 15
1

This is because functions are made so that you don't have to rewrite the same code over and over again, so when you're writing:

function(2)

it executes the entire code written in

def function(x):

which includes printing text.

If you want to assign the function to a variable, you need to write:

y = function

without brackets and you will be able to do

result = y(2)

However, you cannot assign the value returned by the function without printing the text if the print() function is in your function def. If you want to get the returned value without the printed text, you need to get rid of the print() function in your function def code.

Vincent Quirion
  • 389
  • 2
  • 4
  • 16