Skip to main content

All 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 ...
J B's user avatar
  • 479
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 ...
lucidbrot's user avatar
  • 662
0 votes
1 answer
1k views

How to handle config/env vars in a library project

I am building a new Python library project to be consumed by several of my application projects. The existing code consumes environment variables for various configuration settings. Should my ...
Brian's user avatar
  • 183
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: ...
jaaq's user avatar
  • 181
2 votes
2 answers
4k views

Module with globals or Class with attributes?

Currently I'm working with a lot of modules where the original developers used global variables to control states and to exchange important information between functions, like so: STATE_VAR = 0 def ...
Andrei's user avatar
  • 131
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 ...
N. Virgo's user avatar
  • 894
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 ...
NewCoder18's user avatar
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 ...
lucasgcb's user avatar
  • 365
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 ...
loa_in_'s user avatar
  • 229
-3 votes
1 answer
523 views

Import chains in Python

If my foo.py is merely foo_var = 1 and bar.py is merely import foo, I know I can write baz.py that says from bar import foo_var, but should I? (Or should I instead do from foo import foo_var?) Is ...
Pro Q's user avatar
  • 697
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'...
ffledgling's user avatar
-4 votes
4 answers
3k views

Creating one function for multiple purposes vs multiple functions for one purpose each [closed]

I have one function that is used to compute distances of an object in 3 different ways. Is one of the following two methods considered better practice: Creating 3 different functions, one each for ...
rahs's user avatar
  • 115
1 vote
3 answers
246 views

What is a good method/practice I can employ to keep identical code snippits in two places in sync? Also, help documenting functionals

If I could get some input on the design of this, I would be grateful as well. Note that I'm programming in python. There's a function F that takes lots of data, runs some analysis on it (taking ...
chausies's user avatar
  • 165
-1 votes
2 answers
10k views

Algorithm for scheduling shifts

I am trying to write a program to help scheduling shifts for the employees of a small business. There are 28 shifts that needs to be assigned to 28 employees (so this means that each person gets a ...
CL OS's user avatar
  • 11
-1 votes
1 answer
329 views

What is the programming paradigm when I just use functions in a file to organize my program?

I'm programming a telegram bot with Python and, for a number of reasons, there are no classes in the whole project, just several functions correlated to the the file where they are located. E.g., my ...
Teodoro Mendes's user avatar

15 30 50 per page