파이썬 초보 클래스 질문드려요
조회수 1063회
class BankAccount:
no_of_accounts = 0
def __init__(self, name, balance = 0):
self.name = name
if balance >= 0:
self.balance = balance
else:
self.balance = 0
def show_name(self):
if self.name:
print(self.name)
def show_balance(self):
if self.balance:
print(self.balance)
def deposit(self,amount):
if amount >= 0:
if isinstance(amount,int) == True:
self.balance += amount
def withdraw(self,amount):
if amount >= 0 and self.balance >= amount:
self.balance -= amount
이 코드에서 클래스에 객체가 하나 생성될때마다 no_of_accounts 에 1씩 더하고싶은데 어떻게하면 될까요? 매번 객체가 생성될때마다 init 메소드가 사용되서 init 메소드 내부에 no_of_accounts += 1 을 더하면 no_of_accounts 는 클래스 변수라 오류가 뜨네요
댓글 입력