class AddrBookEntry(object): 'address book entry class' def __init__(self,nm,ph): self.name = nm self.phone = ph print 'created instance for:',self.name def updatephone(self,newph): self.phone = newph print 'update phone for:',self.name def updatename(self,newname): self.name = newname print 'update phone for:',self.phonejohn = AddrBookEntry('xiewenbin','13711710490')print john.nameprint john.phonejohn.updatephone('18617311540')john.updatename('xwb')print john.phoneprint john.nameclass EmpAddrBookEntry(AddrBookEntry): 'employee address book entry class' def __init__(self,nm,ph,id,em): AddrBookEntry.__init__(self,nm,ph) self.empid = id self.email = em def updateemail(self,newem): self.email = newem print 'update email address for:',self.namejone = EmpAddrBookEntry('jone doe','408-555-1212',42,'543361609@qq.com')print jone.nameprint jone.phoneprint jone.emailjone.updatephone('18617311541')print jone.phonejone.updateemail('186@qq.com')print jone.emailclass HotelRoomCalc(object): 'hotel room rate calculator' def __init__(self,rt,sales=0.084,rm=0.1): '''hotelroot calc default arguments: sales tax == 8.5% and room tax == 10%''' self.salestax = sales self.roomtax = rm self.rootrate = rt def cacltotal(self,days=1): 'calculator total;default to daily rate' daily = round((self.rootrate * (1 + self.roomtax + self.salestax)),2) return float(days) * dailysfo = HotelRoomCalc(299)print sfo.cacltotal(3)class TestStaticMethod(object): @staticmethod def foo(): print 'calling static method foo()'class TestClassMethod(object): @classmethod def foo(cls): print 'calling class method foo()' print 'foo() is part of class:',cls.__name__class C(object): foo = 100print C.foo + 1class Myclass(object): 'myclass class definition' myversion = 19.0 def showmyversion(self): print Myclass.myversionmc = Myclass()mc.showmyversion()print dir(Myclass)print Myclass.__dict__"""class InstCt(object): count = 0 def __init__(self): InstCt.count += 1 def __del__(self): InstCt.count -= 1 def howmany(self): return InstCt.counta = InstCt()b = InstCt()"""x = 3 + 0.14jprint x.__class__print [type(getattr(x,i)) for i in ('conjugate','imag','real')]class Foo(object): x = {2003:'poe2'}foo = Foo()print foo.xfoo.x[2004] = 'xie'print foo.xprint Foo.xdel foo.xprint foo.xprint Foo.x