I'm searching for a good way to pass default values for arguments to a calling function. Let me explain this by example:
def greet(name, greeting='Hello', punctuation='!'):
return greeting+' '+name+punctuation
def greet_with_hi(name, punctuation='!'):
return greet(name, 'Hi', punctuation)
This is an example without use but here's my question: How can I omit the default value for the puncuation
argument of greet_with_hi
? A default value for this argument is already defined in greet
.
Solutions I can think of:
- let it as it is, cons: changing the default value must be done on every function definition instead of only changing it in one place
- use
None
as value for default argument and handleNone
insidegreet
, cons: special handling - use a global "constant" like
DEFAULT_PUNCTUATION='!'
and use this as value for the default argument, cons: seems not to be very pythonic
I haven't seen a common pattern in APIs and other code. How would you handle this pattern?
Aucun commentaire:
Enregistrer un commentaire