python-class

Are class variables accessed via self used as iterables good practice/pythonic?

Are class variables accessed via self used as iterables good practice/pythonic? Question: Is it appropriate to use a class variable accessed with self as an iterable in a loop? The following python code is valid and produces the expected result ("1, 2, 3") but it feels wrong somehow: class myClass(): foo = None def myfunc(self): …

Total answers: 1

How to prevent class contructor from blocking other threads?

How to prevent class contructor from blocking other threads? Question: I am new to threading, so this question might be too basic. I have two classes A and B. If I put their respective instances and methods in two threads as below: from threading import Thread from time import sleep class A: def __init__(self): sleep(10) …

Total answers: 1

Initialize class more efficiently in Python

Initialize class more efficiently in Python Question: I have this code in which I initialize a class (Adapter) by the name I get from the request. It seems to be a bit clumsy, and I’m sure there’s a better/cleaner way of doing it. from adapters.gofirst_adapter import GoFirstAdapter from adapters.spicejet_adapter import SpiceJetAdapter from adapters.airasia_adapter import AirAsiaAdapter …

Total answers: 2

Dict instance variable being changed for all instances

Dict instance variable being changed for all instances Question: I made a simple repro of my issue below. I have a class with an instance variable (inst_dict) which is of type dict. I also have a class variable (myClass_dict) which I use to keep track of each instance of my class. What I am trying …

Total answers: 1

Check if value in an array of objects

Check if value in an array of objects Question: I recently wrote a code that had all attributes stored separately in different arrays like order_id = [] order_name = [] order_products = [] order_total = [] And when importing new orders through an API request I would check if I already have that order by …

Total answers: 2

Python: How to get all the parent classes and function names in one array within that function

Python: How to get all the parent classes and function names in one array within that function Question: What I tried: import inspect class ClassA: class ClassB: def FunctionC(): print(inspect.stack()[0][3]) # returns ‘FunctionC’ ClassA.ClassB.FunctionC() This only returns the current function name, what I need is all the parent classes and function names inside one array. …

Total answers: 2

Module "Word" not found on custom class

Module "Word" not found on custom class Question: I have a script1.py that is trying to call a custom class named Word.py that is in the same folder as the custom class. The directory is like this: main/ ├─ folder1/ │ ├─ __init__.py │ ├─ script1.py │ ├─ Word.py ├─ folder2/ │ ├─ __init__.py │ …

Total answers: 2

Forcing read-only Parameter / property in __init__ class constructor in Python

Forcing read-only Parameter / property in __init__ class constructor in Python Question: It might be dumb and repeat question just like Class-level read-only properties in Python, which is hard to understand and implement. But is there any simple way to stop object user to modify class level defined read-only property (not method), just like other …

Total answers: 2

passing self with other argument on function call python

passing self with other argument on function call python Question: I have my code below. I want to call the "speak" function with two arguments inside the main() class. When I call speak it says that self its not defined I’m new to POO. class main(): def blueon(self): print("teste") def speak(self,fala): self.blueon print(fala) speak("testeaaaaa") Asked …

Total answers: 2

How do I convert a json file to a python class?

How do I convert a json file to a python class? Question: Consider this json file named h.json I want to convert this into a python dataclass. { "acc1":{ "email":"[email protected]", "password":"acc1", "name":"ACC1", "salary":1 }, "acc2":{ "email":"[email protected]", "password":"acc2", "name":"ACC2", "salary":2 } } I could use an alternative constructor for getting each account, for example: import json …

Total answers: 3