python—这是创建对象的好方法吗?

vc6uscn9  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(203)

我试图用一种方法创建一个对象,该方法将简单地从数量中减去数量,并更新余额。这是一个好方法吗?

class Potatoes():
    def __init__(self, size, farmers, qty_in_out=0):
        self.size = size
        self.farmers = farmers
        self.qty_in_out = {
            'stock_in': qty_in_out,
            'stock_out': [0],
            'stock_balance': qty_in_out
        }

    def stock_out(self, amount_out):
        self.qty_in_out['stock_out'].append(amount_out)
        sum_number = 0
        for i in self.qty_in_out['stock_out']:
            sum_number += i
        if self.qty_in_out['stock_in'] > sum_number:
            self.qty_in_out['stock_balance'] = self.qty_in_out['stock_in'] - sum_number
            # print('balance==', self.qty_in_out['stock_in'] - sum_number)
        else:
            arr = self.qty_in_out['stock_out']
            x = arr[len(arr) - 1]
            arr.remove(x)
            self.qty_in_out['stock_out'] = arr
            print(self.farmers.upper(), 'stock out is greater than stock in!!!!!')

malemba = Potatoes(size='7kg', farmers='malemba', qty_in_out=10)
top_crop = Potatoes(size='10kg', farmers='top_crop', qty_in_out=1000)
malemba.stock_out(100)
top_crop.stock_out(3)

# del top_crop

ic(malemba.__dict__, top_crop.__dict__)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题