Questions tagged [python-3.x]
Python 3 is the latest version of the Python programming language and was formally released on December 3rd, 2008.
156 questions
5
votes
2
answers
730
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
5k
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 ...
1
vote
3
answers
584
views
Python logging in shared functions called by multiple main programs
I have reviewed this, but it doesn't seem to address what I'm asking here.
https://stackoverflow.com/questions/15727420/using-logging-in-multiple-modules
I want to have multiple programs call the same ...
1
vote
0
answers
65
views
How can I use django to render a rotating view of a database?
As a toy problem for learning Django, I am trying to create a simple web app that tracks encounters and initiative for one of my D&D campaigns. I have a database with models for combatants:
class ...
2
votes
3
answers
454
views
Does it make sense for an API with 1 daily job to do to be asynchronous?
This is a conceptual question about whether my specific use-case warrants the use of an asynchronous API.
Language: Python 3.11
Framework: FastAPI (ASGI)
I believe I am confused about what an ...
0
votes
2
answers
356
views
Appropriate design pattern for providing a default Argparse instance, eliminating boilerplate
I'm using argparse.ArgumentParser extensively; however, it comes with a lot of boilerplate to set up, and this is especially noticeable when you've got more than a few common arguments that probably ...
0
votes
1
answer
231
views
Match making algorithm respecting players' choices
I am currently developing an application in Python that has a match making functionality to form sports teams of 4 and group them by skill. The following has been implemented and works.
E.g.
Form ...
1
vote
0
answers
95
views
What is the best way to implement the following type of paradigm?
I have the following piece of code. Line 1 is a container (for simplicity, one can think of it as a list of elements e1, e2, ..., en). Now there is a function function_fun which takes as input an ...
2
votes
2
answers
170
views
OOP Best practices: Is there any reason to separate out Factory functionality from an abstract base class?
Consider the following python3 code:
from abc import ABC, abstractmethod
class Food(ABC):
_food_factory_map = {}
_recipes = {}
@classmethod
def getFood(cls, foodName):
return ...
1
vote
2
answers
1k
views
Should I duplicate or inherit a python dataclass which changes attributes based on the version of an API endpoint?
I'm working on a python library for a REST API.I'm using python data classes to represent the structure of the returned JSON
The v2 of this API returns a slightly different object when compared to v1.
...
1
vote
0
answers
1k
views
Python - Where do I store data classes which are common across different files?
I'm working on a python wrapper for a REST API. I'm using python data classes to store the shape of the JSON response of each endpoint so developers have features like autocomplete and objects they ...
21
votes
6
answers
7k
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 ...
-2
votes
1
answer
91
views
python language construct or facility to tag entering and exiting a while loop
Here's what I want to achieve:
while complex_compound_condition_statement:
foobar() # ... do some stuff
I would like the log output of the above to be:
<timestamp> INFO: started doing stuff ...
2
votes
1
answer
4k
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 ...
5
votes
1
answer
360
views
Does my View Model violate the Single Responsibility Principle? Or am I just used to toy examples from tutorials?
I am a very beginner writing one of my first webapps. I'm using FastAPI and I'm stuck on the logic of creating an endpoint that has to do a lot of things before it returns something back to the user. ...