510 questions
1407
votes
17
answers
574k
views
What is a mixin and why is it useful?
In Programming Python, Mark Lutz mentions the term mixin. I am from a C/C++/C# background and I have not heard the term before. What is a mixin?
Reading between the lines of this example (which I have ...
2
votes
0
answers
66
views
New Dialog Class Incorrect Button Style
I was trying to make a subclass that acts like the original but its go function returned the string of the text of the button, instead of the index of the button.
My own defined subclass makes the ...
0
votes
1
answer
1k
views
Build a Python class for FPDF
I want to use the Python library fpdf2 and build my own class to generate a pdf document. I tried this:
from fpdf import FPDF
class MyPdf(FPDF):
def __init__(self, *args, **kwargs):
...
0
votes
0
answers
36
views
Accessing a sibling inner class in initialiser
Consider the following code, if you will:
class ParentService:
BASE_DIR = '/some/path'
class Results(str, Enum):
RESULT1 = 'ResultOne.xlsx'
RESULT2 = 'ResultTwo.pdf'
...
0
votes
2
answers
867
views
Decorate all functions of a Python class while being able to access the class attributes within the decorator
I've got a class and would like to apply a decorator to all functions within that class without having to add a function decorator to every single function.
I know that there are solutions like the ...
1
vote
1
answer
44
views
Adding a second class breaks tkinter variable
I have a class for a GUI with two radio buttons one to set save_to_excel to True and the other to False. However I wanted to add a loading screen using threading, but when I add that second class the ...
0
votes
0
answers
33
views
how I can describe that my function return (or need) a object with same type that the class I'm building? [duplicate]
I'm have an unexpected problem. This is a random example, but describe the problem it self.
With this code:
class Location:
def __init__(self, name: str) -> None:
self.name = name
...
0
votes
1
answer
57
views
Trying to center and uniformly space a 4x4 grid of entry widgets within a frame, within a frame class. What am I missing?
First up, I'm really pretty bad at OOP and tkinter/customtkinter and this is only my third project I've built with classes over straight functions, so please excuse my mess. I'm getting there, and ...
0
votes
0
answers
34
views
Having a problem with calling the class twice
import tkinter as tk
import math,time,random
from PIL import ImageTk, Image,ImageEnhance
start_time = time.time()
window = tk.Tk()
window.geometry("300x300")
canvas = tk.Canvas(window, ...
0
votes
0
answers
5k
views
How can I create one-to-many relationships in SQLAlchemy Flask?
How do I create a one-to-many relationship in a Flask SQLAlchemy database?
My db class:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String, ...
0
votes
1
answer
40
views
How can I substitute an imported python class in a .py file without modifying the file itself?
What I'd like to achieve is:
b.py(might be in a third party package I cannot modify):
from xxx import A
class B:
def __init__():
self.a = A()
The file I actually execute:
execute.py
...
100
votes
7
answers
43k
views
What are the advantages of class-based views compared to function-based views?
I always use FBVs (Function Based Views) when creating a django app because it's very easy to handle. But most developers said that it's better to use CBVs (Class Based Views) and use only FBVs if it ...
0
votes
0
answers
41
views
Unable to properly initialize a tkinter scrollbar inside a UI
I'm not an expert python programmer. After some time and effort I've built a tkinter UI that properly works. Recently, I've found myself needing to add a scrollbar inside the frame, since I need to ...
0
votes
2
answers
926
views
List all properties defined on a Pydantic BaseModel
Is there an elegant way to list all properties defined on a Pydantic BaseModel?
It's possible to extract the list of fields via BaseModel.model_fields and the list of computed fields via BaseModel....
0
votes
1
answer
165
views
Programatically create multiple instances of CustomTkinter Combobox Python
New to python and trying to create an app with customtkinter. The app lets users pick values from different drop-downs and generates codes based on the chosen value.
Updated per comment from Mike-SMT:
...
2
votes
1
answer
44
views
Python convert class instance variable to nested structure
Defining a class of instance variable from commonly used fields in API response by looping through each JSON record
self.id=response['identifier'] -->output: 1234
self.name=response['...
1
vote
1
answer
160
views
ROS (Robot Operating System) simple publisher and subscriber using class in python
I am trying to run a simple code in ROS to create a subscriber and a publisher using classes in python. I am new to using classes in python and I am not sure what I am doing wrong here.
Below is the ...
0
votes
0
answers
41
views
Updating class attributes in python
I have two classes Player and LineUp, one is used within the other (shown below). I am trying to update my LineUp class after I have already instantiated it.
class Player:
def __init__(self, ...
0
votes
1
answer
48
views
How is class Franchise, linked with Class Menu? in Below Python Code?
What informs the code that The relationship between the Franchise and Menu classes is that The Franchise class has an attribute menus that stores instances of the Menu class, when clearly Menu and ...
0
votes
1
answer
555
views
Error while using a custom class in Python: 'AttributeError: 'module' object has no attribute 'MyClass
I'm facing an issue while working with a custom class in Python. I have a module named 'my_module' with a class called 'MyClass'. However, when I try to instantiate the class in another script or ...
-2
votes
1
answer
476
views
Python Selenium AttributeError: object has no attribute
The problem is although i can see the method I'm calling, the error says I don't have a method the way I call. Here is the error:
Traceback (most recent call last):
File "C:\Users\htuna\...
12
votes
2
answers
5k
views
Python dataclass, one attribute referencing other
@dataclass
class Stock:
symbol: str
price: float = get_price(symbol)
Can a dataclass attribute access to the other one? In the above example, one can create a Stock by providing a symbol and ...
-2
votes
2
answers
118
views
Why are these 2 python class instance could equal to each other?
from dataclasses import dataclass
@dataclass
class ThreeDPoint:
x: int | float
y = 0.0
z: int | float = 0.0
point_1 = ThreeDPoint(1.0,2)
point_3 = ThreeDPoint(1,2)
print(point_1 == ...
0
votes
1
answer
72
views
Why can't you name an object, which is created in a function, exactly the same as it's class-name in Python?
I'm programing a Black Jack game in a Jupyter notebook and for that I have a "player" and a "dealer" class and also a function (BlackJack()) which basically runs the entire game.
def BlackJack():
...
5
votes
3
answers
4k
views
Python decorator in a class: missing required positional arguments:
class Something:
def __init__(self, ...):
...
def update(self):
...
def add_update(self, func):
def fct(*args, **kwargs):
self.update()
func(*args,...