Default parameter for a method... again

s0suk3 at gmail.com s0suk3 at gmail.com
Wed Apr 16 12:26:03 EDT 2008


I had posted this before but all the spam whipped it out...

I wanted to know if there's any way to create a method that takes a
default parameter, and that parameter's default value is the return
value of another method of the same class. For example:

class A:
    def __init__(self):
        self.x = 1

    def meth1(self):
        return self.x

    def meth2(self, arg=meth1()):
        # The default `arg' should would take thereturn value of
meth1()
        print '"arg" is', arg

This obviously doesn't work. I know I could do

...
    def meth2(self, arg=None):
        if arg is None:
            arg = self.meth1()

but I'm looking for a more straightforward way.



More information about the Python-list mailing list