All Questions
Tagged with python coding-style
76 questions
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:
...
0
votes
2
answers
622
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 ...
-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 ...
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 ...
24
votes
8
answers
7k
views
Is using lambdas to express intent not pythonic?
PEP 8 states the following about using anonymous functions (lambdas)
Always use a def statement instead of an assignment statement that
binds a lambda expression directly to an identifier:
# Correct: ...
2
votes
1
answer
470
views
How to balance 'efficient' vs 'clean' code? [closed]
I have been coding in python for a little over a year, and I have learned a lot and developed quite a few applications, in the process.
I do not program for my profession, I simply program ...
1
vote
3
answers
175
views
Weighting guidelines to choose between a method and a function
Quite often I find difficult to decide between implementing operations as functions or as methods because I am not sure how to weight various well-known guidelines for this problem. I would like to ...
3
votes
2
answers
1k
views
Python import order, mixing from ... import ... and import ... (and import ... as ...)
This is the mess of imports currently at the top of my file:
import argparse
from copy import deepcopy
from functools import cmp_to_key, partial
from itertools import chain
import math
from ...
7
votes
1
answer
3k
views
Python: Class vs NamedTuple vs Hybrid vs DataClass
So all four of these approaches to structure data on their surface work more or less the same to keep data well structured.
Are there any reasons, be they hidden performance issues/enhancements, ...
3
votes
1
answer
1k
views
Strictly only importing modules in subdirectories: is this a good rule?
I'm making my 1st official project. It's written in Python, is open-sources, and I'd like people to be able to freely and easily fork and modify the code. The project name is "shelf" and the ...
3
votes
5
answers
1k
views
When to extract boolean conditions into their own function?
I commonly use a boolean condition/variable when it's something too big or complicated and takes too much space by itself inside ifs - basically to avoid repeatability and thus improve readability. E....
0
votes
2
answers
257
views
Code style to keep track of nested objects and data types?
In untyped languages (Python, Javascript specifically) find myself making a lot of bugs / wasting a lot of time because I forget what's in the objects I'm passing around. For example, I forget things ...