Skip to main content

All Questions

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
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
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 ...
swoutch's user avatar
  • 321
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
13 votes
3 answers
27k views

Is it a better practice pre-initialize attributes in a class, or to add them along the way?

I'm sorry if this is a ABSOLUTELY sophomoric question, but I'm curious what the best practices are out there, and I can't seem to find a good answer on Google. In Python, I usually use an empty class ...
Gilead's user avatar
  • 233
12 votes
1 answer
4k views

When should I subclass an exception in Python?

In my code there are about seven places where I raise an exception. All of these exceptions are treated the same: print an error to log file, return software state to default and exit. During code ...
Ezra's user avatar
  • 365
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
6 votes
1 answer
2k views

Why would it be considered bad practice if a concrete class inherits from only one Mixin?

I'm referring to "Fluent Python" by Luciano Ramalho. In chapter 12, there's a section "Coping with Multiple Inheritance" where the author suggests a few best practices, such as: ...
Lagerbaer's user avatar
  • 556
5 votes
2 answers
1k views

Design classes to model 3D scanned faces of ancient Greek/Roman sculptures: is multiple inheritance a good design solution?

I would like to deepen the topic of multiple inheritance using Python and I usually find examples that are too simple. I love art and I imagined the following problem and I want to understand if ...
blunova's user avatar
  • 398
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
5 votes
1 answer
305 views

In more canonical OO Python situations, what is the rule of thumb for default access modifiers?

Generally speaking in canonical OOP situations, the rule of thumb is to write your classes with the least access as necessary. i.e. only make public only what is necessary, make protected only what is ...
Trevor Boyd Smith's user avatar
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
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
4 votes
2 answers
8k views

Setting class/methods conditionally

I want to keep the end-user (often myself) from being presented with inapplicable methods when coding directly in an interpreter, but I also don't want the user to have to inspect the incoming data ...
Tom's user avatar
  • 291
3 votes
1 answer
906 views

Best Practice for Populating Objects in Python

So I am pulling data (list of JSON) from an API and want to parse it into Python objects. However the JSON objects of my list returned from my API need to be transformed a bit to fit into my object. I ...
cdesar78's user avatar

15 30 50 per page