+ 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?

24th Aug 2017, 9:39 PM
Markus Beyer
Markus Beyer - avatar
8 odpowiedzi
+ 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)
25th Aug 2017, 4:37 AM
David Carroll
David Carroll - avatar
+ 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.
25th Aug 2017, 12:00 PM
David Carroll
David Carroll - avatar
+ 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..
24th Aug 2017, 9:53 PM
D_Stark
D_Stark - avatar
+ 1
Thanks so far. But how and why???
24th Aug 2017, 9:55 PM
Markus Beyer
Markus Beyer - avatar
+ 1
im not sure but someone will know.
24th Aug 2017, 10:16 PM
D_Stark
D_Stark - avatar
+ 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
25th Aug 2017, 3:29 AM
Sunera Avinash
Sunera Avinash - avatar
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!
25th Aug 2017, 6:30 AM
Markus Beyer
Markus Beyer - avatar
0
Done so!
25th Aug 2017, 2:33 PM
Markus Beyer
Markus Beyer - avatar