Decorator

Decorator គឺ​ជា​វត្ថុ​ទាំងឡាយ​ណា ដែល​មាន​សមត្ថភាព​អាច​យក mothod ឬ function ណាមួយ មក​កែ​លំអ​អោយ​ទៅ​ជា​វត្ថុ​ផ្សេង​ទៀត​។ ពនិត្យ​កម្មវិធី​ខាង​ក្រោម​នេះ៖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Cash():
    def toStaticMethod(method):
        method = staticmethod(method)
        return method
 
    def toClassMethod(method):
        method = classmethod(method)
        return method
 
    @toClassMethod
    def setClassAttribute(cl, data):
        cl.data = data
 
    @toStaticMethod
    def display():
        print(Cash.data)
 
 
money = Cash()
money.setClassAttribute(1000)
Cash.display()
1
1000