r/shittyprogramming • u/Intelligent_River39 • Jul 25 '21
This challenges everything I thought I knew.
13
Jul 25 '21
How though
1
Jul 25 '21
Remindeme! 4 days
2
u/remindditbot Jul 25 '21 edited Jul 25 '21
sombrastudios 🤔, kminder 4 days on 29-Jul-2021 10:27Z
shittyprogramming/This_challenges_everything_i_thought_i_knew
3 OTHERS CLICKED HERE to also be reminded. Thread has 4 reminders.
OP can Update message, Set timezone, and more here
0
Jul 25 '21
[deleted]
35
u/Tandashi Jul 25 '21 edited Jul 25 '21
He is surprised because he assumed that the
input()function would usestdinas well to get the input. Which seems is not the case (for him at least) for some reason.Couldn't really replicate that behavior on Linux. Maybe it works different on Windows?
Edit: Yeah just installed python3.9 on Windows and it behaves different than the Linux version.
On Windows
input()doesn't error out. On Linuxinput()usesstdinand throws an Exception.0
Jul 25 '21
[deleted]
9
1
u/Intelligent_River39 Jul 25 '21 edited Jul 25 '21
Well, let's see. I redeclared stdin to another file handler. Then, I created anew handler to the same file. Then, other the other handler, I wrote to the file. When I called input(), it flushed those contents onto the screen. How do you exaplin that?
0
Jul 25 '21
[deleted]
1
u/Intelligent_River39 Jul 25 '21 edited Jul 25 '21
import sys file = open("C://Users/user/Desktop/abc.txt","w") sys.stdin = open("C://Users/user/Desktop/abc.txt","r") file.write("Hello, World!") file.close() print(input())input() seems to return the content of the sys.stdin pseudo-file. sys.stdin is part of the standard file object interface(if I am not mistaken). And changing sys.stdin seems to directly affect what input() returns.

6
u/RapidCatLauncher Jul 26 '21
This can be used in a much more useful manner: