Hacker News new | ask | show | jobs
by iandanforth 4765 days ago
I love python and this is awesome. But I HATE magic global objects. 'ctx' seems to be at the heart of everything but is never explicitly passed into the scope or imported.

This should really be fixed.

Also "from html import *"? No no no. Never, not even once.

2 comments

The ctx global and 'from html import *' are just part of the demo code. Look at line 289 of the HTML file where ctx is initialized. It's not some magic thing baked into Brython, it's just sample code. Of course you could write either of these in any way you prefer.
A demo of a new way to use a programming language shouldn't be full of these "Oh, that crap is just for the demo, it would look better in practice." It's a programming language demo, show us something that makes us want to use it.
I agree completely: sample/demo code should be the best you can make it. And I'm sure the Brython authors would welcome a pull request that improves their demo code.

But the comment I replied to was a bit misleading (no doubt unintentionally): it made two quite valid complaints about coding practices, without mentioning that these are only in the demo code.

When I read that comment I thought, "WTF? Brython injects a magic 'ctx' global into your Python code?" It doesn't, so I thought it was worth pointing out that these are just sample code issues that wouldn't affect any code you write in Brython.

"ctx" is a very common name for the "context" with which you work when doing stuff in <canvas>. There's nothing magical about it.

Also it's defined at the bottom of the script which, I admit, is a bit weird:

    if hasattr(canvas,'getContext'):
        ctx = canvas.getContext("2d")

from html import * looks bad, I agree, but I believe they did that to allow us to play with their toy from the console.