Passing a function as an argument from within the same class?

zealalot zealalot at gmail.com
Fri May 1 10:35:40 EDT 2009


So, I'm trying to come up with a way to pass a method (from the same
class) as the default argument for another method in the same class.
Unfortunately though, I keep getting "self not defined" errors since
the class hasn't been read completely before it references itself.

Is there a better way of doing this?

--- CODE ---

class SomeClass():
    def doNothing(self):
        pass
    def function1(self):
        print "Running function 1."
    def function2(self, passedFunction=self.doNothing):
        print "Running passed function."
        passedFunction()

someObject = SomeClass()
someObject.function2(someobject.function1)

--- CODE ---

Thanks,
- Zealalot



More information about the Python-list mailing list