

Replacing `import` with `accio`: A Dive into Bootstrapping and Python's Grammar - ThePhysicist
http://mathamy.com/import-accio-bootstrapping-python-grammar.html
Summary of Blog Post (I&#x27;m not the author):<p>At Hacker School, I&#x27;ve been building an alternate universe Python by overwriting builtin functions and statements with Harry Potter spells. This is a thing you can do at Hacker School!
======
NoodleIncident
This kind of stuff is why I read Hacker News. Completely pointless but
indisputably awesome technical hacks that teach you something about how stuff
works anyway.

~~~
sadfnjksdf
Awesome, but accio is a bit Potterish. How about 'have' or something shorter.

~~~
jacalata
You know its deliberately potter-ish, right? Did you miss the wingardium
leviosa reference at the beginning of the post?

~~~
sadfnjksdf
And avra kadabra to you also.

------
jodrellblank
I was expecting the magic to be changing

    
    
        import math
        x = math.ceil(y)
    

into

    
    
        x = accio ceil(y)
    

and having it Just Magically Work (tm).

But it's a nice write up of recompiling Python with a changed grammar.

~~~
lazzlazzlazz
I don't know how you could possibly have expected that.

~~~
spoiler
Hence the "magically" :-)

------
lawl
I'm not a python programmer. But shouldn't this be possible at runtime by
modifying the AST to change it back to import in the AST? That way you could
have a module you can just include to run this without a custom interpreter.

Edit: Looks like this is doable by messing with the tokenizer:
[http://stackoverflow.com/questions/214881/can-you-add-new-
st...](http://stackoverflow.com/questions/214881/can-you-add-new-statements-
to-pythons-syntax)

You could create a loader.py where you tokenize the script yourself, and
replace accio with a custom import that again loadds imports by tokenizing
them yourself first. And then after you've modified the tokens you should be
able to build a valid AST and run it :)

I'm pretty sure you can just throw a SyntaxError there yourself too if you
encounter a real "import".

Nice hack nevertheless.

~~~
anon4
Oh yes, you can. But once you're doing that, why stop at a single statement?
Why not macros
[https://github.com/lihaoyi/macropy](https://github.com/lihaoyi/macropy)

~~~
sitkack
Don't forget
[http://www.pocoo.org/projects/karnickel/#karnickel](http://www.pocoo.org/projects/karnickel/#karnickel)
the magnificent

------
varenc
its probably easier to add custom syntax to python by using custom encodings.
The dropbox pyxl library does this. Details:
[https://github.com/dropbox/pyxl#implementation-
details](https://github.com/dropbox/pyxl#implementation-details)

------
stcredzero
When I search for "Harry Potter Programming Language" I keep getting
references to an article where JK Rowling's writing process is used as an
analogy for the creation of Python.

~~~
Schiphol
Is there an interesting story behind that search? You got me wondering.

------
dalek2point3
this is such a great assignment for a principles of programming languages
class. there is no better way to learn something than to break it up, change
it and make it do your wish. i'm going to be teaching my next batch of muggles
this.

------
groovy2shoes
Reminds me in some ways of LUA:
[https://github.com/mniip/LUA](https://github.com/mniip/LUA)

------
lvh
Awesome hack!

Did you consider import hooks to accomplish this? Of course that wouldn't make
your bootstrap language any different; that'd still be vanilla python. In the
imported file you can do whatever you want, for example the inimitable paultag
implemented hy, a python lisp, using those.

------
j1z0
Wow. Such an awesome article. I really enjoyed reading it.

------
girvo
Really neat post, but a missed opportunity in naming it: "Basilisk" would have
been quite a nice fit with both worlds ;)

~~~
jebus989
Did you see she named it Nagini? Arguably even better

------
daGrevis
If you just want to accio act as import, you can always do this:

    
    
        accio = __import__
    

Anyway, interesting article!

~~~
jodrellblank
Read the article, that was discussed and dismissed. It doesn't make accio 'act
as import' because import is a keyword not a function call.

~~~
daGrevis
import is a statement. __import__ is a function hence will work.

    
    
        In [1]: accio = __import__
        
        In [2]: accio("os")
        Out[2]: <module 'os' from '/usr/lib/python2.7/os.pyc'>

~~~
raimondious
From the article...

    
    
        >>> accio = __builtins__.__import__
        >>> accio sys
          File "<stdin>", line 1
            accio sys
                    ^
        SyntaxError: invalid syntax
        
        # :(
    

The author wanted accio to replace import not __import__.

