'bind' functions into methods
Oren Tirosh
oren-py-l at hishome.net
Sun Oct 20 02:41:05 EDT 2002
On Sun, Oct 20, 2002 at 12:04:15AM +0000, TeaAndBikkie wrote:
> I have a set of functions, op1(), op2(), etc. with initial argument 'n' the
> same.
>
> I want to "bind" them into a class as methods that pass the first argument from
> an instance variable.
> ... sample code, on Python 2.2.1 ...
>
> def op1(n, dude):
> print "[[[%d -> %d]]]" % (n, dude)
>
> def op2(n, eric, fish):
> print "[[[%d -> %d :: %d]]]" % (n, eric, fish)
>
> class tobj:
> def __init__(self, n=None):
> self.n = n
> def op1(self, *arg, **kwargs):
> return op1(self.n, *arg, **kwargs)
> def op2(self, *arg, **kwargs):
> return op2(self.n, *arg, **kwargs)
def bind(function, arg1):
def bound(*args, **kwargs):
return function(arg1, *args, **kwargs)
return bound
class tobj:
def __init__(self, n):
self.op1 = bind(op1, n)
self.op2 = bind(op2, n)
Python 2.2: works
Python 2.1: requires "from __future__ import nested_scopes"
Python <=2.0: sorry
Oren
More information about the Python-list
mailing list