python進階之函數和類內建魔法屬性
前言
關於對象的魔法方法我們已經講得太多,但是對於類或函數內建的魔法屬性和功能我們涉及較少,下面系統了解一下類和函數的內建屬性。
查看內建屬性
class Person(object): pass def get_name(): pass if __name__ == "__main__": person = Person() print(dir(get_name)) print(dir(Person)) print(dir(person)) # 結果 [‘__annotations__‘, ‘__call__‘, ‘__class__‘, ‘__closure__‘, ‘__code__‘, ‘__defaults__‘, ‘__delattr__‘, ‘__dict__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__get__‘, ‘__getattribute__‘, ‘__globals__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__kwdefaults__‘, ‘__le__‘, ‘__lt__‘, ‘__module__‘, ‘__name__‘, ‘__ne__‘, ‘__new__‘, ‘__qualname__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘] [‘__class__‘, ‘__delattr__‘, ‘__dict__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__le__‘, ‘__lt__‘, ‘__module__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘__weakref__‘] [‘__class__‘, ‘__delattr__‘, ‘__dict__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__le__‘, ‘__lt__‘, ‘__module__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘__weakref__‘]
比較一下發現類和它的實例內建的屬性和方法是一樣的,函數似乎比類的內建的屬性和方法更多,分離出屬性:
- 類的內建屬性有:
‘__class__‘, ‘__dict__‘, ‘__doc__‘, ‘__module__‘, ‘__weakref__‘
- 函數的內建屬性有:
‘__annotations__‘, ‘__class__‘, ‘__closure__‘, ‘__code__‘, ‘__defaults__‘, ‘__dict__‘,‘__doc__‘, ‘__kwdefaults__‘, ‘__module__‘, ‘__name__‘,‘__qualname__‘,__globals__
前面我們基本介紹過來類的內建屬性,現在重點針對函數的內建屬性。
函數的內建屬性
內建屬性是不能在函數的作用域內直接使用的,因為它們沒有被顯性地定義,它們需要通過函數變量來調用。
- __annotations__
記錄函數的參數和返回值的類型,前提是定義時指定了類型,否則為{}.
def get(x:int, y:int) -> int: return x + y print(get.__annotations__) # {‘x‘: <class ‘int‘>, ‘y‘: <class ‘int‘>, ‘return‘: <class ‘int‘>}
- __class__
python萬物皆對象,所以函數也是一個對象,可以看做簡化版的類的實例,它的類對象就是FunctionType。
def get(x:int, y:int) -> int:
return x + y
print(get.__class__) # <class ‘function‘>
- __defaults__和__kwdefaults__
__defaults__屬性用來存儲參數的默認值,是一個元組,如果沒有默認值為None。
def get(x:int=1, y:int=3) -> int:
return x + y
print(get.__defaults__) # (1, 3)
__kwdefaults__記錄強制關鍵字參數的默認值,字典形式,沒有置為None。
def get(x:int, y:int, *, age=20):
pass
print(get.__kwdefaults__) # {‘age‘:20}
# 函數*後面的參數表示強制關鍵字參數,即一定要:
get(3,4,age=30)這種方式調用
- __name__和__qualname__
__name__和__qualname__分別表示函數的名字和合法名,__name__僅僅是函數名字,而__qualname__會用點示法顯示函數所在的類和模塊。
__doc__和__module__:和類的該屬性一樣,__doc__記錄該函數的說明,__module__表示函數定義的模塊的名字。
__globals__:函數定義所在模塊的全局命名空間的字典的引用。
__closure__:以包含cell的元組形式返回閉包所包含的自由變量。
def name(x=2):
c = 0
def name1(y):
return x + y + c
return name1
n = name()
print(n.__closure__)
# 結果
(<cell at 0x000001B1B201EEB8: int object at 0x00000000619760C0>,
<cell at 0x000001B1BAEFB768: int object at 0x0000000061976100>)
# 這裏的自由變量指的是y和c
- __dict__
既然函數也是對象,那麽它也應該有自己的屬性,默認為{}.
def get(x:int=1, y:int=3) -> int:
return x + y
get.name = ‘get‘
print(get.__dict__) # {‘name‘:‘get‘}
- __code__:返回已編譯的函數對象,CodeType對象。
總結
以上是普通的自定義函數的內建屬性,它和生成器函數、協同函數的概念是不一樣的;
對於普通的業務開發來說,需要用到的內建屬性一般為:__defaults__,__name__,__doc__.
參考
https://www.jianshu.com/p/cfda0b76501c
《python參考手冊(第四版)》
python進階之函數和類內建魔法屬性