3 ms·
How about def __repr__(self): import warnings warnings.warn("Use exit() or Ctrl-Z plus Return to exit") return super().repr()
by dmurray 3y ago
How about
def __repr__(self):
import warnings
warnings.warn("Use exit() or Ctrl-Z plus Return to exit")
return super().repr()
- edflsafoiewq 3y agoWhat if you aren't in the REPL? Using __repr__ at all seems like an abstraction violation. It should be the REPL's job to layer on special casing for exit.
- dmurray 3y agoYou could use the same pattern as DeprecationWarning. It's suppressed by default and test runners like pytest enable it. A new ReplWarning could be enabled by interactive tools only. I think I agree though: what you really want to special case is the situation where the user types precisely 'exit<cr>' at a REPL prompt, and that hack needs to exist further up the stack than in the implementation of __repr__.
- amluto 3y agoThe actual behavior is indeed a bit ridiculous: >>> print('%r' % str) <class 'str'> >>> def whatisit(x): ... print('It is %r' % x) ... >>> whatisit(min) It is <built-in function min> >>> whatisit(exit) It is Use exit() or Ctrl-D (i.e. EOF) to exit Excuse me? IMO if the REPL wanted a friendly feature like this, it should be a generic REPL feature, not a hack applied to the function exit.
- dale_glass 3y agoAlso a bit funny: >>> def get_function(): ... return exit ... >>> a = get_function() >>> a Use exit() or Ctrl-D (i.e. EOF) to exit