When I create an ordinary object, like below, I can assign different variables to it that hasn’t been defined in the __init__()
method:
class Test:
pass
test = Test()
test.Something = 0
but in the code below, I can't.
import pygame
pygame.init()
screen = pygame.display.set_mode()
print(type(screen))
red = (255, 0, 0)
blue = (0, 0, 255)
screen.turn = 0
while True:
if (screen.turn):
screen.fill(blue)
else:
screen.fill(red)
screen.turn = 1 - screen.turn
pygame.event.pump()
pygame.display.update()
Instead, an AttributeError
exception is raised and the program gives the output below:
pygame 2.1.2 (SDL 2.0.18, Python 3.9.13)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "/Users/armaho/Programs/Python/main.py", line 10, in <module>
screen.turn = 0
AttributeError: 'pygame.Surface' object has no attribute 'turn'
<class 'pygame.Surface'>
Why is that?