All Questions
Tagged with programming-practices python
53 questions
37
votes
4
answers
22k
views
Are exceptions for flow control best practice in Python?
I'm reading "Learning Python" and have come across the following:
User-defined exceptions can also signal nonerror conditions. For
instance, a search routine can be coded to raise an exception ...
29
votes
6
answers
3k
views
Turning a personal Python project into a releasable library
I'm an academic rather than a programmer, and I have many years' experience writing Python programs for my own use, to support my research. My latest project is likely to be useful to many others as ...
16
votes
10
answers
9k
views
Preferring Python over C for Algorithmic Programming
I've been studying a bit of algorithms and have been looking at sites like SPOJ.pl TopCoder etc. I've seen that programmers prefer C or C++ usually for most algorithmic programming contests.
Now I'...
16
votes
5
answers
7k
views
Is monkeypatching considered good programming practice?
I've been under impression, that monkeypatching is more in quick and dirty hack category, rather than standard, good programming practice. While I'd used from time to time to fix minor issues with 3rd ...
13
votes
3
answers
27k
views
Is it a better practice pre-initialize attributes in a class, or to add them along the way?
I'm sorry if this is a ABSOLUTELY sophomoric question, but I'm curious what the best practices are out there, and I can't seem to find a good answer on Google.
In Python, I usually use an empty class ...
12
votes
3
answers
8k
views
Does it make sense to use string constants in Python instead of string literals as keys?
There is a dictionary in my class with informative, short string constants as keys that identify certain parts of the solution, like "velocity", "death_star_power_output". My colleague suggested that ...
8
votes
3
answers
3k
views
how to programtically build a grid of interlocking but random sized squares
I want to create a two dimensional layout of rectangular shapes, a grid made up of random sized cubes. The cubed should fit together and have equal padding or margin (space between). Kind of like a ...
8
votes
2
answers
6k
views
Sharing Docstrings between similar functions?
Assuming we have different classes with methods that possess the exact same description, however execute code a bit differently for the same return type.
class Foo:
"""This is the Foo class ...
7
votes
1
answer
6k
views
Breaking a Large Python Project into Multiple Packages
I have a medium sized Python program (12 KLOC) organized as a single Python package with multiple subpackages:
proj/
setup.py
proj/
__init__.py
projfile1.py
subproj1/
...
7
votes
1
answer
3k
views
Signature-changing decorator: properly documenting additional argument
Let's say I have a custom decorator, and I want it to properly handle docstring of decorated function. Problem is: my decorator adds an argument.
from functools import wraps
def custom_decorator(f):...
6
votes
2
answers
2k
views
Is it good practise to rely on the insertion order of python dicts?
Since python 3.7, it is guaranteed that dictionaries maintain insertion order. The linked stackoverflow Q&A states
This simply means that you can depend on it.
Is it good practise to depend on ...
6
votes
2
answers
19k
views
Best practice for Python main function definition and program start/exit
What is best practice to define a main function and entry point for a script/module that may be used started as main, but not always?
Here's how I've been doing it in the past, similar to realpython:
...
6
votes
1
answer
7k
views
Is it pythonic to use properties to limit the mutability of class attributes (variables and methods)?
Some Explanation
I'm somewhat new to python and to programming (I've been at it for a little over a year). I just recently discovered python properties, and I've been using them to limit the ...
5
votes
1
answer
1k
views
Should I avoid using style like `for k, v in dict_sample.items()`?
Today I was viewing my colleague's code and I saw a function like this:
def manager_skill_tree_func(*args, **kwargs):
"""# manage_skill_tree: Initialize the manage skill tree
"""
...
5
votes
3
answers
7k
views
How to write tests for function that depends on a config file?
I have a function that uses information from a config file. How do I test the function? Ideally, I'd want to inject my own version of the config file and test from there, but I'm not using dependency ...