Skip to main content

All Questions

Tagged with
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 ...
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 ...
3 votes
2 answers
419 views

How does the Python runtime know which object a method is bound to?

When defining methods in a class we do have an argument for the object on which the method is invoked i.e self. Say I have a class Foo, and an instance of it: class Foo: def m(self): pass ...
2 votes
1 answer
388 views

Speech to text - action - text to speech: architecture/logic. Is there a better way to build my robo-player?

I have decided to start a personal project using Python since I have used it for several years now and I would like to know if the approach that I have considered is good or not. Description It's ...
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 ...
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 ...
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 ...
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[...
-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 ...
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 ...
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 ...
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 ...
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 ...
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: ...

15 30 50 per page
1
2 3 4 5
7