Re: Writing func_closure?

2005-06-09 Thread Michael Hoffman
Fernando Perez wrote:

 Yes, I knew of the new.function() approach, but the problem is that I don't 
 know
 how to make a fresh closure for it.  I can reuse the closure from a different
 function, but the docs don't say how to make a valid closure tuple.

  def makeclosure(x):
... def _f():
... x
... return _f.func_closure
...
  makeclosure(42)
(cell at 0x4613bc: int object at 0x4a3e4c,)

OK, that's kind of limited. God only knows what happens when there is 
more than one variable, although you could add extra levels of hackery 
to do with that.

 Many thanks though, I'll probably end up using a less dirty hack :)

*Excellent* idea.
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing func_closure?

2005-06-08 Thread Michael Hoffman
Fernando Perez wrote:

  I am  trying to do a run-time modification of a function's closure,
  where I want to modify the value of one of the variables in the closure.

Out of curiosity, why?

 In [21]: def wrap(x):
: def f(y):
: return x+y
: return f
:
 
 In [22]: f1=wrap('hello')
 
 In [23]: f1.func_closure
 Out[23]: (cell at 0x4168bcd4: str object at 0x41bc0080,)
 
 My question is, how can I create one of these cell objects to stuff into the
 closure (I want to do this from pure Python, not C extensions).

  f1.func_closure[0].__class__()
Traceback (most recent call last):
   File stdin, line 1, in ?
TypeError: cannot create 'cell' instances

Hmmm, that didn't work so well.

  f1.func_closure = f1.func_closure
Traceback (most recent call last):
   File stdin, line 1, in ?
TypeError: readonly attribute

Closer inspection of the docs http://docs.python.org/ref/types.html 
reveals that it is not writable after all. Therefore the only way I can 
see to do it without writing an extension is to generate some dummy 
function and copy the func_closure attribute from it. Luckily, you have 
already produced a factory for such a function:

  f1( there)\
'hello there'
  _f2 = wrap(howdy)
  f1 = new.function(f1.func_code, f1.func_globals, f1.func_name, 
f1.func_defaults, _f2.func_closure)
  f1( there)
'howdy there'

Mix-and-match functions! What will they think of next?
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing func_closure?

2005-06-08 Thread Fernando Perez
Michael Hoffman wrote:

 Fernando Perez wrote:
 
   I am  trying to do a run-time modification of a function's closure,
   where I want to modify the value of one of the variables in the closure.
 
 Out of curiosity, why?

Oh, I was just trying to play a little trick inside a tight loop where I would
modify on the fly the function's closure to change a parameter.  I can do it in
a million ways, but at creation time, the closure approach provides the
cleanest syntax.  But at runtime, I have an algorithm that needs to modify
certain parameters many times, and the least-expensive way would be to be able
to write directly into the closure.

 Closer inspection of the docs http://docs.python.org/ref/types.html
 reveals that it is not writable after all. Therefore the only way I can

Ah, the docs have improved.  I'm using 2.3.4, and the same page:

http://www.python.org/doc/2.3.4/ref/types.html

only says:

Of these, func_code, func_defaults, func_doc/__doc__, and func_dict/__dict__ may
be writable; the others can never be changed.

That's what led me to believe it could be done. Thanks for pointing me to the
2.4 docs, which are much less ambiguous.


 see to do it without writing an extension is to generate some dummy
 function and copy the func_closure attribute from it. Luckily, you have
 already produced a factory for such a function:

Yes, I knew of the new.function() approach, but the problem is that I don't know
how to make a fresh closure for it.  I can reuse the closure from a different
function, but the docs don't say how to make a valid closure tuple.  This is
the typical problem of the stdlib docs, which under-specify what is supposed to
go into a call and don't give at least a specific example.

Many thanks though, I'll probably end up using a less dirty hack :)

Cheers,

f

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


Re: Writing func_closure?

2005-06-08 Thread Greg Ewing
Fernando Perez wrote:

 I can reuse the closure from a different
 function, but the docs don't say how to make a valid closure tuple.  This is
 the typical problem of the stdlib docs, which under-specify what is supposed 
 to
 go into a call and don't give at least a specific example.

As far as I know, there is currently no supported way
of directly creating or modifying cell objects from Python;
it can only be done by some obscure trickery. So the docs
are telling the truth here, in a way. :-)

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,   
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing func_closure?

2005-06-08 Thread Fernando Perez
Greg Ewing wrote:

 As far as I know, there is currently no supported way
 of directly creating or modifying cell objects from Python;
 it can only be done by some obscure trickery. So the docs
 are telling the truth here, in a way. :-)

In a twisted, convoluted way :)

But thanks for the clarification (which IMHO belongs in the docs).  Oh well, it
was a dirty hack anyways, so it's probably best not done.

Cheers,

f

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