Python's class method is not well understood.

Asked 6 days ago, Updated 6 days ago, 1 views

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 Bank"

        # 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
                # Right now
                self.balance = (self.balance * 1.01)


    def withdraw(self, amount):
        if self.balance > amount:
            self.balance -= amount

    def display_info(self):
        print("Bank Name: ", self.bank)
        print("Deposit: ", self.name)
        print("Account number: ", self.account_number)
        print("Balance: ", self.balance)

p = Account ("Python", 10000)
p.deposit(10000)
p.deposit(10000)
p.deposit(10000)
p.deposit(5000)
p.deposit(5000)
print(p.balance)

The above is the answer to the example.

import random

class Account:
    count=0
    in_count=0

    def __init__(self, name, money):
        self.name=name
        self.money=money
        self.bank="scbank"

        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 ("Bank Name:", self.bank)
        print("Deposit:", self.name)
        print ("Account Number:", self.num)
        print("Balance: {:,} won".format(self.money))

    @classmethod
    def get_account_num(cls):
        print(cls.count)

This is the answer I made, and in my case, I added the contents to the original method without using the class method, but the results are the same.

If so

class python

2022-09-20 15:32

1 Answers

Look at the code below and hope you will have a little deeper understanding of the class and instance.

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('Full :', Account.get_account_num())

    p1 = Account ("Joo Jihoon", 10000)
    print(p1.get_name(), p1.get_money())
    print('Full:', p1.get_account_num()) #ClassvariableAccount.count

    print()

    p2 = Account ("Badonna", 20000)
    print(p2.get_name(), p2.get_money())
    print('Full :', p2.get_account_num()) #ClassvariableAccount.count

    print()

    p3 = Account ("Jeon Ji-hyun", 30000)
    print(p3.get_name(), p3.get_money())
    print('Full:', p3.get_account_num()) #ClassvariableAccount.count    

    print()
    print('Full :', Account.get_account_num())


2022-09-20 15:32

If you have any answers or tips


© 2022 pinfo. All rights reserved.