New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use __builtin__ instead of __builtins__ #1

Closed
merwok opened this Issue Mar 15, 2014 · 4 comments

Comments

Projects
None yet
2 participants
@merwok

merwok commented Mar 15, 2014

The code written to turn import into accio relies on a detail of CPython instead of a defined part of the language: http://stackoverflow.com/questions/11181519/python-whats-the-difference-between-builtin-and-builtins

Excellent hack by the way :‑D

@amygdalama

This comment has been minimized.

Owner

amygdalama commented Mar 17, 2014

Thanks for the link! After reading the Python documentation, I still don't entirely understand the difference. I'll look into this today.

@merwok

This comment has been minimized.

merwok commented Mar 17, 2014

In short: the module that can be changed is named __builtin__; the other thing named __builtins__ is an implementation detail in CPython that you can mostly ignore, it’s not part of the Python language (i.e. your code is not guaranteed to work with other interpreters like PyPy if you use it).

@amygdalama

This comment has been minimized.

Owner

amygdalama commented Mar 17, 2014

Ah, I am beginning to understand. I'm not concerned with the code in this repository being dependent on CPython implementation details, because it is only intended to be used with my modified version of CPython. For example, you couldn't import the harrypotter.py script in this repo in another interpreter (even a muggle CPython interpreter) because the script uses accio instead of import. So this script will only work when interpreted by my version of CPython anyway.

However, I should totally use __builtin__ instead of __builtins__ in the harrypotter.py file for another reason! The script was initially created to be ran at PYTHONSTARTUP. However, if someone wanted to instead import, ahem, accio it, it wouldn't work, because in that case __builtins__ would be a dictionary and __builtins__.float, etc, would throw an AttributeError: 'dict' object has no attribute 'float'.

Thanks for bringing this up! It was fun to learn about.

@merwok

This comment has been minimized.

merwok commented Mar 17, 2014

It’s one of the unclean corners that the import system is full of :‑) Python 3.3 and 3.4 clean up some of these things.

Glad to help : )

@amygdalama amygdalama closed this Mar 17, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment