파이썬 @staticmethod 사용 이유와 코드 내 __repr__의 작동원리가 궁금합니다
조회수 317회
유튜브를 보고 지뢰찾기를 만드는 도중에 궁금증이 생겨서 질문드립니다. 유튜브 주소는 https://www.youtube.com/watch?v=OqbGRZx4xUc&list=LL&index=2&t=3782s 입니다.
메인 파일
from tkinter import *
from cell import Cell
import settings
import utils
root=Tk()
#override the settings of the window
root.configure(bg="black")
root.geometry(f'{settings.WIDTH}x{settings.HEIGHT}')
root.title("Minesweeper Game")
root.resizable(False, False)
top_frame = Frame(
root,
bg='black',
width=settings.WIDTH,
height=utils.height_prct(25)
)
top_frame.place(x=0, y=0)
left_frame = Frame(
root,
bg='black',
width=utils.width_prct(25),
height=utils.height_prct(75)
)
left_frame.place(x=0, y=utils.height_prct(25))
center_frame = Frame(
root,
bg='black',
width=utils.width_prct(75),
height=utils.height_prct(75)
)
center_frame.place(
x=utils.width_prct(25), y=utils.height_prct(25)
)
for x in range(settings.GRID_SEZE):
for y in range(settings.GRID_SEZE):
c = Cell(x,y)
c.create_btn_object(center_frame)
c.cell_btn_object.grid(
column=x,row=y
)
print(Cell.all)
#Cell 파일
from tkinter import Button
class Cell:
all = []
def __init__(self,x,y, is_mine=False):
self.is_mine = is_mine
self.cell_btn_object = None
self.x=x
self.y=y
Cell.all.append(self)
def create_btn_object(self, location):
btn = Button(
location,
width=12,
height=4,
text=f'{self.x},{self.y}'
)
btn.bind('<Button-1>',self.left_click_actions) # Left Click
btn.bind('<Button-3>',self.right_click_actions) # Right Click
self.cell_btn_object = btn
def left_click_actions(self,event):
print(event)
print("I am left clicked!")
def right_click_actions(self,event):
print(event)
print("I am right clicked!")
@staticmethod
def randomize_mines():
pass
def __repr__(self):
return f"Cell({self.x},{self.y})"`
첫번째로 궁금한 것은 @staticmethod 사용 이유와 두번째론
Cell 파일 상단부를 보면 ```class Cell: all = [] def init(self,x,y, is_mine=False): self.is_mine = is_mine self.cell_btn_object = None self.x=x self.y=y
Cell.all.append(self)
인데 이미 Cell.all.append(self)로 all 파일에 정보를 저장했는데 어떻게 `def __repr__(self):`이 영향을 줄 수 있는지가 궁금합니다.
댓글 입력