+ 1
Variables get printed right away, even if just defined without called, why?
I Code like this Test = print("Testifyyyy") But I didn't after that code print(Test) I didn't. But still it gets printed right away. What do you think why?
8 Answers
+ 2
@Markus: Based on your question, it sounds like you were expecting Test to equal "Testifyyyy" and you were surprised to see that the value was immediately printed to the screen.
First, it might help understand, that, in Python, the print(arg) function is literally printing the arg value to the output stream. This explains why the value "Testifyyyy" was being printed immediately.
Second, the print(arg) function always returns the value "None" rather than the argument value. This explains why Test contains the value "None" instead of "Testifyyyy".
To accomplish what you were expecting (in Python), your code would need to be rewritten as such:
Test = "Testifyyyy"
print(Test)
+ 3
@Markus Awesome!!! I'm glad my explanation was helpful.
Another way to identify my earlier post as being the most helpful is to select the check mark next to that posted answer. That will mark that post as the correct answer and move it to the top.
+ 2
this wouldent work in java,
its like it ignores "Test=" and only sees "print("Testifyyy")"
but
"Test =" on its own throws an error so im assuming "Test" is initialized to "print("testifyyy")" and calls is self while compiling......
im not 100% but thats how i see it..
+ 1
Thanks so far. But how and why???
+ 1
im not sure but someone will know.
+ 1
In python 3 print is a function which returns None
so when you use
Test=print("Hi")
what really happens is,
1. Need to find Test, so print("Hi") is called
2. "Hi" gets printed
3. print function returns None
4. so Test = None
so after the line is executed "Hi" is printed and Test=None
0
Ahhhhhhhhh @David I thank you so much for this! Now I understand... Okay I will do it like this a thousand up votes for this guy!
0
Done so!