더북(TheBook)

코드 6-17 oop1/oop1_2/account.py ①

class Account:
    num_acnt = 0           #1

    @classmethod
    def get_num_acnt(cls): #2
        '''
        cls.get_num_acnt() -> integer
        '''
        return cls.num_acnt

    def _ _init__(self, name, money):
        self.user = name   #3
        self.balance = money
        Account.num_acnt += 1  #4

    def deposit(self, money):
        vif money < 0:
             return
        self.balance += money

    def withdraw(self, money):
        if money > 0 and money <= self.balance:
            self.balance -= money
            return money
        else:
            return None

    def transfer(self, other, money):    #5
        '''
        obj.transfer(other, money) -> bool
        other : The object to interact with
        money : money the user wants to send

        returns True if the balance is enough to transfer
        returns False if not
        '''
        mon = self.withdraw(money)
        if mon:
            other.deposit(mon)
            return True
        else:
            return False

    def _ _str__(self):      #6
        return 'user : {}, balance : {}'.format(self.user, self.balance)
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.