News for hackers. Stuff that matters.
Re:Python: Common Newbie Mistakes, Part 1 by nknighthb on Saturday July 06, 2013 @08:43PM (#6000580)
A simple fix to the time/now example is also useful for demonstrating first-class functions:

    def print_now(now=time.time):
        print now()
Re:Python: Common Newbie Mistakes, Part 1 by herge on Sunday July 07, 2013 @02:12PM (#6002818)
The classic way is:

    def print_now(now=None):
        now = now or time.time()
        print now
Re:Python: Common Newbie Mistakes, Part 1 by mnordhoff on Sunday July 07, 2013 @06:57PM (#6003960)
I hope "now" is never 1970-01-01 00:00:00.

Edit:

    if now is None:
        now = time.time()
Re:Python: Common Newbie Mistakes, Part 1 by herge on Monday July 08, 2013 @04:49AM (#6005802)
Or even more paranoid:

import mock

def print_now(now=mock.sentinel.now): now = now if now != mock.sentinel.now else time.time()

Re:Python: Common Newbie Mistakes, Part 1 by mnordhoff on Tuesday July 09, 2013 @03:21AM (#6011541)
I don't have experience with mock, but I've used object() -- literally -- as a sentinel.

    _sentinel = object()

    def print_now(now=_sentinel):
        if now is _sentinel:
            now = time.time()
        ...
Re:Python: Common Newbie Mistakes, Part 1 by DrJ on Saturday July 06, 2013 @09:57PM (#6000749)
if I recall, this is how SQLAlchemy does default values that are functions.
Re:Python: Common Newbie Mistakes, Part 1 by ajanuary on Sunday July 07, 2013 @04:56AM (#6001519)
You do then have to wrap values in lambdas if you want to pass them in.