All Questions
Tagged with python coding-style
76 questions
310
votes
2
answers
539k
views
Python file naming convention?
I've seen this part of PEP-8 https://www.python.org/dev/peps/pep-0008/#package-and-module-names
I'm not clear on whether this refers to the file name of a module/class/package.
If I had one example ...
59
votes
6
answers
111k
views
Should I really use all uppercase for my constants?
I am a Python programmer primarily who uses pylint for linting source code. I am able to eliminate all of the warnings except one: Invalid name for a constant. Changing the name to all caps fixes it, ...
13
votes
3
answers
3k
views
Using `any` to indicate a wildcard value
I'm writing a validator class to validate certain request objects against a known format. Rule declarations and the validator will both be written entirely in Python, and I don't need to store the ...
24
votes
6
answers
9k
views
Best practice for redundant conditions in if-elif-else statements
What is considered better practice?
Case 1:
if n == 0:
doThis()
elif n < 0:
doThat()
elif n > 0:
doSomethingElse()
Case 2:
if n == 0:
doThis()
elif n < 0:
doThat()
else:
...
36
votes
3
answers
39k
views
Is it ok to have multiple classes in the same file in Python?
In Java and PHP (although not strictly required), you are expected to write each class on its own file, with file's name is that of the class as a best practice.
But in Python, or at least in the ...
0
votes
2
answers
621
views
Is there some easy way to refactor deeply coupled python code
I recently took on a long ago python project which has some weird code style that I can't pinpoint. e.g.
# this is a params and value package?
opts={
infile="xxx",
outfile="xxx"
}
...
2
votes
1
answer
608
views
Does "happy path to the left edge" break Python conventions?
I found the short article Align the happy path to the left edge quite helpful in improving readability of functions. Briefly, reading down the left edge of the function should step you through the ...
17
votes
3
answers
14k
views
`var is None` vs `var == None`
Why do people prefer var is None over var == None when is can be used on few objects only?
174
votes
23
answers
26k
views
Programming cleanly when writing scientific code
I don't really write large projects. I'm not maintaining a huge database or dealing with millions of lines of code.
My code is primarily "scripting" type stuff - things to test mathematical ...
7
votes
2
answers
3k
views
Why should imports be made at the beginning
Why is it considered good style to do all imports in a python application at the beginning, even if what is imported is used only once?
I have been programming only shorter pieces of software for a ...
-1
votes
1
answer
3k
views
Best practices for naming python utils / extending core modules?
So a lot of the time my utils end up with a structure that mirrors the core library. I might end up writing a multiline version of str.center, an itertools-y function that returns the first or last ...
14
votes
3
answers
3k
views
Should I choose repeated code in unit test or test logic? Can I avoid both?
When writing unit tests, I feel that there is a trade-off between code repetition and test logic.
Example of my current (likely flawed) approach:
To test this function (overly simple function for ...
8
votes
2
answers
7k
views
When is it not Pythonic to use properties instead of a method that doesn't take arguments?
It's widely agreed that Python's properties are not merely a kludge for working around the past mistake of exposing of publicly exposing data members. But then when is it not Pythonic to use a ...
15
votes
5
answers
19k
views
Is using nested function calls a bad thing?
In a recent homework assignment I ended up calling my functions in an ugly way uglyReceipt(cashParser(cashInput())) the program itself worked perfectly but I still felt like I was doing something ...
4
votes
1
answer
2k
views
Duplicate code for imports Python
In a project I have the same imports in multiple files. For example:
import os
import logging
import json
import time
import pathlib
and pylint will tell me I have duplicate code. I know that there ...