14

In PEP8, the general rule is to declare constants in UPPER_CASE chars. In real life, there can be a number of situations:

#!env python

DATABASE_HOST = 'localhost'
app = Flask('myapp')
base_two = partial(int, base=2) 

Typically we consider string-type or numeric-type variables as immutable and thus constants, but not object or function. The problem is there is no-way a linter can do type-checking.

How do you deal with this case? Should I capitalize them or just disable this rule in my linter?

6
  • 1
    You can do whatever you want, but for production and/or team work, and readability, it’s a good idea to follow some sort of style guide. Pep8 is one of them. Commented Jun 11, 2018 at 9:45
  • @FHTMitchell, I see - removed. But pep8 also describes what is usually considered a CONSTANT. Immutability of the value has nothing to do with the name. Commented Jun 11, 2018 at 9:48
  • Which linter is complaining about not capitalising values? Commented Jun 11, 2018 at 9:49
  • @AChampion sure, say that :) Commented Jun 11, 2018 at 9:49
  • 1
    I don't see this as a logically correct definition. It states constants are defined at module level but does not say what is considered constant. Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL Commented Jun 12, 2018 at 11:00

1 Answer 1

2

Personally the only time I ever capitalise something is when providing a value externally that should never be changed. Otherwise, it's fine to leave it in lowercase, especially if the value is just part of the logic flow. So

FLAG = object()

def func(arg_with_default = FLAG):
    if arg_with_default is FLAG:
         do_default()
    else:
         do_something_else(arg_with_default)

I think you're confusing immutability with constants.

t = (1, 2)
t[0] = 3  # error -- immutable
t = 3.14  # fine -- not constant

Python has no concept of constants. Every variable can be overridden, hence why in cases where it matters that a variable is not changed, that is perhaps the time to make that obvious by using capitalisation.

If the linter is complaining that you aren't capitalising things that don't need to be, I'd disable that option.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Disabling the entire name checking rule is okay but not the best option. I guess there is no easy way to enforce this rule then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.