0

I'm trying to learn the difference between class variables and init ones. I understand the class variables are usually static and can change.

I would like to know why when you print the Variable Accounts after it had been changed by the instance of Sam it has changed for David.

HOWEVER, whenever you update the balance for Sam, it does not change for David. Thank you.

class BankAccount:
    Balance = 0
    Accounts = []

Sam = BankAccount()
David = BankAccount()

print("Sam's Balance", Sam.Balance)
print("David's Balance", David.Balance)
print("")
print("Sam's Accounts", Sam.Accounts)
print("David's Accounts", David.Accounts)


Sam.Balance = 1000
Sam.Accounts.append("Sam's Personal Checking")

print("Sam's Balance", Sam.Balance)
print("David's Balance", David.Balance)
print("")
print("Sam's Accounts", Sam.Accounts)
print("David's Accounts", David.Accounts)
3
  • 2
    You are binding a new variable Sam.Balance that hides the BankAccount.Balance vs. append()ing to the existing list. Even with Sam.Balance += 1000 would still hide BankAccount.Balance because ints are not mutable. Commented Nov 15, 2016 at 2:05
  • With Sam.Balance=1000 you are creating new instance variable. Commented Nov 15, 2016 at 2:09
  • When I added a print function within the class to print The global Variable Balance and self.Balance they were the same though? I'm a bit confused about that part. Commented Nov 15, 2016 at 3:02

3 Answers 3

1

Whenever you assign an attribute to an instance, it becomes an instance variable (not shared by any other instance), even if it was a class variable before. This is what happened in the case of Sam.Balance = 1000. However, Sam.Accounts.append("Sam's Personal Checking") is an entirely different situation - you did not assign anything, the Accounts list is exactly the same object as it was before, still a class variable. You simply made a change to the contents of that list, which is visible no matter how you retrieve the list.

Sign up to request clarification or add additional context in comments.

Comments

0

To illustrate the answers given: you can check to see if objects are the same thing using id() or the is operator.

>>> class BankAccount:
    Balance = 0
    Accounts = []


>>> a = BankAccount()
>>> b = BankAccount()
>>> id(BankAccount.Balance), id(a.Balance), id(b.Balance)
(507102960, 507102960, 507102960)
>>> BankAccount.Balance is a.Balance is b.Balance
True
>>> BankAccount.Accounts is a.Accounts is b.Accounts
True
>>> b.Balance = 2
>>> id(BankAccount.Balance), id(a.Balance), id(b.Balance)
(507102960, 507102960, 507103024)
>>> BankAccount.Balance is a.Balance is b.Balance
False
>>> a.Accounts.append(2)
>>> BankAccount.Accounts is a.Accounts is b.Accounts
True
>>>

Naming and Binding is a good read. And the Classes section of the Tutorial may provide insight.

1 Comment

Thank you so much this is a wonderful example, haha it's quite difficult for me to wrap my head around the full difference. I've been trying to read a lot about the difference between knit and class Variable. @wwii
0

Integer in Python is not mutable, so when Sam.Balance = 1000, this will add instance variable Balance to instance Sam, not change the class variable.

2 Comments

class BankAccount: Balance = 0 def PrintSelf(self): try: print(self.Balance) except: print("I have nit found myself yet") print(Balance) Sam = BankAccount() Sam.Balance = 1000 Sam.PrintSelf()
I'm on my iPad so return is automatically making me do that. Why in this code above does Balance == 1000 if it does not change the class variable? Thank you guys so much for the help!! @whbb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.