vendredi 28 février 2020

Returning different number and types of values?

Often I need to have function/method return different number OR types of values.

def test(x,y,ret='both') :
  s = x + y
  m = x * y
  if ret = 'both' : return s, m
  if ret = 'sum' : return s
  if ret = 'mult' : return m

Also sometimes I want to have Aspect type behavior where I inject debugging code, which may also change the return types/num-of-vals

Is there a standard Programming pattern that handle this ? Decorators ?

The example work but is abit clunky..

Aucun commentaire:

Enregistrer un commentaire