I've been learning python recently. I'm just getting started so I'm running into all the stupid beginner problems, for instance the insane behaviour of default argument values. Take this example:
from datetime import datetime
import time
class Weird:
def __init__(self, dt = datetime.now()):
self.dt = dt
w1 = Weird()
time.sleep(1)
w2 = Weird()
w3 = Weird(datetime.now())
print w1.dt == w2.dt # True
print w1.dt == w3.dt # False
The
datetime in the
w1 and
w2 objects will actually be the same since the default argument value in the method definition is
only evaluated once! What!?!? This essentially renders default arguments useless since you always end up writing the following:
class Weird:
def __init__(self, dt = None):
self.dt = dt or datetime.now()
I don't understand why you would design a default argument values features in your language like this. Doing it this way certainly violates the
principle of least astonishment!