![K. Lange](/assets/img/avatar_default.png)
Unlike in Python, I'm taking the approach of evaluating these at function call time rather than definition time. Assigning things like empty lists/dicts to default arguments has always been a ridiculous thing in Python, and I don't want to make that mistake. I'm pretty sure Python only continues to do that because it was something they didn't want to break for backwards compatibility reasons even in Python 3.
24 lines
470 B
Plaintext
24 lines
470 B
Plaintext
def foo(default="bacon"):
|
|
print "You like",default,"right?"
|
|
|
|
foo()
|
|
foo("sports")
|
|
|
|
def fillValues(a=1,b=2,c="c",d=None,e=2.71828):
|
|
print a,b,c,d,e
|
|
|
|
fillValues(b=True)
|
|
fillValues(c="test",a="one",e=object)
|
|
|
|
# Not like in Python! This is absolutely an anti-feature in Python.
|
|
def alwaysAFreshList(l=[]):
|
|
print "l=",l
|
|
l.append(1)
|
|
print "l*=",l
|
|
|
|
alwaysAFreshList()
|
|
alwaysAFreshList()
|
|
alwaysAFreshList([1,2,3])
|
|
alwaysAFreshList([1,2,3])
|
|
alwaysAFreshList()
|