Python Forum
why globals() exits python when quit() is aliased as q
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why globals() exits python when quit() is aliased as q
#1
I was trying to set up my python so that typing 'q' or 'quit' (instead of 'quit()') will exit the python interpreter, by doing:
type(quit).__repr__ = type(quit).__call__
q=quit
But then I noticed that if I do 'globals()', it will exit the python. Can anybody tell me why this happens? Thanks!

abc12@kc-lenovo@~
26 $ python
Python 3.9.10 (main, Jan 20 2022, 22:28:26)
[GCC 11.2.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
Could not open PYTHONSTARTUP
FileNotFoundError: [Errno 2] No such file or directory: '/home/mobaxterm/kdrive/misc/dot.pythonrc.py'
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> type(quit).__repr__ = type(quit).__call__
>>> q=quit
>>> globals()

abc12@kc-lenovo@~
27 $
buran write May-23-2023, 06:39 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
This is a duplicate thread. The other has been (soft) deleted.
FYI: Please read: https://python-forum.io/misc.php?action=help&hid=22
Reply
#3
You remapped quit.__repr__() to call quit(). Then you assign a variable (q) to reference quit. When you type globals() it prints q: q.__repr__(), but __repr__() now does quit() instead of returning a string. Boom! I mean, that is why typing "q" works, why wouldn't you expect the same thing for globals().
Reply
#4
Ctrl+d is not much harder to type than q+Return ...
abc12346 likes this post
Reply
#5
You could insert a q object in __builtins__ instead of __globals__
# startup.py
class Q:
    def __repr__(self):
        self()

    def __call__(self):
        quit()

    def __str__(self):
        return f'<{type(self).__module__}.{type(self).__name__} object at {hex(id(self))}>'

__builtins__.q = Q()
del Q
Now you can print globals(), but if you print vars(__builtins__), it will exit the interpreter.

Output:
λ PYTHONSTARTUP=paillasse/pf/startup.py python Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> print(q) <__main__.Q object at 0x7f0b751f1b10> >>> q λ
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to avoid exec(), globals(), locals(), eval() paul18fr 10 5,084 Apr-21-2021, 05:53 PM
Last Post: snippsat
  accessing globals from a function Skaperen 8 4,162 Sep-01-2018, 11:50 PM
Last Post: Skaperen
  snake game quit() ^ SyntaxError: invalid syntax arti 1 4,047 Jan-17-2018, 06:26 PM
Last Post: buran
  Use of Globals CWatters 5 3,856 Nov-13-2017, 09:00 PM
Last Post: CWatters
  Globals vs Function mcmxl22 2 3,363 Feb-04-2017, 11:38 AM
Last Post: ichabod801
  Check if an duplicate exits in a list Server94 9 7,140 Jan-31-2017, 04:56 PM
Last Post: Server94

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020