0

The following code works for providing a meaningful default value for conns but it is quite ugly:

def query(s,sql,conns=None):
  conns = s.conns() if not conns else conns

A preferred construct would be:

def query(s,sql,conns=s.conns()):

But that is apparently disallowed: I am inferring that instance method invocations are not supported within invocations of other methods on the same instance.

Is there a cleaner pattern for supplying the default/missing parameter?

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • Just use `if conns is None: conns = s.conns()`, you don't need the conditional expression. It's not that it's "not supported", exactly, but think about when the default value is evaluated - the class isn't fully defined, let alone an instance created. – jonrsharpe Mar 27 '20 at 16:26
  • Dupe: https://stackoverflow.com/q/8131942/3001761 – jonrsharpe Mar 27 '20 at 16:27
  • The construct you show of course I considered but it is effectively 2 lines after proper formatting: it's noise. Well I guess there really is not a better pattern. – WestCoastProjects Mar 27 '20 at 16:42

0 Answers0