▲ 1172 ▼ On this deserted island I could use some help() (lemmy.blahaj.zone) submitted 2 years ago by unlawfulbooger@lemmy.blahaj.zone to c/programmerhumor@lemmy.ml 32 comments fedilink hide all child comments
[–] EmergMemeHologram@startrek.website 66 points 2 years ago (13 children) “exit” ✈️: Use exit() or Ctrl-D (I.e. EOF) to exit permalink fedilink source hideshow 13 child comments replies: [–] PlexSheep@feddit.de 15 points 2 years ago (12 children) I mean if they can see that we type exit and show us this message, why could they not just start the exiting when we type exit? permalink fedilink source parent hideshow 12 child comments replies: [–] Zron@lemmy.world 19 points 2 years ago (3 children) Because exit might be a variable you use to determine if you should exit. exit() is a function that actually does the exiting. It’s the difference between pointing at a jogger and saying “run” and actually running after them. permalink fedilink source parent hideshow 3 child comments replies: [–] Bronco1676@lemmy.ml 17 points 2 years ago (2 children) If you have a variable called exit you've overwritten the function in that scope, and won't be able to execute it. e.g. >>> exit=1 >>> exit() Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not callable >>> permalink fedilink source parent hideshow 2 child comments replies: [–] EmergMemeHologram@startrek.website 17 points 2 years ago Reminds me of setting pi = 3 in my friends matlab subroutines in school. permalink fedilink source parent [–] peopleproblems@lemmy.world 5 points 2 years ago wow it does do that. cool permalink fedilink source parent [–] WhiskyTangoFoxtrot@lemmy.world 14 points 2 years ago (5 children) Guessing at what the programmer wants instead of implementing consistent behaviour is what Javascript does. Do you want Python to become Javascript? permalink fedilink source parent hideshow 5 child comments replies: [–] tetris11@lemmy.ml 5 points 2 years ago (4 children) Just once I want '1' + '2' to equal '3'. Is that so much to ask? permalink fedilink source parent hideshow 4 child comments replies: [–] Metype@lemmy.world 7 points 2 years ago (2 children) You want to remove the string concatenation operator? Cause that'll do it permalink fedilink source parent hideshow 2 child comments replies: [–] tetris11@lemmy.ml 9 points 2 years ago (1 child) I think every language needs a please operator, which acts to enforce human expectation of a statement: '1' + '1' ## evaluates to '11' please '1' + '1' ## evaluates to '2' permalink fedilink source parent hideshow 1 child comment replies: [–] PlexSheep@feddit.de 4 points 2 years ago I kinda like that permalink fedilink source parent [–] wewbull@feddit.uk 5 points 2 years ago Yes. Yes it is. permalink fedilink source parent [–] Bronco1676@lemmy.ml 7 points 2 years ago* This is the code (Github link): class Quitter(object): def __init__(self, name, eof): self.name = name self.eof = eof def __repr__(self): return 'Use %s() or %s to exit' % (self.name, self.eof) def __call__(self, code=None): # Shells like IDLE catch the SystemExit, but listen when their # stdin wrapper is closed. try: sys.stdin.close() except: pass raise SystemExit(code) What happens is that the python repl calls __repr__ automatically on each variable/statement that you type into the repl (except assignments e.g. x = 1). But this basically only happens in the repl. So "executing" only exit wouldn't work in a python script as it is not calling __repr__ automatically, so better you learn how to do it right than using just exit in your python scripts and scratching your head why it works in the repl but not in your code. permalink fedilink source parent [–] eluvatar@programming.dev 2 points 2 years ago Because python has strong opinions permalink fedilink source parent
[–] PlexSheep@feddit.de 15 points 2 years ago (12 children) I mean if they can see that we type exit and show us this message, why could they not just start the exiting when we type exit? permalink fedilink source parent hideshow 12 child comments replies: [–] Zron@lemmy.world 19 points 2 years ago (3 children) Because exit might be a variable you use to determine if you should exit. exit() is a function that actually does the exiting. It’s the difference between pointing at a jogger and saying “run” and actually running after them. permalink fedilink source parent hideshow 3 child comments replies: [–] Bronco1676@lemmy.ml 17 points 2 years ago (2 children) If you have a variable called exit you've overwritten the function in that scope, and won't be able to execute it. e.g. >>> exit=1 >>> exit() Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not callable >>> permalink fedilink source parent hideshow 2 child comments replies: [–] EmergMemeHologram@startrek.website 17 points 2 years ago Reminds me of setting pi = 3 in my friends matlab subroutines in school. permalink fedilink source parent [–] peopleproblems@lemmy.world 5 points 2 years ago wow it does do that. cool permalink fedilink source parent [–] WhiskyTangoFoxtrot@lemmy.world 14 points 2 years ago (5 children) Guessing at what the programmer wants instead of implementing consistent behaviour is what Javascript does. Do you want Python to become Javascript? permalink fedilink source parent hideshow 5 child comments replies: [–] tetris11@lemmy.ml 5 points 2 years ago (4 children) Just once I want '1' + '2' to equal '3'. Is that so much to ask? permalink fedilink source parent hideshow 4 child comments replies: [–] Metype@lemmy.world 7 points 2 years ago (2 children) You want to remove the string concatenation operator? Cause that'll do it permalink fedilink source parent hideshow 2 child comments replies: [–] tetris11@lemmy.ml 9 points 2 years ago (1 child) I think every language needs a please operator, which acts to enforce human expectation of a statement: '1' + '1' ## evaluates to '11' please '1' + '1' ## evaluates to '2' permalink fedilink source parent hideshow 1 child comment replies: [–] PlexSheep@feddit.de 4 points 2 years ago I kinda like that permalink fedilink source parent [–] wewbull@feddit.uk 5 points 2 years ago Yes. Yes it is. permalink fedilink source parent [–] Bronco1676@lemmy.ml 7 points 2 years ago* This is the code (Github link): class Quitter(object): def __init__(self, name, eof): self.name = name self.eof = eof def __repr__(self): return 'Use %s() or %s to exit' % (self.name, self.eof) def __call__(self, code=None): # Shells like IDLE catch the SystemExit, but listen when their # stdin wrapper is closed. try: sys.stdin.close() except: pass raise SystemExit(code) What happens is that the python repl calls __repr__ automatically on each variable/statement that you type into the repl (except assignments e.g. x = 1). But this basically only happens in the repl. So "executing" only exit wouldn't work in a python script as it is not calling __repr__ automatically, so better you learn how to do it right than using just exit in your python scripts and scratching your head why it works in the repl but not in your code. permalink fedilink source parent [–] eluvatar@programming.dev 2 points 2 years ago Because python has strong opinions permalink fedilink source parent
[–] Zron@lemmy.world 19 points 2 years ago (3 children) Because exit might be a variable you use to determine if you should exit. exit() is a function that actually does the exiting. It’s the difference between pointing at a jogger and saying “run” and actually running after them. permalink fedilink source parent hideshow 3 child comments replies: [–] Bronco1676@lemmy.ml 17 points 2 years ago (2 children) If you have a variable called exit you've overwritten the function in that scope, and won't be able to execute it. e.g. >>> exit=1 >>> exit() Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not callable >>> permalink fedilink source parent hideshow 2 child comments replies: [–] EmergMemeHologram@startrek.website 17 points 2 years ago Reminds me of setting pi = 3 in my friends matlab subroutines in school. permalink fedilink source parent [–] peopleproblems@lemmy.world 5 points 2 years ago wow it does do that. cool permalink fedilink source parent
[–] Bronco1676@lemmy.ml 17 points 2 years ago (2 children) If you have a variable called exit you've overwritten the function in that scope, and won't be able to execute it. e.g. >>> exit=1 >>> exit() Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not callable >>> permalink fedilink source parent hideshow 2 child comments replies: [–] EmergMemeHologram@startrek.website 17 points 2 years ago Reminds me of setting pi = 3 in my friends matlab subroutines in school. permalink fedilink source parent [–] peopleproblems@lemmy.world 5 points 2 years ago wow it does do that. cool permalink fedilink source parent
[–] EmergMemeHologram@startrek.website 17 points 2 years ago Reminds me of setting pi = 3 in my friends matlab subroutines in school. permalink fedilink source parent
[–] peopleproblems@lemmy.world 5 points 2 years ago wow it does do that. cool permalink fedilink source parent
[–] WhiskyTangoFoxtrot@lemmy.world 14 points 2 years ago (5 children) Guessing at what the programmer wants instead of implementing consistent behaviour is what Javascript does. Do you want Python to become Javascript? permalink fedilink source parent hideshow 5 child comments replies: [–] tetris11@lemmy.ml 5 points 2 years ago (4 children) Just once I want '1' + '2' to equal '3'. Is that so much to ask? permalink fedilink source parent hideshow 4 child comments replies: [–] Metype@lemmy.world 7 points 2 years ago (2 children) You want to remove the string concatenation operator? Cause that'll do it permalink fedilink source parent hideshow 2 child comments replies: [–] tetris11@lemmy.ml 9 points 2 years ago (1 child) I think every language needs a please operator, which acts to enforce human expectation of a statement: '1' + '1' ## evaluates to '11' please '1' + '1' ## evaluates to '2' permalink fedilink source parent hideshow 1 child comment replies: [–] PlexSheep@feddit.de 4 points 2 years ago I kinda like that permalink fedilink source parent [–] wewbull@feddit.uk 5 points 2 years ago Yes. Yes it is. permalink fedilink source parent
[–] tetris11@lemmy.ml 5 points 2 years ago (4 children) Just once I want '1' + '2' to equal '3'. Is that so much to ask? permalink fedilink source parent hideshow 4 child comments replies: [–] Metype@lemmy.world 7 points 2 years ago (2 children) You want to remove the string concatenation operator? Cause that'll do it permalink fedilink source parent hideshow 2 child comments replies: [–] tetris11@lemmy.ml 9 points 2 years ago (1 child) I think every language needs a please operator, which acts to enforce human expectation of a statement: '1' + '1' ## evaluates to '11' please '1' + '1' ## evaluates to '2' permalink fedilink source parent hideshow 1 child comment replies: [–] PlexSheep@feddit.de 4 points 2 years ago I kinda like that permalink fedilink source parent [–] wewbull@feddit.uk 5 points 2 years ago Yes. Yes it is. permalink fedilink source parent
[–] Metype@lemmy.world 7 points 2 years ago (2 children) You want to remove the string concatenation operator? Cause that'll do it permalink fedilink source parent hideshow 2 child comments replies: [–] tetris11@lemmy.ml 9 points 2 years ago (1 child) I think every language needs a please operator, which acts to enforce human expectation of a statement: '1' + '1' ## evaluates to '11' please '1' + '1' ## evaluates to '2' permalink fedilink source parent hideshow 1 child comment replies: [–] PlexSheep@feddit.de 4 points 2 years ago I kinda like that permalink fedilink source parent
[–] tetris11@lemmy.ml 9 points 2 years ago (1 child) I think every language needs a please operator, which acts to enforce human expectation of a statement: '1' + '1' ## evaluates to '11' please '1' + '1' ## evaluates to '2' permalink fedilink source parent hideshow 1 child comment replies: [–] PlexSheep@feddit.de 4 points 2 years ago I kinda like that permalink fedilink source parent
[–] Bronco1676@lemmy.ml 7 points 2 years ago* This is the code (Github link): class Quitter(object): def __init__(self, name, eof): self.name = name self.eof = eof def __repr__(self): return 'Use %s() or %s to exit' % (self.name, self.eof) def __call__(self, code=None): # Shells like IDLE catch the SystemExit, but listen when their # stdin wrapper is closed. try: sys.stdin.close() except: pass raise SystemExit(code) What happens is that the python repl calls __repr__ automatically on each variable/statement that you type into the repl (except assignments e.g. x = 1). But this basically only happens in the repl. So "executing" only exit wouldn't work in a python script as it is not calling __repr__ automatically, so better you learn how to do it right than using just exit in your python scripts and scratching your head why it works in the repl but not in your code. permalink fedilink source parent
[–] eluvatar@programming.dev 2 points 2 years ago Because python has strong opinions permalink fedilink source parent