Matthew Wilson <m...@tplus1.com> writes: > Here's the code that I'm feeding to pylint: > > $ cat f.py > from datetime import datetime > > def f(c="today"): > > if c == "today": > c = datetime.today() > > return c.date() > > > And here's what pylint says: > > $ pylint -e f.py > No config file found, using default configuration > ************* Module f > E: 10:f: Instance of 'str' has no 'date' member (but some types could > not be inferred) > > Is this a valid error message?
Yes. Mentally run through your code and ask “what happens if the condition for the ‘if’ statement is false?” > Is the code above bad? If so, what is the right way? Yes, it's bad: * The function name ‘f’ is completely unhelpful. Consider a reader of the function who has no access to the inside of your head: Your function should be named, preferably, as a verb phrase, to say what the function *does* when it is called. * The parameter name ‘c’ is completely undescriptive. Again, consider a reader ignorant of your thinking: You should name parameters so they help the reader know what the parameter is supposed to be and how it will be interpreted. * You're re-binding the parameter name ‘c’ to something different within the function: it starts out bound to the input string, but by the time the function ends you're expecting it to be bound to a datetime object. Instead, you should be binding a *different* name to the datetime object you create inside the function, and using that for the return statement. -- \ “Read not to contradict and confute, nor to believe and take | `\ for granted … but to weigh and consider.” —Francis Bacon | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list