User Tools

Site Tools


python:class_instance_variables

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
python:class_instance_variables [2016/01/26 09:13]
admin
python:class_instance_variables [2016/01/26 10:23] (current)
Line 25: Line 25:
 === Examples === === Examples ===
  
-We're creating ​class with several variables, but with different ​behaviour.+Now couple of examples to understand ​different ​behaviours:
  
-<code python | Example of different types of variables>+<code python | Shared class variable ​>
 class Visitor: class Visitor:
     counter = 0     counter = 0
-     +
-    bags = []  # class variable, shared +
-    age = 0    # class variable, not shared +
-    ​+
     def __init__(self):​     def __init__(self):​
         type(self).counter += 1         type(self).counter += 1
-        ​self.items = [] # this will be an instance variable +        # similar Visitor.counter but it doesn'​t help with inheritance
-        ​self.bags = []  this will convert ​it to an instance variable ​+
         ​         ​
-    def add_bag(self,​ weight): 
-        self.bags.append(weight) 
-        ​ 
-    def set_age(self,​ age): 
-        self.age = age 
-        ​ 
-    def set_item(self,​ item_name): 
-        self.items.append(item_name) 
-    ​ 
 v1 = Visitor() v1 = Visitor()
-v1.add_bag(5+v2 = Visitor() 
-v1.set_age(15) +v3 = Visitor()
-v1.set_item('​t-shirt'​)+
  
-print("​Counter ​{}"​.format(Visitor.counter)) # or v1.counter ​- displays 1+print("​Final shared counter {} or {}"​.format(Visitor.counter, v2.counter)) 
 +Displays: Final shared ​counter ​3 or 3 
 +</​code>​
  
-v2 = Visitor() +Using self, you can transform a class variable into an instance variable.\\  
-v2.add_bag(12) +The class variable will exist separately of the instance one.
-v2.set_age(25) +
-v2.set_item('​trousers'​)+
  
-print("​Counter {}"​.format(Visitor.counter)) # displays 2 +<code python | Transformed class variable>​ 
-print("​Counter {}"​.format(v1.counter)) # alternative since counter is class variable ​+class Visitor: 
 +    counter ​= 0
  
 +    def __init__(self):​
 +        self.counter += 1
 +        # this is making the above counter variable - instance type 
 +        ​
 +v1 = Visitor()
 +v2 = Visitor()
 v3 = Visitor() v3 = Visitor()
-v3.add_bag(15) 
-v3.set_age(44) 
-v3.set_item('​blue suit') 
  
-print("​For the first visitor"​) +print("​Final shared counter ​{} or {}"​.format(Visitor.counter, v3.counter)) 
-print("​Age: ​{} "​.format(v1.age)) +# Final shared counter 0 or 1
-print("​Bags: ​{} "​.format(v1.bags))+
  
-print("For the third visitor"​+print(Visitor.__dict__
-print("Age: {} ".format(v3.age)+print(v1.__dict__
-print("​Bags:​ {} "​.format(v3.bags))+</​code>​
  
-print("​Now ​for instance variables - not shared between instances"​) +Mixing those two type will result in a strange behaviour ​for the programmer:
-print("​Items first visitor {}"​.format(v1.items)) +
-print("​Items third visitor {}"​.format(v3.items))+
  
-# here we create another reference to the same object +<code python | Messing with variables type> 
-v4 = v1 +class Visitor
-v4.set_age(33) +    ​counter = 0
-# the age would be updated for both references, due to the fact they'​re pointing to the same object +
-print("​Age{} "​.format(v1.age)) ​ +
-print(id(v1),​ id(v4))+
  
-avoid all this using deepcopy +    def __init__(self):​ 
-from copy import deepcopy +        self.counter += 1 
-v5 deepcopy(v1)+         
 +         
 +v1 = Visitor() 
 +Visitor.counter = 100  
 +print("​Counter v1: {}"​.format(v1.counter)) ​displays 1, v1 init already take place 
 + 
 +v2 Visitor(
 +Visitor.counter = 200 
 +print("​Counter v2: {}"​.format(v2.counter)) # displays 101 (previous 100 + 1) 
 + 
 +v3 = Visitor() 
 +print("​Counter ​v1: {}"​.format(v3.counter)) # displays 201 (previous 200 + 1)
 </​code>​ </​code>​
  
-You can check the code [[ https://repl.it/Bgr7/3 | here]].+Example with a different type of a shared variable: 
 + 
 +<code python | Using a list as shared variable>​ 
 +class Visitor: 
 +    items = [
 + 
 +    def __init__(self): 
 +        print("​Previous items: {}".format(self.items)) 
 +     
 +    def add_item(self,​ item_name):​ 
 +        self.items.append(item_name) 
 +         
 + 
 +    
 +v1 = Visitor() ​ # displays Previous items: [] 
 +v1.add_item("​suit"​) 
 + 
 +v2 = Visitor() ​ # displays Previous items: ['​suit'​] 
 +v2.add_item("​t-shir t") 
 + 
 +v3 = Visitor() ​ # displays Previous items: ['​suit',​ '​t-shirt'​] 
 +v3.add_item("​pullover"​) 
 +</​code>​
  
python/class_instance_variables.txt · Last modified: 2016/01/26 10:23 (external edit)