variable scope គឺជាទីកន្លែងផ្សេងៗនៅក្នុងសតិរបស់ឧបករណ៍អេឡិចត្រូនិក ដែលមានព្រុំដែនច្បាស់លាស់។
ដែលហៅថា global scope គឺជាទីកន្លែងនៅខាងក្រៅ function ។ ដើម្បីបង្កើតវត្ថុផ្សេងៗនៅក្នុង global scope យើងត្រូវធ្វើដូចខាងក្រោមនេះ៖
money_list = [2000, 1500] def get_profit(sale=0, buy=0): profit = sale - buy print('The profit is', profit) get_profit(*money_list)
ដែលហៅថា local scope គឺជាទីន្លែងនៅក្នុង function ។ ដើម្បីបង្កើតវត្ថុផ្សេងៗនៅក្នុង local scope យើងត្រូវធ្វើដូចខាងក្រោមនេះ៖
def get_profit(sale=0, buy=0): money_list = [2000, 1500] sale = money_list[0] buy = money_list[1] profit = sale - buy print('The profit is', profit) get_profit()
គ្រប់វត្ថុនៅក្នុង local scope ត្រូវបង្កើតឡើងនៅពេល function ត្រូវបាន call និងត្រូវលុបចោលទៅវិញ នៅពេលដែល function ត្រូវបានប្រើរួចហើយ។ ជាក់ស្តែង នៅក្នុងកម្មវិធីខាងលើនេះ variable ឈ្មោះ money_list ត្រូវបានបង្កើតឡើងនៅក្នុង local scope នៅពេលដែល function ឈ្មោះ get_profit() ត្រូវបាន call តែ variable នេះត្រូវបានលុបចោលទៅវិញ នៅពេលដែល block នៃ statement នៅក្នុង function នោះត្រូវបានអនុវត្តចប់សព្វគ្រប់។
មួយវិញទៀត នៅពេលដែល function មួយត្រូវបានបង្កើតឡើងនៅក្នុង function មួយទៀត ទីកន្លែងរបស់ function នៅខាងក្នុងត្រូវហៅថា nested scope និងទីកន្លែងរបស់ function នៅខាងក្រៅត្រូវហៅថា enclosing scope ។ ដើម្បីបង្កើតវត្ថុផ្សេងៗនៅក្នុង nested scope និង enclosing scope យើងត្រូវធ្វើដូចខាងក្រោមនេះ៖
def enclose_func(): money_list = [2000, 1500] print('The object created in enclosing scope is', money_list) def nested_func(): money_dict = {'sale': 2000, 'buy': 1500} print('The object created in nested scope is', money_dict) nested_func() enclose_func()
វត្ថុនៅក្នុង nested scope និងវត្ថុនៅក្នុង enclosing scope ក៏ដូចជាវត្ថុនៅក្នុង local scope ដែរ គឺវាត្រូវបានបង្កើតឡើងនៅពេលដែល function ត្រូវបាន call និងត្រូវលុបចោលវិញ នៅពេលដែល function ត្រូវបាន call រួចហើយ។ ក៏ប៉ុន្តែ បើសិនជាវត្ថុទាំងនោះត្រូវបានបញ្ជូនចេញទៅកាន់ global scope វត្ថុទាំងនោះនឹងមិនត្រូវបានលុបចោលឡើយ នៅពេលដែល function ត្រូវបាន call រួចហើយនោះ។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
def get_profit(): sale = 1000 buy = 900 profit = sale - buy return profit money = get_profit() print(money)
មួយវិញទៀត ដែលហៅថា built-in scope គឹជាទីកន្លែងមួយនៅក្នុងសតិរបស់កំព្យូទ័រ ដែលវត្ថុមួយចំនួនត្រូវបានបង្កើតឡើងរួចជាស្រេចទុកនៅក្នុងនោះ។ វត្ថុទាំងនោះត្រូវហៅថា built-in object ដែលអាចត្រូវយកទៅប្រើនៅក្នុង scope ណាក៏បានដែរ។

នៅក្នុងភាសា Python នៅពេលដែលយើងយកវត្ថុណាមួយមកប្រើ ការស្វែងរកវត្ថុនោះ ត្រូវធ្វើឡើងតាមគំនូសបំព្រួញដូចខាងក្រោមនេះ៖

