Skip to main content

All Questions

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 ...
langlauf.io's user avatar
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
41 votes
6 answers
11k views

Should I create a class if my function is complex and has a lot of variables?

This question is somewhat language-agnostic, but not completely, since Object Oriented Programming (OOP) is different in, for example, Java, which doesn't have first-class functions, than it is in ...
iCanLearn's user avatar
  • 1,341
5 votes
6 answers
5k views

How to modify object properties?

I've recently read a great article about unit testing. There was an example of a bad method which is not well designed. It looks like this public static string GetTimeOfDay() { DateTime time = ...
Qback's user avatar
  • 231
33 votes
6 answers
13k views

Can you implement "object-oriented" programming without the class keyword?

Say we want to provide an abstraction of an "account" in a bank. Here's one approach, using a function object in Python: def account(): """Return a dispatch dictionary representing a bank account. ...
overexchange's user avatar
  • 2,305
4 votes
2 answers
4k views

What's wrong with using a Singleton?

I'm working on a Python application in which there are two Singleton classes: App and Configuration. The former seems straight forward, only ever instantiate one App instance; the latter seems ...
pstatix's user avatar
  • 1,047
7 votes
2 answers
12k views

Design pattern for similar classes that require different implementations

Edited: Update is at the bottom There could be a common or best practice for this scenario, but I am unfamiliar with it. However, it could very easily be a matter of subjective opinion on how one ...
pstatix's user avatar
  • 1,047
4 votes
4 answers
650 views

OOP - Is this data hiding?

I've been reading about data hiding and there seem to be slightly different definitions of it, so when trying to come up with a practical example, I'm not sure if I'm doing the right thing. So far, I ...
Floella's user avatar
  • 383
2 votes
1 answer
118 views

Is it a good software engineering practice to store libraries as attributes of objects?

Suppose I initialize a library, say, in python object - #filename: file_A import file_ class A(): def __init__(self): self.pd = file_.pd self.np = file_.np And suppose the ...
Mr. Sigma.'s user avatar
1 vote
1 answer
198 views

Representing mathematical tree structures using software in a compact manner

In my work I frequently come across systems of interdependent equations. I have contrived a toy example as follows. The terminal values w, x, y and z are given: e(y) = A+B A(y) = x*log(y)+y^z B(y) =...
user32882's user avatar
  • 267
0 votes
1 answer
2k views

Create object inside a method

Let's say my object needs to use an API to communicate with a device. The API call that I need is Api.do_something(). What would be the best way to solve this assuming that I need to call it only ...
iknownothing's user avatar
0 votes
3 answers
2k views

When NOT to use a class / member variable?

I am trying to learn WHEN NOT to use: classes member variables HERE IS THE CODE access_point_detection_classes.py from scapy.all import * class Handler : def __init__(self) : self....
ma77c's user avatar
  • 119
1 vote
2 answers
101 views

__init__ arguments differ from object attributes

Is the following class definition a good design? class Myclass: def __init__(self,num1,num2): self.complicated_tree = __class__.object_creator(num1,num2) @classmethod def ...
l7ll7's user avatar
  • 263
1 vote
3 answers
2k views

How To Extend Parent Methods in Children Classes?

There is a parent class with a method which many children use but many children extend the method, what is the best way to extend it without violating DRY? Here are my 2 current solutions: 1: The ...
Will Beauchamp's user avatar
3 votes
2 answers
2k views

One boilerplate class or many similar classes?

Lets say I'm trying to model a variety of objects that are virtually identical, the only difference being their class variables. Am I better off creating one boilerplate class and just calling the ...
Dan Oberlam's user avatar
  • 1,291

15 30 50 per page