So I know that a function defined inside a class is called a method. And normally, when we define a function, the variables declared inside the function are only local to that function, right. However, when it comes to class, instance variables defined inside
__init__ method can be shared with other methods defined in that class.
class CurrentAccount: def __init__(self, customer_name): self.name = customer_name self.balance = 0 def get_customer_name(self): return self.name def get_customer_balance(self): return self.balance account_holder = CurrentAccount("Edward") print(account_holder.get_customer_name())
get_customer_balance() are able to use instance variables from inside the
__init__ method. How is it able to use the variables from another method, I thought it’s only local to that method or is it because of
__init__, when I remove
__init__ or change it to something else, it just returns a
TypeError to me.