Rationale for read-only property of co_code
João Neves
sevenjp at gmail.com
Wed Apr 2 12:06:58 EDT 2008
On Apr 2, 4:35 pm, castiro... at gmail.com wrote:
> Are Python bytes codes Python byte codes?
I'm not quite sure I understood your question, sorry.
> Do you foresee any machine-dependent optimizations?
In my personal case, I am not looking for optimizations in the
generated bytecode.
Let me give a very basic example. Say we have these two functions:
def inc(x):
x = x + 1
def dec(x):
x = x - 1
Examining the compiled bytecodes for these two functions:
>>> inc.func_code.co_code
'|\x00\x00d\x01\x00\x17}\x00\x00d\x00\x00S'
>>> dec.func_code.co_code
'|\x00\x00d\x01\x00\x18}\x00\x00d\x00\x00S'
Now suppose that I wanted to mess with inc, and have it behave like
dec.
For that, I would like to do something like this, for instance:
>>> inc.func_code.co_code = '|\x00\x00d\x01\x00\x18}\x00\x00d\x00\x00S'
Of course, as of this moment, I get a TypeError exception, because
co_code is read-only.
The thing I've been wondering is why _is_ it read-only? In what
circumstances having write access to co_code would break the language
or do some other nasty stuff?
João Neves
More information about the Python-list
mailing list