I've created some Object Oriented Python code below, where a user instance has been created (called Ben). The functions work correctly however, I have found that when the last line is changed to ben.check_money = 100, no error is thrown. I know that this is not correct syntax. Adding the correct function call back in however, throws the listed error: TypeError: 'int' object is not callable
Original Code:
class User:
def __init__(self):
self.money = 200
self.score = 0
print('Created ')
def increase_money(self, amount):
self.money += amount
def decrease_money(self, amount):
self.money -= amount
def check_money(self):
print(self.money)
ben = User()
ben.check_money() # No error is thrown here
Modified Code 1;
class User:
def __init__(self):
self.money = 200
self.score = 0
print('Created ')
def increase_money(self, amount):
self.money += amount
def decrease_money(self, amount):
self.money -= amount
def check_money(self):
print(self.money)
**ben = User()
ben.check_money = 100 # No error thrown here
ben.check_money() # Error is thrown here**
Modified Code 2;
class User:
def __init__(self):
self.money = 200
self.score = 0
print('Created ')
def increase_money(self, amount):
self.money += amount
def decrease_money(self, amount):
self.money -= amount
def check_money(self):
print(self.money)
**ben = User()
ben.check_money = 100 # No error thrown here
ben.check_money # No Error is thrown here**
My question is; why does the error only occur in certain situations, depending on how you have previous called it? One would assume that it should throw an error for both Modification code 1 and Modification code 2.