[Tutor] CONSTANTS -- Is it appropriate to use uppercase names within a function or method?

boB Stepp robertvstepp at gmail.com
Tue Nov 12 12:43:05 EST 2019


As I begin to play around with my newly installed editor tools, pylint
does not like the following:

   def get_days_to_goal(goal_date_obj, start_date_obj=None):
--    """Given a start date object and a goal date object, return the
number of
      days it will take to attain a goal, counting the start date as
one of these
      days.

      start_date_obj:  A date object giving the start date for further
          calculations.

      goal_date_obj:  A date object giving the date by which the goal
should be
          attained.

      days_to_goal:  An integer giving the number of days to achieve the goal,
          counting today as one of those days.
      """

      if not start_date_obj:
          start_date_obj = date.today()

--    COUNT_TODAY_TOO = 1
      days_to_goal = (goal_date_obj - start_date_obj).days + COUNT_TODAY_TOO

      return days_to_goal

Please ignore the flagging of the function docstring.  I need to
reread the docstring PEP.  Apparently I need to make the first line of
the docstring a short one-line summary sentence ending in a period.

What I am asking about is the flagging of "COUNT_TODAY_TOO = 1"  At
the time I wrote this I did not want a magic number "1" to appear in
the line beginning "days_to_goal = ..."  So I thought it appropriate
to label "1" as a constant with a meaningful name.  But apparently
according to this section of PEP 8
(https://www.python.org/dev/peps/pep-0008/#id48):

<quote>
Constants

Constants are usually defined on a module level and written in all
capital letters with underscores separating words. Examples include
MAX_OVERFLOW and TOTAL.
</quote>

Scrolling through my entire module I see that I use COUNT_TODAY_TOO
locally in one other function for a similar reason.  The overall
module length is 383 LOC.

I guess the bottom line question is:  What should I have done?

N.B.:  The copying process from editor to Gmail window has apparently
reduced my indentation from 4 spaces to 2 due to the presence of the
gutter where pylint (via the ALE plugin) displays its fun little
flags.

BEWARE!  This installation of linting tools in my editor may lead to a
significant increase in my Tutor postings while I puzzle over all of
these newly flagged items in my code.

-- 
boB


More information about the Tutor mailing list