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

CTO debatem1 at gmail.com
Fri May 1 13:26:12 EDT 2009


> Careful, bearophiles' answer remains the best one.
>
> The only reason your example worked is that you had already had
> SomeClass defined (probably from a previous experiment).

Scott is correct, and if bearophile and I ever give you conflicting
advice, take bearophile's.

A (corrected) bit of code that might serve your purpose would be
as follows:

class SomeClass:

    def doNothing():
        pass

    def function1(self):
        print "running function 1"

    def function2(self, passedFunction=doNothing):
        print "running passed function"
        passedFunction()

again, though- bearophile's is the best, and most standard,
answer here



More information about the Python-list mailing list