xia的小窩

一起來coding和碼字吧

0%

pyclass-修飾器

使用@property將method變成物件變數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class train:
def __init__(self) -> None:
self._tempArem = 0

@property
def temp(self):
return self._tempArem + 30

test = train()
print(test._tempArem)
print(test.temp)

# 0
# 30

如何自製裝飾器

1
2
3
4
5
6
7
8
9
10
11
12
13
def test(func):
x = 50
print(x)
return func

@test
def foo():
print(100)

foo()

# 50
# 100

上述為最簡易的裝飾器