Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 days ago.
Improve this question
What are the naming conventions for keys used in python dictionaries?
I found these common naming conventions, however, they only include methods, functions and constants. From my understanding, key does not fall into any of these categories.
Also, there is no convention mentioned in pep8.
In the end, I think it is just important to stick to one convention, but I am quite curious, what would be the pythonic way?
In this case, the dictionary is part of a JSON request in an API, if that makes any difference.
Is there a convention?
Name the keys by what they are - when defining dictionaries yourself.
Name will be assigned, if for example you parsed a json into a dict.
I would say that most times I have seen everything done in snake_case when it comes to dictionary keys.
Here's a fairly intuitive way of dealing with dictionaries:
question2answer = {question_one:"answer"}
question2user = {question_one:"john doe"}
...
for question in question2answer:
answer = question2answer[question]
author = question2user[question]
print(f"Question: {question}, answer: {answer}, author: {author}")
Here's a good excerpt: https://climbtheladder.com/10-python-list-dictionary-naming-best-practices/#p6
Using meaningful keys in dictionaries makes it easier to read and understand the code. It also helps with debugging, as it is much easier to identify a problem when you can easily recognize what each key represents. Additionally, using meaningful keys allows for more efficient coding, as you don’t have to remember arbitrary numbers or symbols associated with each item.
To use meaningful keys in dictionaries, start by thinking of the data structure as an object that contains properties. Each property should be given a name that accurately describes its purpose. For example, if you are creating a dictionary to store information about a person, you could use “name”, “age”, and “address” as your keys. This will make it easy to access the values associated with each key.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm writing a module which only contains functions. Is it good practice to put these inside a class, even if there are no class arguments and the __init__ function is pointless? And if so how should I write it?
There's no particular reason to force functions to go inside a class if there's no reason to make a class. A python sourcefile already provide a perfectly reasonable namespace for storing similar, related functions. The pypy implementation of the python programming language, for example, has many modules that don't have classes in them (for example, time).
You would want a certain data structure as a class, of course, but there are certain behaviors that are entirely independent of data structures, and putting them in a class would just add needless complexity.
It is good to build modules that contain a class for better organization and manipulation depending on how big the code is and how it will be used, but yes it is good to get use to building classes with methods in them. Can you post your code?
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I read PEP8, and abbreviations for python, and I have question regarding the variable name itself.
I'm familiar with the famous variable abbreviations, For example, :
str for string
cnt for counter
cfg for configuration
It's came into my mind after thought about an appropriate name for a variable that contain first instruction, I thought about fisr_inst or first_instr or even first_instruction but couldn't find any document regarding this in Python.
but is there a table, or a list of all those abbreviation conventions in python? did I miss it?
EDIT - I didn't asked for appropriate name for "my variable name", I asked if there a table or list that contain some abbreviation for common use in python
Explicit and verbose is the recommended standard as far as naming things is concerned. EXTREMELY common abbreviations that are impossible to be mistaken are used to shorten line length, but don't think too far into those.
i for index, j for secondary index (in a 2D array), f for file, del for delete, str for string, cnt for count, cfg for config, prod for production, dist for distribution, fact for factorial, etc.
I don't know what use you have for first_instruction, but I wouldn't try to shorten either word - that is a solid variable name.
edit: My opinion has changed on the matter so I figured I'd share. I've now worked on distributed teams where contributing members have learned English later or are still learning English. Nowadays, I simply never abbreviate because I was only doing it to cut a few characters anyways 🤷♂️
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
As the title suggests, I'm interested in the best (perhaps the most Pythonic way) to structure a program which uses many global variables.
First of all, by "many", I mean some 30 variables (which may be dictionaries, floats or strings) which every module of my program needs to access. Now, there seem to be two ways to do this:
define the "global" variables in seperate modules
use an object oriented approach
The advantage of using an object oriented approach is that I can have many instances of some main class initialized, and perhaps compare different values (results of some analysis, for example) later on.
I already have a program written, but basically it breaks down to one class with some 30 or so attributes. Although it works fine, I'm aware this is a pretty messy way to do this.
So, basically, is I use OOP approach, I would perhaps need to break my main class down to a few subclasses, every one of which stores specific logically related variables.
Any suggestions are welcome.
P.S. Just to be concrete about what I'm trying to do: I have a FEM-solver which needs to store structure info, element and node data, analysis result data, etc. So, I'm dealing with a lot of data types most of which are connected in some way.
Unfortunately, as was hinted at in the comments, there is no "Pythonic" way to do this. Having a large number of global constants is just fine - many programs and libraries do this. But in the comments, you've specified that all of your globals are being modified.
You need to take your program's architecture back to the drawing board. Rethink the relationships between your program's entities (functions, classes, modules, etc). There has to be a better way to organize it.
And by the way, it also sounds like you're getting close to using the God Object Antipattern. Use some of the advice in this SO question to refactor your massive class that has it's fingers all over your program.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I am sure this question was already answered couple of times, and i will soon close this topic, but i couldn't find it.
Is there a recommendet way of arranging member functions?
I am pretty sure not everything need rules, but wanted to if there are thoughts about this topic, which goes more then sorting by access level.
Example class in Pseudocode:
Class
amethod()
bmethod()
cvariable
avariable
bvariable
There are some recommendations, but they are rather vague. The only common point is, quoting the Google Java Style Guide, § 3.4.2,
that each class order its members in some logical order, which its
maintainer could explain if asked. For example, new methods are not just
habitually added to the end of the class, as that would yield "chronological
by date added" ordering, which is not a logical ordering.
The Google C++ Style Guide recommends to order by visibility (which is obvious for C++), then by type:
Typedefs and Enums
Constants (static const data members)
Constructors
Destructor
Methods, including static methods
Data Members (except static const data members)
Oracle’s Code Conventions for Java, § 3.1.3, recommend to order by type, then by visibility (for variables) or functionality (for methods):
Class (static) variables (ordered by visibility)
Instance variables (ordered by visibility)
Constructors
Methods (grouped by functionality rather than by scope or accessibility)
I´m not aware of any generally/widely accepted convention, other than "use something that makes sense" (eg. grouping variables instead of writing a wild mix of vars and functions).
However, it´s not only a style question:
In C++ (and C), the order maps directly to the memory layout, this can lead to different variable sizes because of alignment and padding. Additionally, if serializing something in a binary format, where which value is in the data is of course important (but serializing that way is not exactly good, because the memory layout depends...).
And, like #huu noted in the comments, the variable order determines the initialization order, this is important a member variables is initialized with the value of another member variable (of the same object). A mismatch in variable declaration order and initialization order will lead to a compiler error.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've researched multiple methods for saving a dictionary, such as pickle, JSON and CSV, but I don't see anywhere with the benefits or restrictions of each of these methods.
pickle:
On the plus side, it can handle arbitrary objects (with varying levels of work). On the minus side the flat format is not human-readable, and it shouldn't be used with untrusted input. There are versioning issues, too; there are various different protocols defined.
json:
It's easy to move back and forth between some container (dict, list) and value (string and number) objects and JSON. It's also generally human-readable (subject to "pretty" formatting), widely used and well-supported by most (all?) languages. It can't handle arbitrary objects like pickling can, though.
csv:
Arguably the simplest format, but won't handle nesting well while remaining readable and easy to parse (it's probably best suited to persisting a simple table). There's generally more work to convert back and forth than JSON or pickle, too.