នៅក្នុងភាសា Python ដែលហៅថា constructor គឺជា method ពិសេសម៉្យាង ដែលមានឈ្មោះជា __init__ (សញ្ញា _ ជាប់គ្នាពីរនៅពីមុខនិងនៅពីក្រោយ init) ។ បើសិនជា method ឈ្មោះ __init__ នេះត្រូវបានបង្កើតឡើងនៅក្នុងថ្នាក់ណាមួយ វានឹងត្រូវ call ជាស្វ័យប្រវត្តិ រាល់លើកដែលបណ្តា instance ទាំងឡាយត្រូវបានបង្កើតឡើងចេញពីថ្នាក់នោះ។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
1 2 3 4 5 6 7 8 9 10 11 | class Area(): pi = 3.14 def __init__( self ): print ( 'The constructor was called.' ) def rectangle( self , width, height): area = width * height print ( 'The area of the rectangle is' , area) instance = Area() |
1 | The constructor was called. |
ដូចនេះ យើងឃើញថា ការយកថ្នាក់មកប្រើដើម្បីបង្កើត instance គឺជាការតំរូវអោយបង្កើត instance ផងនិង call constructor ផងបើសិនជាមាន។ ហើយយើងបានដឹងពីសារសំខាន់របស់ constructor ដែលជា mothod ឈ្មោះ __init__ នៅពេលខាងមុខនេះ៕