510 questions
1
vote
2
answers
2k
views
Check if value in an array of objects
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 ...
0
votes
0
answers
68
views
How to implement an adaptive information filter of a CSR sparse matrix representing an article and integers 0-4 representing the topic of the article?
I have the following tuple stored in a variable called newsfeed, which contains a row vector in the form of a CSR sparse matrix of shape (1, 9635) representing a news article and an integer from 0-4 (...
1
vote
2
answers
231
views
Changing Python class methods after class definition–is it harmful/harmless/useful/useless?
I found out that it is possible to assign to class variables after class definitions and that methods are technically class variables. So I tried the following, which appeared to work.
class Fruit():
...
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 ...
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):
...
1
vote
1
answer
66
views
Can a python class have multiple grandparents?
So I just come across a function called super() that will call every parent's version of the function, but when I tried several different things there are things that i didn't quite understand. Here, ...
0
votes
0
answers
24
views
How are these 2 instance attributes are connected to each other?
class Library :
def __init__(self,booklist) :
self.permabooklist = booklist
self.books = booklist
sonipatlibrary = Library(["C++","Django","Flask&...
-1
votes
3
answers
261
views
[Python]: How to update python property in a good way
I'm new to Python and I'm working on a crawler project.
I have a case want to ask you about good way to handle.
For example.
class Student:
def __init__(
self,
user_id: str,
name: str = ...
1
vote
0
answers
97
views
How could I create a docstring decorator in the presence of properties?
I have a collection of ever more specialized classes which correspond to collections of the same kind of data (temperature, density, etc) but for different drifts, for example, one subclass has ...
-1
votes
2
answers
949
views
python oop class attribute returns None when set
I'm trying to learn object orientated programming and when setting an attribute why does it return None?
Any tips on writing better code also appreciated... This is my class:
BEANS = ["black"...
0
votes
1
answer
4k
views
Working with Paho MQTT client in a Python class
I'm trying to set up an MQTT connector using a Python class.
The problem I'm facing is that self.connected is always printed as False, even if in the on_connect module I've changed its value.
Moreover,...
0
votes
2
answers
891
views
Forcing read-only Parameter / property in __init__ class constructor in Python
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 ...
0
votes
3
answers
76
views
Python - init argument not used in self [closed]
I came by a code and wanted to get more understanding on when to use such arrangement.
What would be a good scenario of not using __init__'s argument (name) in self?
class ArgumentNotUsedInSelf:
...
0
votes
3
answers
127
views
Trying to get a 2d array from class but instead getting <class '__main__.className'>
queue = []
goal_grid = {
1: [0, 0],
2: [0, 1],
3: [0, 2],
4: [1, 0],
5: [1, 1],
6: [1, 2],
7: [2, 0],
8: [2, 2]
}
sol = 0
visited = []
class State:
def __init__(...
0
votes
0
answers
78
views
Python/class-How do I convert this existing program into object-oriented program(Classes)?
I wanted to make a program which is CLI based login/register platform. I have made a version of this without using classes;
file = open(r"database.txt","a+")
def register():
...
3
votes
2
answers
2k
views
Wrapping derived class method from base class
Imagine I have a base and a derived class like so:
class A:
def foo(self):
pass
class B(A):
def foo(self):
pass
I wish to wrap calls foo calls made by instances of B. ...
0
votes
2
answers
125
views
Problem with defining derived class in python
I am learning how to use classes in python to alter some Keras methods to create various forms of Generative Adversarial Networks, GANs. In this case, I am trying to implement the gradient penalty ...
1
vote
0
answers
453
views
How do I in a Pythonic way keep HEADER, API Params & Request Object [of a Web Scraper] separate but easily accessible and mutable?
I would like to write a scraper that has 3 different "groups" of attributes (or data) that would [and likely should] be kept separately.
I was hoping to use DataClasses and aim at Pythonic ...
0
votes
0
answers
39
views
I want to access a stored variable from one class from another class in python
I am simulating a person running with an enemy chasing the person. I am struggling in finding the code for the movement of the enemy.
class Person ():
def __init__(self, limits, name)
self....
0
votes
1
answer
281
views
How to change multiple labels in multiple classes with a tkinter dropdown menu
So, I want the user to be able to use my program in English. They should be able to switch to their language via a dropdown-menu. My program has only four pages and a few labels on each, translating ...
0
votes
2
answers
248
views
Trying to destroy a class Frame in tKinter GUI
I'm trying to simulate a GUI as part of a larger card game, using python's tKinter. This GUI asks the user to enter a desired number of cards, press the deal button, and then it updates the buttons on ...
0
votes
1
answer
957
views
I don't understand how nodes work in a linked list [closed]
I have a basic understanding of usage of nodes, like node = node.next, and self.head, and things like
node.next = self.head
self.head = new_node
to add whatever is in new_node to the beginning of the ...
0
votes
1
answer
236
views
Pass Wx Frame class as a variable to another Class
I am trying to pass a WX frame class to another class. I have three py files which are as follows:
gui_20220510.py - this contains the gui code
import wx
class Frame_Demo(wx.Frame):
def __init__(...
0
votes
0
answers
56
views
Saving a class variable as a number
Want to use class to make my code concise and easier to pass around to others.
class distance:
.05 = x
.10 = y
.15 = z
...
I can't name something a number is there a way to get around ...
0
votes
0
answers
227
views
Are classes'objects, in python 3.9, passed by reference?
I have read, multiple times, that python passes arguments by value. However, I was testing the following simple code, and it looks like the class c objects are passed by reference (a modification ...