python 的一些特性

2021-07-03 13:11:21 字數 2415 閱讀 6380

class student(object):

say="this is a student class"

def __init__(self,name,age):#構造器

self.name = name

self.age = age

@classmethod#類方法

def showclassmethod(cls):

print "call classmethod"

print cls.say

@staticmethod#靜態方法,屬於類

def showstaticmethod():

print "call static method"

print student.say

@property#包裝器定義屬性

def ***(self):

print "call property"

return self._***

@***.setter#包裝器設定***

def ***(self,value):

print "call setter"

self._*** = value

def get***(self):#外部獲取***屬性

return self._***

def showall(self):#類物件呼叫

print "call instance method"

print self.age,self.name,self.***,self.get***()

def __del__(self):#析構函式

print "call destructor"

def __str__(self):#獲取類物件的字串描述

return "student object:(name:%s)(age:%d)" % (self.name,self.age)

def __repr__(self):#除錯用 例如在控制台呼叫:s

return self.__str__()

def __getattr__(self, item):#獲取非定義的屬性

if item == "score":

return lambda :99

elif item == "dog":

return "xiaohuang"

else:

raise "attr error"

def __call__(self, *args, **kwargs): #呼叫自己

print "__call__ self:"

for i in range(len(args)):#列印元組

print args[i]

for i in kwargs:#列印鍵值對

print i,kwargs[i]

print "call:my name is zhengjinwei"

student.showclassmethod() #呼叫類方法

s = student("zhengjinwei",24)

s.*** = "male" #呼叫***的包裝器,此處要注意的是student類必須繼承自object才能使用包裝器

s.grade = "1" #動態建立屬性

print s.grade

print s #呼叫 __str__

print s.score() #呼叫__getattr__

print s.dog #呼叫__getattr__

if callable(s):

s(1,2,3,name="zjw",age=24) #呼叫 __call__

s.showclassmethod()

s.showstaticmethod()

s.showall()

結果:

call classmethod

this is a student class

call setter

1student object:(name:zhengjinwei)(age:24)

99xiaohuang

__call__ self:12

3age 24

name zjw

call:my name is zhengjinwei

call classmethod

this is a student class

call static method

this is a student class

call instance method

24 zhengjinwei call property

male male

call destructor

關於python的一些特性

首先,我其實得說說為什麼我們會選擇python。在我加入企業快盤團隊之前,整個專案包括更早的金山快盤都是採用python進行開發的。至於為什麼這麼選擇,當時的架構師蔥頭告訴我,主要是因為python上手簡單,開發迅速。對於團隊裡面大部分完全沒服務端開發經驗的同學來說,python真的是乙個很好的選擇...

https的一些特性

支援客戶 伺服器模式 簡單快速 客戶向服務端請求服務時,只需傳送請求方式和路徑。靈活 允許傳輸任意型別的資料物件。由content type加以標記。無連線 每次響應乙個請求,響應完成以後就斷開連線。無狀態 伺服器不儲存瀏覽器的任何資訊。每次提交的請求之間沒有關聯。持續性瀏覽器和伺服器建立tcp連線...

CSS的一些特性

text indent 20px 2em 2rem em 相對于父元素字型大小的參考單位 text indent 999 隱藏文字的另外做法層疊性,繼承性 inherit 優先順序 繼承性 text line height,font color color 1.屌絲程式設計師表示法 red,blue...