The vast majority of pythoners use None, but I prefer descriptive strings for default values, like this:
from datetime import datetime, timedelta
def midnight_next_day(initial_time="use today's date"):
if initial_time == "use today's date":
initial_time = datetime.now()
return initial_time.date() + timedelta(days=1)
Pylint doesn’t like it though:
$ pylint -e f.py
No config file found, using default configuration
************* Module f
E: 10:midnight_next_day: Instance of 'str' has no 'date' member (but some types could not be inferred)
I changed from using a string as the default to None, and then pylint
didn’t mind:
$ cat f.py
from datetime import datetime
def midnight_next_day(initial_time=None):
if initial_time is None:
initial_time = datetime.now()
return initial_time.date() + timedelta(days=1)
$ pylint -e f.py
No config file found, using default configuration
Checking where a variable is None is probably much faster than doing string comparison, but I find the string more informative.
Looking for comments. I already posted this to comp.lang.python here and got some good feedback.
Add New Comment
Viewing 8 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Add New Comment