[Tutor] Help refactoring this code

D. Guandalino guandalino at gmail.com
Mon Oct 10 00:41:03 CEST 2011


Hi, in this class many methods share common code.

class Foo(object):
    def m1(self):
        if self.foo:
            pass # m1 do something
        elif self.bar:
            pass # m1 do something else
        else:
            pass # m1 do something different

    def m2(self):
        if self.foo:
            pass # m2 do something
        elif self.bar:
            pass # m2 do something else
        else:
            pass # m2 do something different

    # def m_n(self):
        pass # same conditionals, different implementation

I'm wondering if I have to duplicate the if/elif/else for each method.
Does Python 2.7 provide a way to factor out this common structure?


More information about the Tutor mailing list