python-class

How to resolve ModuleNotFoundError when importing a local Python file?

How to resolve ModuleNotFoundError when importing a local Python file? Question: I am studying python. I’m trying to do a simple exercise from the course I’m studying. I tried to separate the classes into different files to make it easier to keep track of the inheritance and to be able to update the program in …

Total answers: 1

Mapping class methods of python class

Mapping class methods of python class Question: I created a message class, which stores an arbitrary message like so: class MessageBase: """Wrapper class for messages from different resources""" def __init__( self, subject, body, sender, recipient, received, to ): self.subject = subject self.body = body self.sender = sender self.recipient = recipient self.received = received self.to = …

Total answers: 2

scope strangeness in python class attributes

scope strangeness in python class attributes Question: Came across what seems to be a weird case of ‘spooky action at a distance’ using python class attributes. If I define X as: class X(): a = list() b = int def __init__(self, value): self.a.append(value) self.b = value Then instantiate: u = X(0) v = X(1) It …

Total answers: 1

is "correct" or pythonic, this way of make getters or is better to use @property?

is "correct" or pythonic, this way of make getters or is better to use @property? Question: I have a class where the attribute is a numpy array, and a lot of getters, for several slices of that array. The question is about what is a more pythonic way of doing this def get_right_knee(self) -> YoloV7PoseKeypoint: …

Total answers: 1

Generating a random number (more than once) and using it inside two class functions

Generating a random number (more than once) and using it inside two class functions Question: I am building a GUI with tkinter where two buttons modify two text fields. One button chooses a random key from the example_dict and displays it in text1 , and the other reveals the corresponding value in text2 . I …

Total answers: 3

Python decorate `class.method` that modify on `class.self`

Python decorate `class.method` that modify on `class.self` Question: How to access self of the decorated method? Based on this answer, the self refer to the decorator: class Decorator: def __init__(self, func): self.func = func def __call__(self, *args, **kwargs): print(self.func.__name__) self.counter += 1 return self.func(*args, **kwargs) class A: def __init__(self): self.counter = 0 @Decorator def method1(self): …

Total answers: 1

Can you modify an object's field every time another field is modified?

Can you modify an object's field every time another field is modified? Question: I have a dataclass that looks like this from dataclasses import dataclass, field @dataclass class Data: name: str | None = None file_friendly_name: str | None = field(default=None, init=False) def __post_init__(self): # If name, automatically create the file_friendly_name if self.name: self.file_friendly_name = …

Total answers: 3

Problems with version control for dictionaries inside a python class

Problems with version control for dictionaries inside a python class Question: I’m doing something wrong in the code below. I have a method (update_dictonary) that changes a value or values in a dictionary based on what is specificed in a tuple (new_points). Before I update the dictionary, I want to save that version in a …

Total answers: 1

"Not all parameters were used in the SQL statement"

"Not all parameters were used in the SQL statement" Question: i am not able to find what is error in this code. help!!! class accountManage: def connection(self): db=mysql.connector.connect(username="root",password="jhanguman",database="test") def createTable(self): cursor=db.cursor() #cursor.execute("drop table if exists") cursor.execute(”’create table if not exists ACCOUNT ( Account_ID int PRIMARY KEY, Account_Name varchar(250), Account_Size int, Account_Duration int, Account_Budget float, Status …

Total answers: 1