How to Generate Bytecode for Python Classes Across Multiple Versions?
Asked by
CreativeCoder87
Hey everyone! I'm looking to generate bytecode (.pyc files) for a class called Foo in Python. I've also got another class, Foo2, that inherits from Foo. My idea is to share the Foo2 source code along with the bytecode for Foo so that my students can use the methods from Foo2 without actually seeing the implementation of Foo. This would help them focus on certain tasks while comparing their work with the parent class methods. The challenge I'm facing is the need to create bytecode compatible with various Python versions. Does anyone know a good way to generate the bytecode for different Python versions? Or do you have any alternative suggestions? I've included a simple example of my code: [https://godbolt.org/z/WdcWsvo4c](https://godbolt.org/z/WdcWsvo4c)
Answers
Honestly, why not just tell your students to treat foo.py as a third-party library? For this lesson, they should only modify foo2.py and stick to the specs. Itโs also a learning experience to follow instructions!
Reply to this answer
You might want to check out PyArmor. It's a handy tool for obfuscating your Python scripts, and it could work well for your scenario! It allows you to distribute your Foo2 class without revealing Foo's code, while still working with various Python versions. You can find it on GitHub: https://github.com/dashingsoft/pyarmor.
Reply to this answer
If you really need to go this route, consider rewriting the main library in C or using Cython 3.1+. This way, you can compile one library per platform and keep compatibility across Python versions.
Reply to this answer
Just a thoughtโobfuscated code can be pretty tricky to work with. It might complicate things more than help them understand the basics.
Reply to this answer
Add your answer
Answers are reviewed before publication.
StudentSeeker99: Interesting, I didn't know that! I'll definitely take a look!
HelpfulHarry: Just a heads up though, PyArmor has similar restrictions as bytecode itselfโit means that if you obfuscate code for Python 3.8, itโll only run on 3.8.x and won't work on earlier or later versions.