James Franco James Franco - 1 year ago 51
Python Question

is there a way to bind parameter to a function variable in python?

Consider the following example:

class foo:
def __init__(self):
print ("Constructor")
def testMethod(self,val):
print ("Hello " + val)
def test(self):
ptr = self.testMethod("Joe") <---- Anyway instead of calling self.testMethod with parameter "Joe" I could simple bind the parameter Joe to a variable ?

k = foo()

In the defintion
is it possible for me to create a variable which when called calls the method
with the parameter

Answer Source

You could pass a name to the constructor (and store it on the class instance), this is then accessible to methods:

class Foo:
    def __init__(self, name):
        self.name = name

    def testMethod(self):
        print("Hello " + self.name)

    def test(self):

As follows:

k = Foo("Joe")
k.test()          # prints: Hello Joe