Static Method គឺជា method ទាំងឡាយណាដែលមិនត្រូវការ instance ជា argument សំរាប់ parameter self ហើយក៏គ្មាន parameter self នៅក្នុងនោះដែរ។ ដើម្បីបង្កើត static method យើងត្រូវធ្វើដូចខាងក្រោមនេះ៖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Cash(): def __init__( self ): print ( 'The constructor has been called.' ) def display(): print ( 'Show info.' ) display = staticmethod (display) money = Cash() Cash.display() money.display() |
1 2 3 | The constructor has been called. Show info. Show info. |
ជារួម static method មានសណ្ឋានដូចជា function ធម្មតានៅខាងក្រៅថ្នាក់ដែរ ទោះជាវាត្រូវបានបង្តើតឡើងនៅក្នុងថ្នាក់យ៉ាងណាក៏ដោយ។ ម៉្យាងទៀត ដោយសារតែ static method ត្រូវបានបង្កើតឡើងនៅក្នុងថ្នាក់ ដូចនេះគ្រប់ការយក static method មកប្រើ ត្រូវតែធ្វើឡើងតាមរយៈថ្នាក់របស់វាឬ instance នៃថ្នាក់របស់វា៕