Home > OS >  Common default arguments in class methods
Common default arguments in class methods

Time:06-16

I created a class where 3 arguments are always the same by default:

    class Test:
        def f1(a, b, verbose=True, delim='\t', lvl=0):
            ...
        def f2(c, verbose=True, delim='\t', lvl=0):
            ...
        def f3(d, e, f, verbose=True, delim='\t', lvl=0):
            ...

Sometimes of course, I need f2 with lvl=1 for instance, so I need some flexibility, while the default should always be lvl=0 for each function I create. I was wondering if there was a more elegant way to do it than what I just wrote.

CodePudding user response:

I'd leave it as is. You could store the defaults outside of the class, for example:

class TestDefaults:
    verbose = True
    ...

and then use verbose=TestDefaults.verbose.

  • Related