Define function to change member data : Class Method « Class « Python






Define function to change member data

Define function to change member data
 


class NextClass:                            # define class
    def printer(self, text):                # define method
        self.message = text                 # change instance
        print self.message                  # access instance

x = NextClass()                             # make instance

x.printer('instance call')              # call its method

print x.message                               # instance changed

NextClass.printer(x, 'class call')      # direct class call

print x.message                               # instance changed again


           
         
  








Related examples in the same category

1.Class method Demo: define and callClass method Demo: define and call
2.Function defined outside the class
3.Getter and Setter Methods
4.roman Number