파이썬의 클래스 메소드가 잘 이해되지 않습니다.
조회수 604회
import random
class Account:
# class variable
account_count = 0
def __init__(self, name, balance):
self.deposit_count = 0
self.name = name
self.balance = balance
self.bank = "SC은행"
# 3-2-6
num1 = random.randint(0, 999)
num2 = random.randint(0, 99)
num3 = random.randint(0, 999999)
num1 = str(num1).zfill(3) # 1 -> '1' -> '001'
num2 = str(num2).zfill(2) # 1 -> '1' -> '01'
num3 = str(num3).zfill(6) # 1 -> '1' -> '0000001'
self.account_number = num1 + '-' + num2 + '-' + num3 # 001-01-000001
Account.account_count += 1
@classmethod
def get_account_num(cls):
print(cls.account_count) # Account.account_count
def deposit(self, amount):
if amount >= 1:
self.balance += amount
self.deposit_count += 1
if self.deposit_count % 5 == 0: # 5, 10, 15
# 이자 지금
self.balance = (self.balance * 1.01)
def withdraw(self, amount):
if self.balance > amount:
self.balance -= amount
def display_info(self):
print("은행이름: ", self.bank)
print("예금주: ", self.name)
print("계좌번호: ", self.account_number)
print("잔고: ", self.balance)
p = Account("파이썬", 10000)
p.deposit(10000)
p.deposit(10000)
p.deposit(10000)
p.deposit(5000)
p.deposit(5000)
print(p.balance)
위의 것이 예제의 답안이고
import random
class Account:
count=0
in_count=0
def __init__(self, name, money):
self.name=name
self.money=money
self.bank="sc은행"
num1=random.randint(0,999)
num2=random.randint(0,99)
num3=random.randint(0,999999)
if len(str(num1))<3:
num1=str(num1).zfill(3)
else:
num1=str(num1)
if len(str(num2))<2:
num2=str(num2).zfill(2)
else:
num2=str(num2)
if len(str(num3))<6:
num3=str(num3).zfill(6)
else:
num3=str(num3)
self.num=num1+"-"+num2+"-"+num3
Account.count=Account.count+1
def deposit(self, in_money):
if in_money>=1:
self.money=self.money+in_money
Account.in_count=Account.in_count+1
if Account.in_count%5==0:
self.money=self.money+(self.money*0.01)
def withdraw(self, out_money):
if out_money<self.money:
self.money=self.money-out_money
def display_info(self):
print("은행이름:",self.bank)
print("예금주:", self.name)
print("계좌번호:", self.num)
print("잔고:{:,}원".format(self.money))
@classmethod
def get_account_num(cls):
print(cls.count)
이것이 제가 만든 답인데 저같은 경우 클래스 메소드를 사용하지 않고 원래 있던 메소드에 내용을 추가하였는데 결과는 같게 나옵니다.
그렇다면
- 클래스 메소드가 굳이 필요한지
- 클래스 메소드의
cls
가self
로 바꿀 수 있거나, 클래스의self
처럼self
외에 다른 문자로 표시해도 상관 없는 것이라면 클래스 메소드와 일반적인 메소드가 실질적으로 차이가 있는 것인지, 어떠한 차이인지 알고 싶습니다.
1 답변
-
아래 코드를 보시고 클래스와 인스턴스에 대한 이해가 조금이라도 깊어지기를 바랍니다.
class Account: count = 0 # def __init__(self, name, money): self.name = name self.money = money Account.increase_account_num() def get_name(self): return self.name def get_money(self): return self.money @classmethod def increase_account_num(cls): cls.count += 1 @classmethod def get_account_num(cls): return cls.count def main(): print() print('전체 :', Account.get_account_num()) p1 = Account("주지훈", 10000) print(p1.get_name(), p1.get_money()) print('전체 :', p1.get_account_num()) # 클래스 변수 Account.count print() p2 = Account("배두나", 20000) print(p2.get_name(), p2.get_money()) print('전체 :', p2.get_account_num()) # 클래스 변수 Account.count print() p3 = Account("전지현", 30000) print(p3.get_name(), p3.get_money()) print('전체 :', p3.get_account_num()) # 클래스 변수 Account.count print() print('전체 :', Account.get_account_num())
댓글 입력