[Tutor] CONSTANTS -- Is it appropriate to use uppercase names within a function or method?
boB Stepp
robertvstepp at gmail.com
Tue Nov 12 19:31:32 EST 2019
On Tue, Nov 12, 2019 at 5:47 PM Alan Gauld via Tutor <tutor at python.org> wrote:
>
> On 12/11/2019 21:00, boB Stepp wrote:
> > IF I were to use type annotations as you suggest, would there be any
> > point to the more expansive docstring? The combination of type
> > annotations with already very descriptive variable names seems to make
> > any more description superfluous.
>
> I'm not a big type hints fan so don't use them...
I think I may become a fan, though a year or two ago I had a different
mindset. Now that I have added mypy linting to my editor -- which
occurs as I type -- your comment inspired me to force it to give me an
error. So I took Mats suggestion:
21 def get_days_to_goal(goal_date_obj: date, start_date_obj: date =
None) -> int:
22 """Return the number of days to achieve a goal.
23
24 goal_date_obj: A date object giving the date by which the
goal should be
25 attained.
26
27 start_date_obj: A date object giving the start date for further
28 calculations.
29 """
And added this which before I barely hit <Enter> generated an error:
>> 30 goal_date_obj = 1
And at the bottom of the editor in a QuickFix window:
1 pages_per_day.py|30 col 21 error| Incompatible types in assignment
(expression has type "int", variable has type "date")
which showed up in a nice bright orange color due to my recently added
Airline plugin.
I have to say, I think I'm game for a little extra typing if (1) It
makes the function definition and docstring more informative to a
later code reader; and (2) It so quickly flags a potentially bad error
without even having to run a test or compile the code.
--
boB
More information about the Tutor
mailing list