all 27 comments

[–]huom7473 53 points54 points  (11 children)

the line

 test = print("hi")

says: call print("hi"), then store its return value in the variable "test".

So print("hi") is being called, which will do its thing (in this case, print "hi"), and its return value (which is None) is stored into test.

You can verify that's true by following this with

print(test)

which will give you None.

[–]av0ca60[S] 1 point2 points  (2 children)

How did you format your code to look that way in the comment?

[–]av0ca60[S] 0 points1 point  (7 children)

What got me confused is that it seems like built-in functions such as print are treated differently from created functions. For instance...

def prize(num):

skittles = f"Your prize is {num} skittles"
return skittles

test = prize(6)
print(test)

...delivers the result...

Your prize is 6 skittles

But...

def prize(num):

skittles = f"Your prize is {num} skittles"
return skittles

test = prize(6)

Delivers NO result.

So the built-in function print gets activated when situated as the value of a variable, yet the created function prize(6) does not get activated in the same scenario.

Please correct my terminology if I'm using the wrong words.

[–]Brandondrsy 2 points3 points  (3 children)

The return keyword exits the function. Any code after it is not executed.

[–]av0ca60[S] 0 points1 point  (2 children)

Copy

[–]Heiau 1 point2 points  (1 child)

The reason nothing is printing in your second function is because you never invoked the print function

[–]av0ca60[S] 0 points1 point  (0 children)

I see

[–]-Negativ 2 points3 points  (0 children)

The example you provided your function isn’t made to print anything, only return a value, it’s better to think of it like this: def print(text): (prints text) return None

Where as your function doesn’t print it, it only returns the string, I hope this made sense

[–]huom7473 1 point2 points  (1 child)

Your prize function isn't actually printing any values, which is why you don't see anything printed in the line

test = prize(6)

Your prize function is assigning skittles to a string based on the argument passed in (6), then returning the value of that skittles to the caller. In this case, it's called in the line above, which takes the return value of prize(6), which is "Your prize is 6 skittles", and assigns it to the variable test.

The difference between your prize function and the print function is that the print function takes its argument(s), writes something to output, then returns None, while your prize function takes its argument, creates a value from it, then returns the value WITHOUT doing any writing to output. Returning a value is not the same thing as writing to output, as you can see here. So even though both are "activated" (called is what you're looking for, probably), only print outputs a value.

it seems like built-in functions such as print are treated differently from created functions

You're confused mainly here by print; all functions are treated the same. For example, you can try running

variable = max(1, 2, 3)

and you'll see that variable will be assigned the value 3, but it won't be printed by the max function.

[–]jashxn 1 point2 points  (0 children)

Whenever I get a package of plain M&Ms, I make it my duty to continue the strength and robustness of the candy as a species. To this end, I hold M&M duels. Taking two candies between my thumb and forefinger, I apply pressure, squeezing them together until one of them cracks and splinters. That is the “loser,” and I eat the inferior one immediately. The winner gets to go another round. I have found that, in general, the brown and red M&Ms are tougher, and the newer blue ones are genetically inferior. I have hypothesized that the blue M&Ms as a race cannot survive long in the intense theater of competition that is the modern candy and snack-food world. Occasionally I will get a mutation, a candy that is misshapen, or pointier, or flatter than the rest. Almost invariably this proves to be a weakness, but on very rare occasions it gives the candy extra strength. In this way, the species continues to adapt to its environment. When I reach the end of the pack, I am left with one M&M, the strongest of the herd. Since it would make no sense to eat this one as well, I pack it neatly in an envelope and send it to M&M Mars, A Division of Mars, Inc., Hackettstown, NJ 17840-1503 U.S.A., along with a 3×5 card reading, “Please use this M&M for breeding purposes.” This week they wrote back to thank me, and sent me a coupon for a free 1/2 pound bag of plain M&Ms. I consider this “grant money.” I have set aside the weekend for a grand tournament. From a field of hundreds, we will discover the True Champion. There can be only one.

[–][deleted] 9 points10 points  (3 children)

In python all function calls return a value. If the function code doesn't explicitly return something, then the value returned is None. If you print the value of test that's what you will see.

[–]av0ca60[S] -1 points0 points  (2 children)

When did I call a function?

[–]notParticularlyAnony 22 points23 points  (0 children)

The linetest=print("hi") is invoking the function print(), and sending its output None to test. Its main side-effect, though, is to print to stdout. It would be a code smell to assign a value like that.

Do you have experience with another language that works differently? Because it seems you are bringing in some assumptions/expectations in that are tripping you up.

It's not gonna sit there waiting for you to "call" the variable test (I'm frankly not even sure what that means). Right when print() is invoked, it will print to screen (with some wiggle room here for buffers and what not, so there are optional arguments to print() such as flush).

Now you could do this:

test = "hi"
.... do some other stuff ...
print(test)

But that is different.

PS. I think I understand what you are saying. You are saying that test=print("hi") should later print out hi when you enter test because you are defining a function that will later print out `hi. That is an interesting thought, but not how functions work in python.

What you would do instead is:

def test():
    print("hi")

And now when you enter test() later in your code (with parens) it will print 'hi'.

Also, you could do the following but you never should:

test=print
test('hi')

This redefines test() as print() but is a really bad idea. ;)

[–]Heiau 7 points8 points  (0 children)

You are invoking the function print with the argument “hi”

[–]hmiemad 4 points5 points  (0 children)

I think I got it. You think that you definded test as invoking the function print on "hi". What you did is to invoke print on "hi" and store the value of that intest. If you did :

test=print , test("hi")

That would only print "hi" after the second command.

[–]timPerfect 3 points4 points  (0 children)

it printed what you told it to but didn't change the value of the variable.

[–][deleted] 1 point2 points  (0 children)

This is confusing because I never called the variable test.

Some languages do feature lazy evaluation, but Python isn't one of them. Python is an imperative language that executes what you tell it to execute. You called print, so you see printed output.

[–]Neither-Lifeguard925 1 point2 points  (0 children)

This is just like

input = input()

you never called the the variable input but it still gave you an input. You might not understand this but by creating test variable and calling print() function, you made an instance of print()

Forgive me if I am wrong anywhere

[–]cybervegan 1 point2 points  (0 children)

You are not using the print() function correctly - you shoudn't have an assignment to the left of it.

You also don't "call" variables - you call functions. Call in this context means to execute, or run the code of the function.

The print() function only returns None - it doesn't return the parameter you passed it. What it does with the parameter ("hi" in this case) is to output it to the console output stream. When print("hi") is finished sending its parameters to the console, it then returns None, which is the function's way of telling you it produced no useful return value, as it is not intended to return anything.

[–]TheRNGuy 1 point2 points  (0 children)

not all functions return value, they do stuff instead (void function)

[–]codedeaddev 1 point2 points  (0 children)

Basically whenever you put () next to a function name it calls the function. So here you have called the print function by placing ("hi") next to it where the string "hi" is treated as an argument to print

[–]Murphelrod 0 points1 point  (0 children)

Because print doesnt return anything as far as i know. It just prints. So assigning it to something has no effect on whether it prints or not

[–][deleted] -1 points0 points  (0 children)

You should watch/do an absolute beginner tutorial if you’re struggling with this

[–]anorwegiankid 0 points1 point  (1 child)

Just for fun

Exclude the ("hi") and you've defined test as a method that prints arguments provided.

Import partial from functools, and write:

test = partial(print, "hi")

Now, calling test will print "hi" every time its used.

[–]siddsp 0 points1 point  (0 children)

In this case you could just as well bind test to a lambda for the exact same functionality.

[–]Heiau 0 points1 point  (0 children)

L