What was wrong with "def CreateGapLease(self, lease_path='/leases/gap'):"? Immutable types like strings aren't a risk in default args. My version probably runs slightly faster and will produce much better automatically-extracted docs.
The related trap, which you might have been trying to avoid, looks like this:
def func(x=[]): . x.append(5) . return x
(Except without the silly dots to workaround blogger's formatting)
That program only has one list in it which will keep growing every time you call func(). That's where the "x=None .. if x is None: x = []" idiom is important.
I assume FLAGS.test_tmpdir is a randomly-named temporary directory (e.g. created with tempfile.mkdtemp), otherwise you still might have problems if you run the test suite concurrently and both instances try to write to the same file at the same time.
What was wrong with "def CreateGapLease(self, lease_path='/leases/gap'):"? Immutable types like strings aren't a risk in default args. My version probably runs slightly faster and will produce much better automatically-extracted docs.
ReplyDeleteThe related trap, which you might have been trying to avoid, looks like this:
def func(x=[]):
. x.append(5)
. return x
(Except without the silly dots to workaround blogger's formatting)
That program only has one list in it which will keep growing every time you call func(). That's where the "x=None .. if x is None: x = []" idiom is important.
Nice post.
ReplyDeleteDo yuo have some example of good file system mocking? :)
I assume FLAGS.test_tmpdir is a randomly-named temporary directory (e.g. created with tempfile.mkdtemp), otherwise you still might have problems if you run the test suite concurrently and both instances try to write to the same file at the same time.
ReplyDelete