បានន័យថា ការស្វែងរកវត្ថុត្រូវធ្វើឡើង ដោយចាប់ផ្តើមនៅក្នុង scope ដែលទីនោះវត្ថុត្រូវបានយកទៅប្រើ រួចបានបន្តទៅ scope ផ្សេងៗទៀត តាមសញ្ញាព្រួញដូចនៅក្នុងរូបខាងលើនេះ រហូតដល់អស់ scope បើនៅតែរកមិនឃើញ។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
global_str = 'global scope.' def enclosing_func(): enclosing_str = 'local scope or enclosing scope.' def nested_func(): nested_str = 'nested scope.' print('global_str is found in', global_str) print('enclosing_str is found in', enclosing_str) print('nested_str is found in', nested_str) nested_func() enclosing_func()
ដោយការស្វែងរកវត្ថុផ្សេងៗត្រូវប្រព្រឹត្តទៅដូចនៅក្នុងគំនូសបំព្រួញខាងលើ ដូចនេះយើងមិនអាចយកវត្ថុនៅក្នុង scope ផ្នែកខាងក្រោម ទៅប្រើនៅក្នុង scope នៅខាងលើបានឡើយ។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
global_str = 'global scope.' def enclosing_func(): enclosing_str = 'local scope or enclosing scope.' def nested_func(): nested_str = 'nested scope.' nested_func() enclosing_func() print(enclosing_str)
វត្ថុនៅក្នុង scope ខុសៗគ្នា គឺជាវត្ថុខុសៗគ្នា ទោះបីជាវត្ថុទាំងនោះមានឈ្មោះដូចគ្នាក៏ដោយ។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
object = 1000 print('In global scope, object is', object) def enclosing_func(): object = True print('In enclosing scope, object is', object) def nested_func(): object = 'sentence' print('In nested scope, object is', object) nested_func() enclosing_func()
យើងអាចយកវត្ថុនៅក្នុង global scope មកប្រើក្នុង local scope និងឬ nested scope បានមែន តែយើងមិនអាចយកឈ្មោះរបស់វត្ថុទាំងនោះទៅភ្ជាប់នឹងវត្ថុណាផ្សេងទៀតបានឡើយ។ ការប៉ុនប៉ងយកឈ្មោះរបស់វត្ថុទាំងនោះទៅភ្ជាប់នឹងវត្ថុផ្សេងទៀត គឺជាការបង្កើតវត្ថុថ្មីមានឈ្មោះដូចគ្នា នៅក្នុងបណ្តា scope ទាំងនោះ។ ក៏ប៉ុន្តែ បើយើងពិតជាចង់យកឈ្មោះរបស់វត្ថុនៅក្នុង global scope ទៅភ្ជាប់នឹងវត្ថុផ្សេងទៀត នៅក្នុង local scope និងឬ nested scope យើងត្រូវប្រើប្រាស់ statement global ដោយធ្វើដូចខាងក្រោមនេះ៖
object = 1000 object_list = [210, False, 'profit'] def enclosing_func(): global object object = True def nested_func(): global object_list object_list = 'Sentence' nested_func() enclosing_func() print('The new "object" is', object) print('The new "object_list" is', object_list)
ចំពោះវត្ថុដែលមានលក្ខណៈ mutable និងដែលស្ថិតនៅក្នុង global scope យើងអាចយកវាមកដោះដូរ element នៅក្នុង local scope និងឬ nested scope បានជាធម្មតា ដោយមិនចាំបាច់ប្រើប្រាស់ statement global ឡើយ។ ដោយហេតុថា ការដោះដូរ element នៅក្នុងវត្ថុដែលជា container មិនមែនជាការយកឈ្មោះរបស់វត្ថុដែលជា container ទៅភ្ជាប់នឹងវត្ថុផ្សេងទៀតឡើយ។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
object_list = [210, False, 'profit'] def enclosing_func(): object_list[0] = True def nested_func(): object_list[1] = 'Sentence' nested_func() enclosing_func() print('The new "object_list" is', object_list)
យ៉ាងណាម៉ិញ យើងអាចយកវត្ថុនៅក្នុង local/enclosing scope មកប្រើនៅក្នុង nested scope បានមែន តែយើងមិនអាចយកឈ្មោះរបស់វត្ថុទាំងនោះទៅភ្ជាប់នឹងវត្ថុផ្សេងទៀតបានឡើយ។ ការប៉ុនប៉ងយកឈ្មោះរបស់វត្ថុទាំងនោះ ទៅភ្ជាប់នឹងវត្ថុផ្សេង គឺជាការបង្កើតវត្ថុមានឈ្មោះដូចគ្នា នៅក្នុង scope ចុងក្រោយនេះ។ ក៏ប៉ុន្តែ បើយើងពិតជាចង់យកឈ្មោះរបស់វត្ថុនៅក្នុង local/enclosing scope ទៅភ្ជាប់នឹងវត្ថុនៅក្នុង nested scope មែន យើងចាំបាច់ត្រូវប្រើប្រាស់ statement nonlocal ដោយធ្វើដូចខាងក្រោមនេះ៖
def enclosing_func(): object = 1000 def nested_func(): nonlocal object object = 'Sentence' nested_func() print('The new "object" is', object) enclosing_func()
យ៉ាងណាម៉ិញ ចំពោះវត្ថុដែលស្ថិតនៅក្នុង local scope ដែលជា function ខុសៗគ្នា គឺជាវត្ថុខុសៗគ្នា ទោះបីជាវត្ថុទាំងនោះមានឈ្មោះដូចគ្នាក៏ដោយ។ ដោយហេតុថា function និមួយៗគឺជា scope ដោយឡែកពីគ្នា។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
def get_profit(sale, buy): profit = sale - buy return profit def sum_profit(sale, buy): money = sale + buy + get_profit(sale=sale, buy=buy) print(money) sum_profit(1000, 900)
សរុបមក ឈ្មោះរបស់វត្ថុនៅក្នុងភាសា Python ក៏ដូចជាឈ្មោះរបស់មនុស្សយើងដែរ។ ពោលគឺមនុស្សឈ្មោះ វុធ នៅក្នុងគ្រួសារមួយ ខុសពីមនុស្សឈ្មោះ វុធ នៅក្នុងគ្រួសារមួយទៀត ទោះបីជាមនុស្សទាំងពីរនាក់នោះមានឈ្មោះដូចគ្នាក៏ដោយ៕