1

I was wondering if there was a more efficient way of doing:

class myClass():

    def __init__(self):
        self.defaultVal = "default"

    def myMeth(self, valToDefault=None):
        if valToDefault is None:
            valToDefault = self.defaultVal
        else:
            ...

This gets repetitive and I'm sure there is a better method of doing it that I'm just not thinking of.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
john doe
  • 27
  • 3
  • 1
    `valToDefault = self.defaultVal if valToDefault is None else valToDefault`? Or maybe `valToDefault = valToDefault or self.defaultVal`, if you want to replace any false-y value. But what do you do in the `else` case? Also note [the style guide](https://www.python.org/dev/peps/pep-0008/) on naming conventions. – jonrsharpe May 12 '17 at 22:09
  • 2
    `*args` and best of all would be `**kwargs` i recon, you could make a wrapper that traverse a default map of sorts and that could be a solution. – Torxed May 12 '17 at 22:14

0 Answers0