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 🤷‍♂️
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 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.
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 10 months ago.
Improve this question
As far as I know, it is usually considered good to extract most strings to constant variables and avoid having "magic" strings and numbers in the middle of code. But in the Python world I have seen Exceptions raised with an string literal most of the time. I have also seen the same done in logging statements, and other similar situations.
Example of raising an Exception with a constant:
ARGUMENT_LIST_TOO_SHORT = "The provided list is too short"
def whatever_function():
raise ValueError(ARGUMENT_LIST_TOO_SHORT)
Example of raising an Exception with a string literal:
def whatever_function():
raise ValueError("The provided list is too short")
As I see it, it shouldn't be too bad if the string is only used once, as the risk of mistyping it is the same as typing it in the constant in the first place.
So is it a good practice to extract error description strings and log strings into constants? Or is it more Pythonic to use string literals?
Think about what you would need to do if you were writing code that was to be executed in a locale where the language didn't match the fixed text. As far as I know, Python doesn't having any out-of-the-box concept of a "resource bundle" (unlike Java for example).
This is a matter of opinion but I would argue strongly in favour of saying that text the user sees/interacts with should always be referenced indirectly. Such a technique facilitates re-use and language portability.
Having said that, the original question (and definitely this answer) are opinion based and therefore frowned upon in this forum.
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 2 years ago.
Improve this question
I have a list of words, and a few definitions as follows:
word - trans1, trans2, trans3 ...
I'm now unsure if all those translations are correct. I want to use a library that for that given word gives all translations possible
word - trans1 ... transn
I'll then match each of my translations to those provided by the library / translation software, and make sure that it is a valid translation. The problem is I don't know of such a library. I do not want something like googletrans as it only provides one possible translation [because it is used to translating paragraphs] and there is a clear word / search limit every time I use it, stopping abruptly after a few with just running a few trials. It also is inconsistent in its translation pattern, for example sometimes adding "to" to the infinitives of verbs and sometimes not. Is there something like this that exists? Essentially what I want is a many result English-destinationlanguage dictionary library.
Google Translate API is probably your best bet out there. I'm no Google fanboy but credit where credit's due, and Google Translate is probably the best in the game right now.
As far as the problem of the program abruptly stopping, make sure that you're using the API correctly(read this).
As far as the infinitives are concerned, machines generally suck at translation, to understand watch this great video by Tom Scott.
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 6 years ago.
Improve this question
For example, would it be okay to use "pd" instead of "pandas" and "df" instead of "data_frame" in Python code?
On the one hand, using short variable names (less than 3 characters in length) is generally bad practice, stylistically, and from a maintainability perspective.
On the other hand, df and pd are rather ubiquitous for any Python programmer that uses the Pandas library.
What say you?
Stack Overflow really isn't the place for questions like this, may I suggest the Programmers Stack Exchange?
But since it is here, speaking as someone who has been forced to work with a massive C codebase with lots of 1-3 letter variable names and no documentation for the last few months, more verbose and descriptive variable names are probably better, especially if other people have to work with your code.
It would be better practice to use more letters, because if other people review your code, the variable pd would not make sense to them.
No, unless it's longer than 7 or 8 characters, as a rule of thumb. Get a proper IDE that can do some decent autocomplete, like PyCharm (autocomplete dialog automatically pops up and enter completes the variable).
df is too cryptic. Take a look at C++, with all its cryptic names itoa, itoa, stoi, etc.
PyCharm can do variable/class/function renaming automatically too.