This example is provided to show how to make python objects.
#---------------------------------------------
# Traditional procedural approach
#---------------------------------------------
a=100
b=200
c=300
def spam(v):
a=v
def eggs(v):
b=v
def ham(v):
c=v
spam(10)
eggs(20)
ham(30)
print a,b,c
# yields 10,20,30
#---------------------------------------------
# object oriented approach
#---------------------------------------------
class abc:
def __init__(self,a=100,b=200,c=300):
self.a=a
self.b=b
self.c=c
def spam(self,v):
self.a=v
def eggs(self,v):
self.b=v
def ham(self,v):
self.c=v
myAbc=abc()
print myAbc.a, myAbc.b, myAbc.c
# yields 100,200,300
myAbc.spam(10)
myAbc.eggs(20)
myAbc.ham(30)
print myAbc.a, myAbc.b, myAbc.c
# yields 10,20,30
anotherAbc=abc(10,20,30)
anotherAbc.eggs(40)
print anotherAbc.a, anotherAbc.b, anotherAbc.c
# yields 10,40,30
|