En Wed, 02 Apr 2008 17:54:33 -0300, João Neves <[EMAIL PROTECTED]>  
escribió:

> On 2 Abr, 21:38, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
>> There is no need to overwrite co_code. Create a new code object with
>> your desired bytecode and use that instead.
>
> Yes, it may work (haven't tested - isn't there any problem with stuff
> like co_name, for instance?), but for simplicity's sake, wouldn't it
> be far more convenient if you could just write over co_code? :)
> In the end, it's all a matter of convenience, I guess.

Functions aren't just code - they contain the environment needed to  
actually call the code. But they're easy to create given a code object:

py> import new
py> new.function
<type 'function'>
py> help(new.function)
Help on class function in module __builtin__:

class function(object)
  |  function(code, globals[, name[, argdefs[, closure]]])
  |
  |  Create a function object from a code object and a dictionary.
  |  The optional name string overrides the name from the code object.
  |  The optional argdefs tuple specifies the default argument values.
  |  The optional closure tuple supplies the bindings for free variables.

py> import types
py> types.FunctionType is new.function is type(lambda:0)
True

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to