2,204,264 questions
Score of -1
1 answer
34 views
Why can a parameterized class be instantiated?
I have this generic class in Python 3.12:
class Box[T]:
def __init__(self, val: T):
self.val = val
According to the docs, I can instantiate it like this:
box = Box[int](42)
But don't ...
Score of 0
2 answers
58 views
Python API returns 8 and 22 instead of dates and symbols when selecting individual columns
I query a DFS table from Python. select * gives me correct dates and tickers, but as soon as I list the columns, TIME turns into 8 and CODE turns into 22 — the numeric columns are fine.
raw = session....
Score of 0
0 answers
38 views
Weird effects when calling pygame.display.set_mode() without explicitly initializing pygame.display
I ran the example from Pygame docs:
# Example file showing a basic pygame "game loop"
import pygame
# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame....
Score of 3
2 answers
65 views
Difference between three forms of instantiating a generic class in Python
I have this generic class in Python 3.12:
class Box[T]:
def __init__(self, val: T):
self.val = val
I can instantiate it in 3 ways:
box: Box[int] = Box(42)
box = Box[int](42)
box = Box(...
Score of 1
1 answer
100 views
Is there a way to use lazy imports in backwards-compatible code?
I'm writing some code that I want to make use of lazy imports, which were introduced in Python 3.15. However, I want the code to run fine on Python < 3.15 too, albeit without lazy imports.
Just ...
Score of -6
0 answers
73 views
Storing items in classes in python game development [closed]
Is there a way to make multiple copies of a class without specifying the variable name? Like how scratch has a "make clone of self" block. What I want is for there to be multiple items, so I ...
Score of 5
2 answers
103 views
Why does attrs treat a frozen attrs class differently from a frozen attrs field [duplicate]
I have these two attrs classes in Python:
import attrs
@attrs.define(frozen=True)
class FooA:
x: int
@attrs.define
class FooB:
x: int = attrs.field(on_setattr=attrs.setters.frozen)
In #1, ...
Score of -1
2 answers
115 views
Looking for a practical example where making a super call in the body of __init__ method of a metaclass makes a difference
Consider the following class Point and its metaclass PointMeta. What happens if I omit the super call in __init__ method of PointMeta class, is there any difference in behavior?
class PointMeta(type):
...
Score of 2
1 answer
122 views
Do I need a try/finally clause when calling cv2.VideoCapture.release()?
I'm learning how to capture video from camera using opencv for Python. The official tutorial provides the following example (modified):
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():...
Score of -5
0 answers
124 views
Dog Bark Emotion Analysis: Should I include a breed classification pre-model? [closed]
I am a Computer Science student working on a project that analyzes dog barks to determine the underlying emotion.
Aside from the complex data collection process (recording barks and their associated ...
Score of -5
1 answer
234 views
Python 2 vs. Python 3: what does "not backward compatible" mean exactly?
According to Wikipedia:
Python 3.0 was released on 3 December 2008, and was a major revision and not completely backward-compatible with earlier versions, with some new semantics and changed syntax.
...
Score of -8
0 answers
42 views
modifying one inner list in `[[0]] * 3` changes all three. How to avoid it? [duplicate]
In Python, when I create a nested list with grid = [[0]] * 3 and then run grid[0][0] = 5 or grid[0].append(1), the change appears in all three inner lists at once, even though they print as three ...
Score of 4
3 answers
169 views
Python 3.14: NameError for a self-referencing annotation, but only when it’s read in __init_subclass__
The Python 3.14 docs say annotations are now evaluated lazily, so forward references work without from __future__ import annotations. I removed that import from my mini ORM. Now defining any model ...
Score of -3
1 answer
169 views
Why can't I use key.A and key.B after importing Pyglet?
This Pyglet program doesn't seem to understand the key function and gives a NameError every time it runs. Does anyone know a solution to this?
import random
import pyglet
p=0
de=0
#opens window
window ...
Score of -9
0 answers
222 views
forecast the future price of a stock based on the past prices of other stocks [closed]
company1 = ['10', '11', '13', '15', '17', '19', '20']
company2 = ['20', '21', '23', '25', '27', '29', '30']
company3 = ['40', '41', '43', '45', '47', '49', '50']
The lists (company1, company2, ...