Python scope(পাইথন স্কোপ)
আমরা আজকে পাইথনে স্কোপ(scope) নিয়ে আলোচনা করব। স্কোপ হল আমরা প্রোগ্রামে কোন একটি ভ্যারিয়েবল কে কোথায় পরিচিত করাচ্ছি এবং প্রোগ্রামের কোন কোন জায়গা থেকে আমরা তাকে পেতে পারি তা নির্ধারণ করে দেয়া।
স্কোপ মূলত দুই ধরনের-
- গ্লোবাল স্কোপ
- লোকাল স্কোপ
গ্লোবাল স্কোপ
নামটি থেকেই আমরা ধারনা পাচ্ছি যে, আমরা এই স্কোপ এ কোন ভ্যারিয়েবল কে নির্ধারণ করলে তা পুরো প্রোগ্রাম ব্যাপী যে কোন জায়গায় পাওয়া যাবে।
সাধারণত গ্লোবাল ভ্যারিয়েবল কোন ফাংশন এর অধিনস্ত থাকে না। তাকে প্রোগ্রামের প্রত্যেকটি স্কোপ থেকে পাওয়া যায়।
global_variable_1 = "I am global." # global variable available in global print("1. ",global_variable_1) class demo_class: # global variable avaiable inside class print("2. ",global_variable_1) def demo_func_inside_class(self): # global variable available inside a function of a class print("3. ",global_variable_1) def demo_func(): # global variable available inside a function print("4. ",global_variable_1) dc_obj = demo_class() dc_obj.demo_func_inside_class() demo_func() # Output: # 1. I am global. # 2. I am global. # 3. I am global. # 4. I am global.
লোকাল স্কোপ
সাধারণত কোন ফাংশনের মধ্যে সীমাবদ্ধ ভ্যারিয়েবলকে যা উক্ত ফাংশন ব্যাতিত পাওয়া যায় না ভ্যারিয়েবল এর এমন অবস্থান কে লোকাল স্কোপ বলা হয়।
def demo_func(): local_variable_1 = "I am local." print(local_variable_1) demo_func() # Output:# I am local.
লোকাল স্কোপ এ নির্ধারিত ভ্যারিয়েবল কে লোকাল স্কোপ ব্যাতিত পাওয়া যায় না।
def demo_func(): local_variable_1 = "I am local." demo_func() print(local_variable_1) # Output: # NameError: name 'local_variable_1’ is not define
বিশেষ
গ্লোবাল ভ্যারিয়েবল কে চাইলে ফাংশনের মধ্যে নির্ধারণ করা যায় global কীওয়ার্ড টি ব্যবহার করে।
def demo_func(): global defined_global_variable_inside_func defined_global_variable_inside_func = "I am global." demo_func() print(defined_global_variable_inside_func) # Output: # I am global.
আমাদের একটি গ্লোবাল এবং লোকাল ভ্যারিয়েবল এর নাম যদি একই হয় তাহলে যে লোকাল স্কোপ এ আমরা লোকাল ভ্যারিয়েবল টি নির্ধারণ করেছি তাতে আমরা লোকাল ভ্যারিয়েবল টির মান পাব, অন্য সব জায়গায় গ্লোবাল ভ্যারিয়েবল টির মান পাব।
variable_1 = "I am global" def demo_func(): variable_1 = "I am local" print("1. ", variable_1) demo_func() print("2. ",variable_1) # Output: # 1. I am local # 2. I am global
আমরা যদি লোকাল স্কোপ এ একই নামের ভ্যারিয়েবল এ গ্লোবাল ভ্যারিয়েবল কে পুনঃনির্ধারন করতে চাই তাহলে আমাদের কে global কীওয়ার্ড টি ব্যবহার করতে হবে।
global_variable = "I am global" def demo_func(): global global_variable print("1. ",global_variable) global_variable = "I am global but changed my value." print("2. ", global_variable) demo_func() print("3. ",global_variable) # Output: # 1. I am global # 2. I am global but changed my value.# 3. I am global but changed my value.
- একটি ফাংশনের মধ্যে কোন একটি ভ্যারিয়েবল ইনিশিয়ালাই করে সেটি যদি ওই ফাংশনের মধ্যে থাকা অন্য একটি ফাংশনে ব্যবহার করতে হয় তাহলে nonlocal কী ওয়ার্ডটি ব্যবহার করতে হবে ।
চলুন একটি উদাহরণ দেখে আসি।
def outer(): x=0 def inner(): nonlocal x x+=1 print(x) inner() outer() #output # 1
এক্ষেত্রে আমরা যদি নন-লোকাল কি ওয়ার্ড টি ব্যবহার না করি তাহলে ইরর আসবে ।
স্কোপ নিয়ে আলোচনা এখানেই শেষ করছি। স্কোপ এর কনসেপ্ট খুব সহজ সরল মনে হলেও মাঝে মাঝে তা অনেক ট্রিকি হতে পারে।
“Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it’s worth it in the end because once you get there, you can move mountains.”
-Steve Jobs
উদাহরন
# This function modifies global variable 's’ def f(): global s print(s) s = "Look for VIVASOFT Python Section" print(s) # Global Scope s = "Python is great !" f() print(s) #Python is great! #Look for VIVASOFT Python Section #Look for VIVASOFT Python Section # Python program to demonstrate # scope of variable a = 1 # Uses global because there is no local 'a’ def f(): print('Inside f() : ', a) # Variable 'a’ is redefined as a local def g(): a = 2 print('Inside g() : ', a) # Uses global keyword to modify global 'a’ def h(): global a a = 3 print('Inside h() : ', a) # Global scope print('global : ', a) f() print('global : ', a) g() print('global : ', a) h() print('global : ', a) #Output global :1 Inside f():1 global :1 Inside g():2 global :1 Inside h():3 global :3 defouter(): x = "local" def inner(): nonlocal x x = "nonlocal" print("inner:", x) inner() print("outer:", x) outer() #output inner: nonlocal outer: nonlocal x = 5 def foo(): x = 10 print("local x:", x) foo() print("global x:", x) #Output local x: 10 global x: 5
এসো নিজে করি
অনুশীলনঃ
১। company নামে একটি ভ্যারিবেল তৈরি করুন এবং এটিতে “vivaSoft” ভ্যালু এসাইন করুন।
২। A=10 দিয়ে একটি গ্লোবাল ভ্যারিয়েবল তৈরি করুন এবং B=20 লোকাল ভ্যরিয়েবল এর সাথে যোগ করুন।
৩। নিচের প্রোগ্রামটির আউটপুট কি হবে?
def myfunc(): global x x = 300 myfunc() print(x)
৪। নিচের প্রোগ্রামটির আউটপুট কি হবে?
x = 300 def myfunc(): global x x = 200 myfunc() print(x)
৫। নিচের প্রোগ্রামটির আউটপুট কি হবে?
def func_outer(): x = "local" def func_inner(): nonlocal x x = "nonlocal" print("inner:", x) func_inner() print("outer:", x) func_outer()
ইন্টারভিউ প্রশ্নোত্তর
- ভ্যারিয়েবল কি ?
- একটি ভ্যারিয়েবল এর মধ্যে প্রথমে “১০” রাখলে পরবর্তীতে এর মধ্যে সংখ্যা রাখা যাবে ?
- লোকাল ভ্যারিয়েবল এবং গ্লোবাল ভ্যারিয়েবল এর মধ্যে পার্র্থক্য কি কি ?
- পাইথনে স্কোপ সম্পর্কে ব্যাখ্যা করুন।
- পাইথনে namespaces কি ? এর ব্যবহার গুলো ব্যাখ্যা করুন।