Make two instance objects and each has its own data : Class fields : Class PYTHON examples


PYTHON examples » Class » Class fields »

 

Make two instance objects and each has its own data


Make two instance objects and each has its own data
 

class MixedNames:                          # define class
    data = 'spam'                          # assign class attr
    def __init__(self, value):             # assign method name
        self.data = value                  # assign instance attr
    def display(self):
        print self.data, MixedNames.data   # instance attr, class attr

x = MixedNames(1)           
y = MixedNames(2)           
x.display() 
y.display()                 # self.data differs, Subclass.data same

           
         
  



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON examples

 Navioo Class
» Class fields