How to Generate Bytecode for Python Classes Across Multiple Versions?

0
1
Asked By CreativeCoder87 On

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)

4 Answers

Answered By ClarifyingCat On

Just a thought—obfuscated code can be pretty tricky to work with. It might complicate things more than help them understand the basics.

Answered By CSharpie On

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.

Answered By BytecodeBuff On

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.

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.

Answered By StraightTalker22 On

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!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.