Example 1, constructors
#-*-coding:utf-8-*-Import SYSclassstudent:def __init__ (self,name,age): Self.__name=name Self.__age=Age def getName (self): format="My name is%s My age is%d"%(self.__name,self.__age) print format def __del__ (self): print"del"if__name__=="__main__": Studeng=student ("Chu", *) Studeng.getname ()
Example two: static members and private members
#-*-coding:utf-8-*-Import sysclass A: y=2 def __init__ ( Self): self.x=1 self.__z=1if __name__==" __main__ " : A=A () print a.x print a.y print a.__z
Example three:
#-*-coding:utf-8-*-Import sysclass A: def prt (self): "my Name is A" def reprt (self): a.prt (self)if __name__=="_ _main__": a=A () a.prt () a.reprt ()
Example four: hash lookup of a dictionary
#-*-coding:utf-8-*-Import SYSif__name__=="__main__": Dict={"a":"Apple","b":"Banana","g":"Grape","o":"Orange"} print dict print dict["a"] Dict2={1:"Apple",2:"Banana",3:"Grape",4:"Orange"} print dict2 print dict2[1]
Python example Two