All Questions
Tagged with python object-oriented
156 questions
0
votes
0
answers
32
views
API design for precomputation cache [closed]
In my numeric code library I have a function totient_sum that depends on an expensive one-time precomputation totsum_range = [...], then different calls to totient_sum(n) are quick. There are several ...
0
votes
1
answer
852
views
Abstract base classes and mix-ins in python
In the python docs, I read this about the ABC (abstract base class) meta class:
Use this metaclass to create an ABC. An ABC can be subclassed directly, and then acts as a mix-in class.
I don't come ...
21
votes
3
answers
24k
views
Why do you need "self." in Python to refer to instance variables?
I have been programming into a number of languages like Java, Ruby, Haskell and Python. I have to switch between many languages per day due to different projects I work on. Now, the issue is I often ...
0
votes
1
answer
258
views
Separation of concerns between business layer, data layer and presentation layer without losing information
I'm developing an api in Fast API using sqlalchemy to manage my ORM classes and operations with the database.
I'm dealing with some design decisions to have my classes as little coupled as possible ...
14
votes
3
answers
41k
views
Python classes with only one instance: When to create a (single) class instance and when to work with the class instead?
Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of ...
1
vote
3
answers
988
views
Storing multiple instances on a Singleton?
RefactoringGuru's example Singleton in Python has an _instances dictionary field
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls....
2
votes
1
answer
1k
views
In Python when is absolutely preferable to use a class instead of a module?
Python is the language I use most in this period.
My background in Java
Before start learning Python I have programmed in Java language. In Java all code is written inside the methods of a class and ...
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 ...
-2
votes
1
answer
295
views
Defining functions inside vs outside a class
Say I have a class with a function do_thing that is comprised of multiple steps, which themselves segregate into functions (first_process and second_process). At what point would this be considered ...
2
votes
1
answer
146
views
Refactoring Processor classes
I am writing some python 3 bioinformatics software and was wondering about the best way to write it in an OOP format. I am pretty sure a lot of my classes are violating the SRP principle, but I'm not ...
-3
votes
2
answers
278
views
Polymorphism with variable default argument count
I'm in the process of writing a library in Python, and I've run into a design problem concerning the use of polymorphism.
I have an ABC with abstract method 'foo':
class A(ABC):
@abstractmethod
...
2
votes
2
answers
2k
views
Why access the attributes of a Python class by reference?
Attribute references and instantiation
In this link, that is part of the official Python documentation, I have found the following information:
Class objects support two kinds of operations: ...
3
votes
3
answers
315
views
Referencing transient class attributes
I've just started dipping my feet into OOP.
Is it considered bad practice to have classes that reference attributes that depend on another function being called and thus may not exist (version 1)? I'...
0
votes
1
answer
940
views
Dependency Injection for dynamic objects
I am learning about Dependency Injection and I have been recently implementing the following classes for an app that executes commands over ssh using Python. I am confused about whether I am using it ...
-1
votes
1
answer
3k
views
Calling helper functions in a Python `__init__` function
Problem
I am currently working with a class that necessarily has a very complicated initialization function (>350 lines of code) given that many computations and attributes need to be performed and ...