如何在python中销毁属性内的对象?

时间:2017-11-25 00:14:24

标签: python python-3.x class methods destructor

我想破坏代码中的对象,如果满足某个条件,但我无法这样做。谁能帮帮我吗? 以下是代码:

class bank:
    name="State bank"
    no_of_user=0
    def __init__(self,name,amount):
        self.ac_name=name
        self.ac_balance=amount
        bank.no_of_user+=1
        print('Welcome')
        return
    def __del__(self):
        bank.no_of_user-=1

    def withdraw(self,amount):
        if self.ac_balance>amount>0:
            self.ac_balance-=amount
            print('An amount of %d is debited from your account and current balance is %d' %(amount,self.ac_balance))
        elif self.ac_balance==amount:
            ask=input('Do you want to close your account(y/n): ')
            if ask=='y'or'Y':
                del(self)
            else:
                self.ac_balance-=amount
                print('An amount of %d is debited from your account and current balance is %d' %(amount,self.ac_balance))
        else:
            if amount==0:
                print('Enter a valid amount ')
            else:
                print('insufficient funds ')

以下是我得到的输出:

IN:a = bank(' Asish',35000)

IN:a.withdraw(35000)

OP:您要关闭帐户(y / n):y

现在我打算用代码删除对象' a'并减少'no_of_user'。但是,这两者都没有发生。而且,即使我给出的价值是“问”'作为' n'

IN:a.withdraw(35000)

OP:您要关闭帐户(是/否):n

代码并未减少帐户余额。有人可以帮我解决问题吗?

0 个答案:

没有答案