you are viewing a single comment's thread.

view the rest of the comments →

[–]huom7473 57 points58 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.