Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么可以';更改另一个对象的实例变量_Python_Class - Fatal编程技术网

Python 为什么可以';更改另一个对象的实例变量

Python 为什么可以';更改另一个对象的实例变量,python,class,Python,Class,嗨,我真的找到了解决问题的办法。然而,我只是想知道为什么我不能通过另一个对象的实例方法(hello.deposit)来更改一个对象实例变量(bye.balance)。相反,我必须通过另一个对象实例方法调用另一个实例方法。我想问问这里所有的编程高手是否还有其他方法 问题:基本上,如果我通过hello.deposit更改bye.balance,则bye.balance仍为0 class People(object): def __init__(self, name , cash, coun

嗨,我真的找到了解决问题的办法。然而,我只是想知道为什么我不能通过另一个对象的实例方法(hello.deposit)来更改一个对象实例变量(bye.balance)。相反,我必须通过另一个对象实例方法调用另一个实例方法。我想问问这里所有的编程高手是否还有其他方法

问题:基本上,如果我通过hello.deposit更改bye.balance,则bye.balance仍为0

class People(object):

    def __init__(self, name , cash, count):
        self.cash=cash
        self.name=name
        self.count=count
        
    def __str__(self):       
        rep=str(self.count) + '\t'+ self.name
        return rep
    
    def deposit(self, dep_amount, account):
        self.cash-=dep_amount
        bye.balance+=dep_amount 


        
class Account(object):
    
    def __init__(self, name, count, balance=0):
        self.name=name
        self.count=count
        self.balance=balance
        
    def __str__(self):
        rep=str(self.count)+'\t'+'account by '+self.name
        return rep

    ##def add(self, dep):
        ##self.balance+=dep

hello=People('cz', 1000, 0)
bye=Account('cz', 0)
dep=int(input('dep amount enter pls'))
hello.deposit(dep, bye.balance)
print(hello.cash, bye.balance)
hello.cash=12345
print(hello.cash)
我想你的意思是:

您好。存款(副总裁,再见)
存款方法可以是:

def存款(自身、折旧金额、账户):
自付现金-=折旧金额
账户余额+=折旧金额

噢,哇,我想我可以让self.balance=account,然后从那里算起!谢谢但是我想问一下,你是否知道为什么我不能让self.balance作为位置论证的逻辑原因。当我使用hello.deposit方法时,因为在python中,
int
是不可变的,所以
account+=dep\u amount
在调用
hello.deposit(dep,bye.balance)
时不会更改
bye.balance
。我的
deposit()
实现具有
account.balance+=dep\u amount
,它将新的
int
值重新分配给
account.balance