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 |