python高階之函式和類內建魔法屬性

2022-07-04 22:54:11 字數 3146 閱讀 5164

關於物件的魔法方法我們已經講得太多,但是對於類或函式內建的魔法屬性和功能我們涉及較少,下面系統了解一下類和函式的內建屬性。

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__
前面我們基本介紹過來類的內建屬性,現在重點針對函式的內建屬性。

內建屬性是不能在函式的作用域內直接使用的,因為它們沒有被顯性地定義,它們需要通過函式變數來呼叫。

記錄函式的引數和返回值的型別,前提是定義時指定了型別,否則為{}.

def get(x:int, y:int) -> int:

return x + y

print(get.__annotations__) #

python萬物皆物件,所以函式也是乙個物件,可以看做簡化版的類的例項,它的類物件就是functiontype。

def get(x:int, y:int) -> int:

return x + y

print(get.__class__) #

__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__) #

# 函式*後面的引數表示強制關鍵字引數,即一定要:

get(3,4,age=30)這種方式呼叫

__name__和__qualname__分別表示函式的名字和合法名,__name__僅僅是函式名字,而__qualname__會用點示法顯示函式所在的類和模組。

def name(x=2):

c = 0

def name1(y):

return x + y + c

return name1

n = name()

print(n.__closure__)

# 結果

(, )

# 這裡的自由變數指的是y和c

既然函式也是物件,那麼它也應該有自己的屬性,預設為{}.

def get(x:int=1, y:int=3) -> int:

return x + y

get.name = 'get'

print(get.__dict__) #

以上是普通的自定義函式的內建屬性,它和生成器函式、協同函式的概念是不一樣的;

對於普通的業務開發來說,需要用到的內建屬性一般為:__defaults__,__name__,__doc__.

Python之常用內建高階函式

map函式 用於接收乙個函式及多個迭代物件,會根據提供的函式對指定序列做對映,然後返回乙個新的map物件 例1 需要乙個引數 a map lambda x x x,1,2 3 print a 輸出結果 map object at 0x00fa73d0 此時a指向於map出的新物件,可以使用list ...

python 內建高階函式

1.map abs i for i in 1,3,4,5 1 對於序列每個元素求絕對值 import random from functools import reduce print list map abs,1,3,4,5 2 對於序列每個元素求階乘 5 import random deffac...

python 內建高階函式

1.map map 函式接收兩個引數,乙個是函式,乙個是序列 map將傳入的函式依次作用到序列的每個元素,並且把結果 作為新的序列返回 求絕對值 print map abs,1,3,4,5 輸出 map列印的不是乙個列表而是乙個物件,所以我們需要轉換為列表才能列印。print list map ab...