Python 之 內建方法(Python14)

2021-10-20 18:13:53 字數 3873 閱讀 2721

# __rmul__()

# 左右兩個物件比較並做乘法

class

foo:

def__init__

(self, val)

: self.val = val

def__rmul__

(self, other)

:return foo(self.val * other.val)

def__str__

(self)

:return

'foo [% s]'

% self.val

class

bar:

def__init__

(self, val)

: self.val = val

def__rmul__

(self, other)

:return foo(self.val * other.val)

def__str__

(self)

:return

'bar [% s]'

% self.val

f = foo(3)

b = bar(4)

print

(f * b)

f = foo(3)

b = bar(

'abc'

)print

(f * b)

print

(b * f)

# __dict__() 例項屬性管理

class

anotherfun

:def

__init__

(self)

: self.name =

'yorlen'

print

(self.__dict__)

self.age =

24print

(self.__dict__)

self.male =

true

print

(self.__dict__)

af = anotherfun(

)# __setattr__()屬性註冊

# 重寫__setattr__方便使用者提供簡單資料

# 從而對物件的初始化做相對改變

# 如果重寫__setattr__()但什麼都不做

# 那麼物件將沒有任何屬性

class

fun:

def__init__

(self)

: self.name =

'yorlen'

self.age =

24 self.male =

true

def__setattr__

(self, key, value)

:print

('-----------------'

)print

('setting:{},with:{}'

.format

(key, value)

)print

('current __dict__:{}'

.format

(self.__dict__)

)# 屬性註冊

self.__dict__[key]

= value

print

('current __dict__:{}'

.format

(self.__dict__)

)def

print_hello

(self)

:print

('hello,{}'

.format

(self.name)

)fun = fun(

)print

(fun)

print

(fun.__dict__)

# __sizeof__()顯示物件的記憶體占用長度

# 類物件的記憶體占用長度為32

print

(fun.__sizeof__())

print

(af.__sizeof__())

print

(f.__sizeof__())

print

(b.__sizeof__())

# __slots__控制物件屬性

# 不能像任意類一樣可以隨意新增屬性

# 在__slots__內定義了哪些屬性

class

test_slots

: __slots__ =

'x',

'y'def

__init__

(self)

: self.x =

22# self.z = 44

defsay

(self)

:print

("i'm {}"

.format

(self.x)

)sl = test_slots(

)print

(sl.x)

try:

print

(sl.y)

except attributeerror as reason:

print

(reason)

sl.y =

33print

(sl.y)

sl.x =

44print

(sl.x)

# sl.z = 55 報錯

# print(sl.z)

class

test_n

:def

__init__

(self)

: self.x =

22 self.y =

33# self.z = 44

defsay

(self)

:print

("i'm {}"

.format

(self.x)

)sl1 = test_n(

)print

(sl1.x)

print

(sl1.y)

sl1.x =

44print

(sl1.x)

sl1.z =

55print

(sl1.z)

c:\users\administrator\pycharmprojects\pythonproject\venv\scripts\python.exe d:/codefile/pass/pythonneizhifunctonsix.py

foo [12]

foo [abcabcabc]

foo [abcabcabc]

-----------------

setting:name,with:yorlen

current __dict__:{}

current __dict__:

-----------------

setting:age,with:24

current __dict__:

current __dict__:

-----------------

setting:male,with:true

current __dict__:

current __dict__:

<__main__.fun object at 0x0000000001e2a400>

3232

323222y

3344

2233

4455

process finished with exit code 0

python之內置函式

非空即真,非0即真 記住這句話可以讓你少寫好多 l asdfgwert3r 1 sorted l 排序 字串可以直接使用sorted排序 2 all 如果list裡面都為真的情況返回為 true all 1 2,3 4 print true all 1 2,3 0 print false all 1...

python之內置函式

它將兩個 非複數 數字作為實參,並在執行整數除法時返回一對商和餘數。對於混合運算元型別,適用雙目算術運算子的規則。對於整數,結果和 a b,a b 一致,分別對應取整數和取餘數 對於浮點數,結果是 q,a b q 通常是 math.floor a b 但可能會比 1 小。如 將153拆解 a,b d...

python 之內置模組

裝飾器 def trace func result func args,kwargs print s r,r r func.name args,kwargs,result return result trace 裝飾器含義就是 fibonacci trace fibonacci def fibona...