Simple Class Program in Python

class MyClass:          # class is created
    variable = "blah"   # variable is created and assigned the value

    def function(self): # funtion is created iniside the class as self
        print "This is a message inside the class."
       
       
       
myobjectx = MyClass()     # object[myobjectx] is created for class
myobjectx.variable        # using created object variable in the class is accessed

myobjecty = MyClass()          # object[myobjecty] is created for class
myobjecty.variable = "yackity" # using created object variable in the class new value is assigned

print myobjectx.variable       # using corresponding object and variable the value is displayed
print myobjecty.variable       # using corresponding object and variable the value is displayed
print MyClass.variable         # using corresponding object and variable the value is displayed

myobjectx.function()           # using corresponding object and function inside the class is accessed

No comments: