All Questions
Tagged with python python-3.x
98 questions
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 ...
5
votes
2
answers
662
views
Why did Python designers decide not to declare vars? [closed]
In Python when you want a local variable, you just assign to it x = 10.
In most modern languages you declare local vars (regardless of type):
JavaScript: let/const/var
Swift: let/var
Kotlin: val/var
...
-3
votes
2
answers
3k
views
Is it better practice to set default values for optional argparse arguments?
I mention python's argparse as an example but this would apply to any CLI argument parser library for any language. Just generally speaking, at a high level, if there are a lot of optional arguments ...
21
votes
6
answers
6k
views
Does subclassing int to forbid negative integers break Liskov Substitution Principle?
In Python 3, I subclassed int to forbid the creation of negative integers:
class PositiveInteger(int):
def __new__(cls, value):
if value <= 0:
raise ValueError("value ...
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: ...
93
votes
7
answers
28k
views
How bad of an idea is it to use Python files as configuration files?
I've always used JSON files for configuration of my applications. I started using them from when I coded a lot of Java, and now I'm working mainly on server-side and data science Python development ...
13
votes
6
answers
11k
views
Is it reasonable to use dictionaries instead of arguments?
In python I often see functions with a lot of arguments. For example:
def translate(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p):
// some code
return(x, y, z)
I like this pattern in some ...
54
votes
5
answers
40k
views
Are Python mixins an anti-pattern?
I'm fully aware that pylint and other static analysis tools are not all-knowing, and sometimes their advice must be disobeyed. (This applies for various classes of messages, not just conventions.)
If ...
2
votes
1
answer
3k
views
How to add some data to an Enum in Python
I have a class that I use to define different types of plots I am performing
class MyPlots(Enum):
STANDARDSCALE = "standard"
LOGSCALE = "log"
there are default values ...
20
votes
2
answers
16k
views
How should I name functions that return values in Python?
I'm confused about choosing names for my functions in Python. Sometimes Python built-in functions are imperative such as: print function and string method find. Sometimes they aren't such as: len its ...
0
votes
3
answers
2k
views
Python best practice when logging optional arguments
I have a method that accepts one or more optional arguments and I'd like to log them, following the best practice of lazy interpolation of log values:
def frobnicate(a: str, b: int, c: typing.Optional[...
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 ...
-1
votes
1
answer
2k
views
Are there different ways in Python to decorate class methods that dynamically assign themselves to a dict within the class?
Here's what I'd like to do in the form of working code, since it's difficult for me to explain otherwise:
from typing import Callable, Generic, TypeVar
from typing_extensions import Self
# The type ...
2
votes
1
answer
3k
views
Type-hinting and accessing values that are not initialized in __init__ (Python)
Suppose I have an instance attribute that I don't initialize in __init__, but in normal use it should be initialized before any other methods use the value. I want to structure everything so that it ...
2
votes
2
answers
5k
views
Alternative to using regex in Python
Background
I do programming with Python and now and then i run into a situation where i have to use regex
Typically i try to learn a bit about it and look at examples of doing things similar to what i'...