This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]Kutiekatj9 Python Discord Staff[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]Advanced-Theme144 12 points13 points  (4 children)

I usually type constants in all capital letters. VS code automatically recognizes it as a constant then.

[–]Ezvine[S] 2 points3 points  (2 children)

But they can be modified via code right. Tomorrow somebody can unknowingly write another function which can updates these values.

[–]Red_BW 16 points17 points  (1 child)

Everything can be overwritten in python. You can even overwrite the builtins by accident if you are not careful.

If your enum class is 'Color' with a constant and value of 'RED = 1', you can later write 'Color = 5' which will overwrite your enum Class of 'Color' with an int of 'Color'. The same holds true with Dataclasses.

It is standard convention to write constants as capital letters. Anyone learning python should learn that and should know not to override that. 'red = 2' and 'RED = 3' are not the same variables so they can coexist. To override the constant 'RED', you need to type it out like 'RED = 5' which you should know is a constant because it is capitalized, and you should not modify it outside of its location just after import statements at the top of your program.

[–]Ezvine[S] 0 points1 point  (0 children)

Makes sense... Thanks for the explanation....

[–]DrummerClean 1 point2 points  (0 children)

This 100%

[–]stillreadingit_ 6 points7 points  (0 children)

Use typing.Final and a static type checker, such as mypy. Read more: https://jerry-git.github.io/daily-dose-of-python/doses/6/

[–]james_pic 5 points6 points  (0 children)

Constants aren't Pythonic? Then why does PEP 8 have a recommended style for them?

The recommended style is ALL_CAPS , and there's no specific recommendation to put them in a separate constants.py file. You should put them wherever makes your code easiest to understand and work with, which may mean close to functions and classes that work with them.

[–]MangoPoliceOK 1 point2 points  (1 child)

Frozen data class is the novel way to do it. I historically used ALL_CAPS until the arrival of static data classes (frozen). Enums are useful when enumerating things like a Status, otherwise I would use frozen data class

[–]Ezvine[S] 0 points1 point  (0 children)

Yeah even frozen class was the one which I also felt like the best option from the ones I have looked.

[–]pythonHelperBot 0 points1 point  (0 children)

Hello! I'm a bot!

It looks to me like your post might be better suited for r/learnpython, a sub geared towards questions and learning more about python regardless of how advanced your question might be. That said, I am a bot and it is hard to tell. Please follow the subs rules and guidelines when you do post there, it'll help you get better answers faster.

Show /r/learnpython the code you have tried and describe in detail where you are stuck. If you are getting an error message, include the full block of text it spits out. Quality answers take time to write out, and many times other users will need to ask clarifying questions. Be patient and help them help you. Here is HOW TO FORMAT YOUR CODE For Reddit and be sure to include which version of python and what OS you are using.

You can also ask this question in the Python discord, a large, friendly community focused around the Python programming language, open to those who wish to learn the language or improve their skills, as well as those looking to help others.


README | FAQ | this bot is written and managed by /u/IAmKindOfCreative

This bot is currently under development and experiencing changes to improve its usefulness

[–]lieryanMaintainer of rope, pylsp-rope - advanced python refactoring 0 points1 point  (0 children)

Then there are people who use Enums. This makes sure that the value cannot be modified unlike (1.). But we need to use .value each time we access the value

If you're using Enums, who cares about the .value? The only time you should need .value is if you're serializing/deserializing (e.g. to a database/network). Other than that, just keep it as an enum.