510 questions
0
votes
0
answers
33
views
How to fix Python problem with embedded class definitions
Working on LeetCode #1032, I'm having a problem that appears related to having embedded classes. Here is the code:
from collections import deque
class StreamChecker:
def __init__(self, words):
...
0
votes
1
answer
64
views
Attribute error when trying to going back and forth between functions in Python
I'm getting the error "AttributeError: 'NoneType' object has no attribute 'name'" for the code below:
class Student:
def __init__(self, name, house):
if not name:
...
0
votes
1
answer
710
views
Get init arguments of child class from parent class
getInitInpArgs on ann gets the input arguments in init of ann to a dictionary. Now I want to define a function like getInitInpArgs on ann (parent class) and not on the child classes, which makes a ...
0
votes
1
answer
526
views
How can I use cogs (discord.py)
I'm quite new to Python, but I understand how cogs work.
But what do I have to write in the main-File (to load all the cogs in a subfolder of my main.py) and what do I have to write in the cog?
And ...
0
votes
1
answer
100
views
How to resolve ModuleNotFoundError when importing a local Python file?
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 ...
1
vote
0
answers
47
views
Overriding a method definition with a method from another class?
class test:
def func(self):
print(5)
class test1:
def testing(self, t):
t.func = self.another_func
def another_func(self):
print(6)
t = test()
t1 = test1()
t1.testing(t)
# now t....
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
84
views
Function "doesn't recognise" a python class method
I have written a class that creates a board for a Klotski game and I want to use breadth-first search to solve the given problem. Here is a bit of my code:
EDIT and DISCLAIMER: I added all the ...
0
votes
1
answer
87
views
How to inherit one class implement by C
I want to add method for one class implemented by C
from lru import LRU
class A(LRU):
def foo(self):
print("a")
lru is a lib from https://github.com/amitdev/lru-dict
error:“...
0
votes
2
answers
164
views
Custom method decorator aware of a class [duplicate]
I had an idea for a simple decorator that would probably not be very useful (feel free to comments on that, but it's not my main focus here). Regardless of that, I think it'd show how to achieve ...
2
votes
2
answers
1k
views
Python "on_destroy" class method
I am wondering if there is a way to call a method before the object is finally "destroyed" - like some sort of event.
I am using a class and a static method. Since it is not an instantiated ...
1
vote
2
answers
226
views
How to generate methods to set and get properties at class instantiation with Python?
I have a class containing tensors that have an unpredictable list of properties.
I would like it to generate a list of properties with getters and setter based on a list given at instantiation.
Do hou ...
1
vote
1
answer
74
views
is "correct" or pythonic, this way of make getters or is better to use @property? [duplicate]
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) -&...
-2
votes
1
answer
96
views
I cannot define a function inside my class
I am trying to define a function inside of my class but still on line 31 'command=clear' does not see my function i created below. Please help, thank you!!
import tkinter as tk
from tkinter import *
...
0
votes
3
answers
54
views
copy.deepcopy(): Class with dictionary copied improperly
Good evening!
I faced a problem with copy.deepcopy(). I wanted to copy a class which had dict self variable and got this result:
>>> import copy
>>> class A:
... a = {'a': 1}
... ...
1
vote
2
answers
127
views
Making interdependent and auto-updating attributes in a Python class
I'm trying to make a class with 2 or more mutually dependent attributes (e.g. a and a-plus-1). The idea is to be able to initialize an object with one and only one of these 2 attributes and the other ...
1
vote
3
answers
93
views
Generating a random number (more than once) and using it inside two class functions
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 ...
0
votes
2
answers
51
views
[Beginner Python]Issues trying to access a function within a class
I am trying to run a simple budget program and I am a complete beginner with classes in python. I am trying to run everything within the class so I can call back to specific functions. The problem is ...
1
vote
1
answer
461
views
How to clone a python class object? (not the instance but the class itself)
Imagine you have the following code:
class A:
pass
NewA = ... # copy A
NewA.__init__ = decorator(A.__init__) # but don't change A's init function, just NewA's
I am looking for a way to change ...
1
vote
1
answer
81
views
Python decorate `class.method` that modify on `class.self`
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,...
2
votes
3
answers
691
views
Can you modify an object's field every time another field is modified?
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)...
0
votes
1
answer
712
views
AttributeError at type object 'Services' has no attribute 'app'
I newly started to develop Django. I want to connect my python algorithm and Django web ui so I tried to connect both of them in views.py but I encountered a problem.
It says AttributeError at type ...
1
vote
1
answer
317
views
Dynamically creating serializable classes in Python
I am trying to update a class that is supposed to be used as a custom type for the multiprocessing.manager and imitate a basic dictionary. All works well on Linux, but things fail on Windows and I ...
1
vote
1
answer
131
views
Selenium opens chrome and shows error TypeError: 'str' object is not callable using Python Classe
Im making an auth bot, but I faced with a problem! When Im trying to start my selenium bot, it opens few tabs in Chrome and do nothing. It also print that
"str" object is not callable
Can ...
0
votes
0
answers
21
views
Method inside a class printed without prompt [duplicate]
So I am following a tutorial and have the following code:
import random
class Coin:
def __init__(self, rare = False, clean = True, heads = True, **kwargs):
for key, value in kwargs.items(...