1 import os 2 3 class Dog(object): 4 def __init__(self,name): 5 self.name = name 6 7 @staticmethod #此方法實際上跟類已經沒什麼關係了,此時就是單純的函數,沒法直接調用類變量和實例變量,可是能夠經過傳進去實例調用實例變量 8 def eat(self): 9 print("%s is eating %s" %(self.name,'dd')) 10 11 def talk(self): 12 print("%s is talking"% self.name) 13 d = Dog("ChenRonghua") 14 d.eat(d) 15 d.talk()
1 import os 2 3 class Dog(object): 4 name = "huazai" 5 def __init__(self,name): 6 self.name = name 7 @classmethod #只能訪問類變量,不能訪問實例變量 8 def eat(self): 9 print("%s is eating %s" %(self.name,'dd')) 10 11 def talk(self): 12 print("%s is talking"% self.name) 13 14 15 d = Dog("ChenRonghua") 16 d.eat()
1 import os 2 3 class Dog(object): 4 '''這個類是描述狗這個對象的''' 5 6 def __init__(self,name): 7 self.name = name 8 self.__food = None 9 10 @property #把一個方法變成靜態屬性,調用的時候不須要加() 11 def eat(self): 12 print("%s is eating %s" %(self.name,self.__food)) 13 @eat.setter 14 def eat(self,food): 15 print("set to food:",food) 16 self.__food = food 17 @eat.deleter 18 def eat(self): 19 del self.__food 20 print("刪完了") 21 22 d = Dog("ChenRonghua") 23 d.eat 24 d.eat="baozi" 25 d.eat 26 del d.eat 27 d.eat
1 aa.py 2 3 class C: 4 5 def __init__(self): 6 self.name = 'alex' 7 8 index.py 9 10 from lib.aa import C 11 obj = C() 12 print(obj.__module__) # 輸出 lib.aa,即:輸出模塊 13 print(obj.__class__ ) # 輸出 lib.aa.C,即:輸出類
1 class Foo: 2 def __init__(self): 3 pass 4 5 def __call__(self, *args, **kwargs): 6 7 print '__call__' 8 9 obj = Foo() # 執行 __init__ 10 obj() # 執行 __call__
1 def func(self): 2 print('hello %s' %self.name) 3 def __init__(self,name,age): 4 self.name = name 5 self.age = age 6 7 Foo = type('Foo', (object,), {'talk': func, 8 '__init__':__init__}) 9 f = Foo("Chrn",22) 10 f.talk() 11 print(type(Foo))
1 def bulk(self): 2 print("%s is yelling...." %self.name) 3 4 class Dog(object): 5 def __init__(self,name): 6 self.name = name 7 8 def eat(self,food): 9 print("%s is eating..."%self.name,food) 10 11 d = Dog("NiuHanYang") 12 choice = input(">>:").strip() 13 14 if hasattr(d,choice): #判斷是否有choice輸入的這個方法 15 func=getattr(d,choice) #有,則調用類方法 16 func("chenronghua") 17 else: 18 setattr(d,choice,bulk) #給類添加新的方法bulk 19 func = getattr(d, choice) #調用剛剛添加的新方法 20 func(d)
1 try: 2 # 主代碼塊 3 pass 4 except KeyError,e: 5 # 異常時,執行該塊 6 pass 7 else: 8 # 主代碼塊執行完,執行該塊 9 pass 10 finally: 11 # 不管異常與否,最終執行該塊 12 pass
1 names = ['alex','jack'] 2 try: 3 data['name'] 4 except KeyError as e : #每種錯入一種處理方法,若是前兩種處理方法同樣則except (KeyError,IndexError) as e,若是全部錯誤使用同一種處理方法則except Exception as e 5 print("沒有這個key",e) 6 except IndexError as e : 7 print("列表操做錯誤",e) 8 except BaseException as e: #除去以上兩種以外的其餘錯誤,或者在最後面使用Exception 9 print("未知錯誤",e) 10 else: 11 print("一切正常") 12 finally: 13 print("無論有沒有錯,都執行")
1 #主動觸發異常 2 try: 3 raise Exception('錯誤了。。。') 4 except Exception,e: 5 print e 6 7 #自定義異常 8 class WupeiqiException(Exception): 9 10 def __init__(self, msg): 11 self.message = msg 12 13 def __str__(self): 14 return self.message 15 16 try: 17 raise WupeiqiException('個人異常') 18 except WupeiqiException,e: 19 print e
1 class AlexError(Exception): 2 def __init__(self, msg): 3 self.message = msg 4 5 try: 6 raise AlexError('數據庫連不上') 7 except AlexError as e: 8 print(e)
1 #客戶端 2 import socket 3 4 client = socket.socket() #聲明socket類型,同時生成socket鏈接對象 5 client.connect(('localhost',6969)) 6 7 while True: 8 msg = input(">>:").strip() 9 if len(msg) == 0:continue 10 client.send(msg.encode("utf-8")) 11 data = client.recv(1024) #一次最大接收1024個字節 12 print("recv:",data.decode()) 13 14 client.close()
1 #服務器端 2 import socket 3 server = socket.socket() 4 server.bind(('localhost',6969)) #綁定要監聽端口 5 server.listen() #監聽 6 7 print("我要開始等電話了") 8 while True: 9 conn, addr = server.accept() # 等電話打進來 10 # conn就是客戶端連過來而在服務器端爲其生成的一個鏈接實例 11 print(conn, addr) 12 print("電話來了") 13 while True: 14 data = conn.recv(1024) 15 print("recv:",data) 16 if not data: #接收到的數據爲空則斷開 17 print("client has lost...") 18 break 19 conn.send(data.upper()) 20 21 server.close()