Jun-04-2019, 06:29 PM
Hi,
I am a newbeee to python and had a prior java knowledge.
So, while learning python object oriented concepts, i am trying to relate to java (kinda easy to understand), but sometimes they don't seem to exactly match like in this case.
Here is what i am trying to understand.
Basically i am assuming that the class variables in python are similar to static variables in java.I did find some online explanations stating that class variables of python are similar to static variables in java.
i.e, while these variables are accessible without creation of an instance/object of the class, any change to these variables either through instance/object or class level would have effect in all the other instances of the class as well. At least that is the case in Java.
So i quickly performed the below test and noticed that is not the case in python.
Initially when i changed the value to "Class Name" using the class, all the instances had the same effect. But when i changed the value from an instance they were different (i was expecting all of them to change). Even surprising was the last assignment when i did it from the class level ("Class Name Again"), they were different (while the first time they were all same).
Can you explain
I am a newbeee to python and had a prior java knowledge.
So, while learning python object oriented concepts, i am trying to relate to java (kinda easy to understand), but sometimes they don't seem to exactly match like in this case.
Here is what i am trying to understand.
Basically i am assuming that the class variables in python are similar to static variables in java.I did find some online explanations stating that class variables of python are similar to static variables in java.
i.e, while these variables are accessible without creation of an instance/object of the class, any change to these variables either through instance/object or class level would have effect in all the other instances of the class as well. At least that is the case in Java.
So i quickly performed the below test and noticed that is not the case in python.
Initially when i changed the value to "Class Name" using the class, all the instances had the same effect. But when i changed the value from an instance they were different (i was expecting all of them to change). Even surprising was the last assignment when i did it from the class level ("Class Name Again"), they were different (while the first time they were all same).
Can you explain

class Customer: name = "" customer_1 = Customer() customer_2 = Customer() print("") print("ID of customer 1 ::", id(customer_1)) print("ID of customer 2 ::", id(customer_2)) print("") print("name of class customer ::", Customer.name) print("name of customer 1 ::", customer_1.name) print("name of customer 2 ::", customer_2.name) print("") print("Assigning new name by class ::") Customer.name = "Class Name" print("name of class customer ::", Customer.name) print("name of customer 1 ::", customer_1.name) print("name of customer 2 ::", customer_2.name) print("") print("Assigning new name by instance 1 ::") customer_1.name = "Customer 1" print("name of class customer ::", Customer.name) print("name of customer 1 ::", customer_1.name) print("name of customer 2 ::", customer_2.name) print("") print("Assigning new name by instance 2 ::") customer_2.name = "Customer 2" print("name of class customer ::", Customer.name) print("name of customer 1 ::", customer_1.name) print("name of customer 2 ::", customer_2.name) print("") print("Assigning new name by class ::") Customer.name = "Class Name Again" print("name of class customer ::", Customer.name) print("name of customer 1 ::", customer_1.name) print("name of customer 2 ::", customer_2.name)
ID of customer 1 :: 99586960 ID of customer 2 :: 147686456 name of class customer :: name of customer 1 :: name of customer 2 :: Assigning new name by class :: name of class customer :: Class Name name of customer 1 :: Class Name name of customer 2 :: Class Name Assigning new name by instance 1 :: name of class customer :: Class Name name of customer 1 :: Customer 1 name of customer 2 :: Class Name Assigning new name by instance 2 :: name of class customer :: Class Name name of customer 1 :: Customer 1 name of customer 2 :: Customer 2 Assigning new name by class :: name of class customer :: Class Name Again name of customer 1 :: Customer 1 name of customer 2 :: Customer 2