PyCon JP 2024

ロケール設定が保存されました。pretalxでは英語のサポートが充実していると思っていますが、問題やエラーが発生した場合は、ぜひご連絡ください。

Crimes with the Python syntax
2024/09/27 , 4F Track4

Over the past few years digging into Python's runtime and language features, I've often run into some weird, obscure parts of the language. Very weird and obscure parts.

Such as imports being hackable enough to directly import JSON files. Or being able to change the syntax of the language, at runtime.

And I'd note them down. All the weird, esoteric, not-to-be-named features of the language. And eventually, I'd find some way to abuse that feature. Do something heinous. Write code that would make any code reviewer question my sanity. Committing crimes, with the Python syntax.

This talk is an explainer of all these features, and why you should never use them. Unless, of course, you need to.


Outline:

  • Introduction (2 minutes)
  • The convoluted Python import machinery (5 minutes)
  • Frame-hacking, and the sys._getframe() API. (5 minutes)
  • Tracers, debuggers, and changing your code at runtime. (5 minutes)
  • Bonus: goto statements in Python? (3 minutes)
  • Creating our own mini language inside Python, at runtime. (7-8 minutes)
  • Abusing the Python startup machinery to run arbitrary code. (7-8 minutes)
  • The end, and questionnaire. (10 minutes)

この題材を選んだ理由やきっかけ

Python on the surface may seem like a very simple, beginner friendly language, but underneath the familiarity, it is an extremely powerful, complex tool. And learning about the inner workings of the interpreter, and how you can tweak it for your use cases allows you to build amazing Python projects and libraries.

オーディエンスが持って帰れる具体的な知識やノウハウ

Learning about the interpreter, the stages it takes to read and run your code, and how you can modify each step of the process.

オーディエンスに求める前提知識

Understanding Python programs, and having at least a year or so of Python development experience would be great for following along.

オーディエンスの経験レベル

Advanced

発表の言語

English

発表資料の言語

English

Long-term Python developer, author, OSS contributor and speaker, Tushar currently works as a Software Engineer building language tools at DeepSource.

When not working, he tries to contribute to the Python developer tools ecosystem, like black, ruff, flake8 and mypy.