Skip to content Skip to sidebar Skip to footer

How To Bind A Constant Parameter In Python?

I have got a class similar to class C: def __init__(self, a): self.a = a def noParam(self): return self.a def withParam(self, b) return self.a

Solution 1:

You want to use functools.partial:

importfunctoolsinstC= C(5.)
meth = functools.partial(instC.withParam, 239)

Then you can pass this method (bound with both your instance and the value 239):

do_thing(meth)

Solution 2:

You can use lambda functions to bind parameters:

meth = lambda: instC.withParam(239)

Post a Comment for "How To Bind A Constant Parameter In Python?"