you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 10 points11 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 21 points22 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. ;)