[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: Python/swig bindings.

From: Greg Stein <gstein_at_lyra.org>
Date: 2004-10-08 09:33:34 CEST

On Sat, Oct 02, 2004 at 12:32:04PM -0400, Greg Hudson wrote:
> On Fri, 2004-10-01 at 12:21, C. Michael Pilato wrote:
> > 3. Callback function/baton pairs get reduced to just callback
> > functions, and the benefit you get from batons is gotten
> > instead through Python default arguments:
> [...]
> > def function(callback_arg1, ..., userdata1=whatever, ...):
>
> Is the use of default arguments really necessary? Can't you just put
> the "whatever" in the body of function? In my tests, an inner function
> seems to be able to refer to the outer function's local variables just
> fine; for instance, the following prints out "5":
>
> def call(func):
> x = 3
> func()
>
> def function():
> x = 5
> def subfunction():
> y = x
> print y
> call(subfunction)
>
> function()

That only works for newer versions of Python (2.2 and later?). They can do
lexical scoping. Older versions could not, so you had to pass outer
variables into the function through default args.

Mike's point about using default args works, but I tend to use class
instances to hold the state:

  class Foo:
    def __init__(self, state):
      self.state = state
    def callback(self, arg1):
      return arg1 + self.state

  f = Foo(10)
  register_callback(f.callback)

The "baton" is the instance itself. Through the "self" variable, I can
access any info needed.

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Fri Oct 8 09:42:51 2004

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.