Estoy intentando actualizar el valor de una variable de clase cuando alguien deposita dinero en mi banco. Mi clase de código Bank_account (): bank_balance = 0 def __init __ (self, name_holder, id, balance): ...
4 respuestas
Si es absolutamente necesario, intente lo siguiente:
(Observe que agregué: Bank_account.bank_balance
porque eso también inicializa la variable de clase. Verifique que la salida producida sea la que desea)
class Bank_account:
bank_balance = 0
def __init__(self, name_holder, id, balance):
self.name = name_holder
self.id = id
self.balance = balance
Bank_account.bank_balance += balance # change is here
def cash_in(self, cash):
self.balance += cash
bank_balance += cash
def with_draw(self, draw):
if self.balance < draw or bank_balance < draw:
print('Error')
else:
print('Here your money!')
John = Bank_account('John', 123456, 2.10**6)
Smith = Bank_account('Smith', 111111, 3.10**6)
Michael = Bank_account('Michael', 123123, 4.10**6)
print(Bank_account.bank_balance)
class Bank_account():
def __init__(self, name_holder, id, balance,bank_balance=0):
self.name = name_holder
self.id = id
self.balance = balance
self.bank_balance = balance
def cash_in(self, cash):
self.balance += cash
self.bank_balance += cash
def with_draw(self, draw):
if self.balance < draw or bank_balance < draw:
print('Error')
else:
print('Here your money!')
John = Bank_account('John', 123456, 2.10**6)
Smith = Bank_account('Smith', 111111, 3.10**6)
Michael = Bank_account('Michael', 123123, 4.10**6)
print(John.bank_balance)
Creo que esto es lo que estás tratando de hacer.
Clase Bank_account ():
bank_balance = 0
def __init__(self, name_holder, id, balance):
self.name = name_holder
self.id = id
self.balance = balance
self.bank_balance += balance
def cash_in(self, cash):
self.balance += cash
bank_balance += cash
def with_draw(self, draw):
if self.balance < draw or bank_balance < draw:
print('Error')
else:
print('Here your money!')
John = Cuenta_bancaria ('John', 123456, 2.1 - ** 6)
Smith = Cuenta_bancaria ('Smith', 111111, 3.10 ** 6)
Michael = Cuenta_bancaria ('Michael', 123123, 4.10 ** 6)
Imprimir (John.bank_balance)
Dado que el código dentro de una clase, pero fuera de los métodos / funciones no se ejecuta, la variable no se declara en la memoria. Entonces, la solución sería declararlo en el método init de la siguiente manera:
class Bank_account():
def __init__(self, name_holder, id, balance):
# This has been changed
bank_balance = 0
self.name = name_holder
self.id = id
self.balance = balance
bank_balance += balance
def cash_in(self, cash):
self.balance += cash
bank_balance += cash
def with_draw(self, draw):
if self.balance < draw or bank_balance < draw:
print('Error')
else:
print('Here your money!')
John = Bank_account('John', 123456, 2.10**6)
Smith = Bank_account('Smith', 111111, 3.10**6)
Michael = Bank_account('Michael', 123123, 4.10**6)
Bank_account.bank_balance
O si desea acceder al saldo bancario separado de cada objeto bank_account, simplemente agregue una línea que diga global bank_balance
de la siguiente manera:
class Bank_account():
def __init__(self, name_holder, id, balance):
# This has been changed
global bank_balance
bank_balance = 0
self.name = name_holder
self.id = id
self.balance = balance
bank_balance += balance
def cash_in(self, cash):
self.balance += cash
bank_balance += cash
def with_draw(self, draw):
if self.balance < draw or bank_balance < draw:
print('Error')
else:
print('Here your money!')
John = Bank_account('John', 123456, 2.10**6)
Smith = Bank_account('Smith', 111111, 3.10**6)
Michael = Bank_account('Michael', 123123, 4.10**6)
Bank_account.bank_balance
Pero recomiendo encarecidamente agregar self
antes de la variable bank_balance
Preguntas relacionadas
Nuevas preguntas
python
Python es un lenguaje de programación multipropósito, de tipificación dinámica y de múltiples paradigmas. Está diseñado para ser rápido de aprender, comprender y usar, y hacer cumplir una sintaxis limpia y uniforme. Tenga en cuenta que Python 2 está oficialmente fuera de soporte a partir del 01-01-2020. Aún así, para preguntas de Python específicas de la versión, agregue la etiqueta [python-2.7] o [python-3.x]. Cuando utilice una variante de Python (por ejemplo, Jython, PyPy) o una biblioteca (por ejemplo, Pandas y NumPy), inclúyala en las etiquetas.