问题
I want to remove doc strings from a Python program but leave asserts (and __debug__
sections) in. I have been using the -OO flag to generate .pyo files but according to the documentation that removes both asserts and doc strings.
I'm using CPython 2.7.
Clarification: I'm removing the docstrings as a cheap obfuscation method. Management made that decision and I think whether that is a useful thing to do is outside the scope of this question.
回答1:
You can't have half of -O
. It removes docstrings and asserts, that's just the way it works. Asserts are usually written to be optional anyway, that is, you can remove them without affecting the behavior of the program.
If I had to remove docstrings and leave all else alone, I would consider writing a tool that opened .pyc files, unmarshaled the contents, modified them (by removing the docstrings), and repacked them as .pyc files. But that would likely be a delicate and fragile tool, if it can even be done that way.
来源:https://stackoverflow.com/questions/11496532/remove-doc-strings-but-not-asserts-from-compiled-cpython