Skip to main content

All Questions

-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 ...
Tim Kirkwood's user avatar
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: ...
User051209's user avatar
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'...
Tim Kirkwood's user avatar
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 ...
Florentin Alexandru Iftimie's user avatar
-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 ...
WhoDatBoy's user avatar
  • 123
0 votes
0 answers
118 views

"Best practice" or "design pattern" to group a class with "associated" classes in an object-oriented language

Sometimes a class A can have an "associated" class B such that the implementation of B depends on the implementation of A. For example, this can happen when B's objects are to be created by A's ...
Alexey's user avatar
  • 942
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
-1 votes
1 answer
585 views

How to write a loose Python interface where subclasses can add extra data?

Here are two object makers I made: def make_assassination(i): neighbors = [] def test(graph): for n in graph.neighbors(i): neighbors.append(n) ...
djechlin's user avatar
  • 2,212
4 votes
1 answer
2k views

Python class naming: nested classes or composed names?

I encountered a scenario where I cannot decide on which is the best (or worst) naming strategy. The context is the following: a bracket (as in a tournament) made up of nodes, where is node is made up ...
dabadaba's user avatar
  • 2,266
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 votes
2 answers
110 views

Object Oriented Python methods and their parameters

Let's say I have a class MyClass ... which has a data member x class MyClass1 : def __init__(self) : self.x = 1 Also a method which does something with x Should I pass self.x as a ...
ma77c's user avatar
  • 119
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
1 vote
2 answers
848 views

How should I structure these Python classes?

Base Class I have a class called Remote. This class represents a remote machine and has properties such as ip, hostname, username, and password, as well as methods for transferring files to/from the ...
ArtOfWarfare's user avatar
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
0 votes
1 answer
1k views

Should I use a class as a wrapper?

Lets say I have a class representing a chemical compound class Compound(networkx.Graph): def __init__(self): super(Compound, self).__init__() And lets say that I want to add some extra ...
Dan Oberlam's user avatar
  • 1,291

15 30 50 per page