nonlocal keyword ব্যবহার করে Python nonlocal Statement তৈরি করা হয় এটি নিকটতম scope এর একটি variable উল্লেখ করতে ব্যবহৃত হয়।
nonlocal keyword টি local বা global ভেরিয়েবলে কাজ করবে না এবং তাই global এবং local একটি ব্যতীত অন্য আরেকটি scope ভেরিয়েবলের উল্লেখ করতে ব্যবহার করা আবশ্যক। nonlocal keyword টি নেস্টেড ফাংশনে ব্যবহৃত হয় প্যারেন্ট ফাংশনে একটি ভেরিয়েবল রেফারেন্স করতে।
১.Python nonlocal Keyword উদাহরণ:
এই উদাহরণে, আমরা nonlocal keyword এর একটি কাজ দেখাব।
def foo():
name = "w3" # Our local variable
def bar():
nonlocal name # Reference name in the upper scope
name = 'w3programmers' # Overwrite this variable
print(name)
# Calling inner function
bar()
# Printing local variable
print(name)
foo()
উদাহরণ ৩: এই উদাহরণে, আমরা দেখতে পাব যে একই নামের variable সহ একাধিক nested function থাকলে কোন variable কে Python nonlocal Statement বোঝায়।
def foo():
# Local variable of foo()
name = "w3p"
# First inner function
def bar():
name = "W3p"
# Second inner function
def ack():
nonlocal name # Reference to the next upper variable with this name
print(name) # Print the value of the referenced variable
name = 'W3P' # Overwrite the referenced variable
print(name)
ack() # Calling second inner function
bar() # Calling first inner function
print(name) # Printing local variable of bar()
foo()
উদাহরণ ১৪.৪: এই উদাহরণে, আমরা একটি reusable counter তৈরি করব (শুধু প্রদর্শনের উদ্দেশ্যে)
# Our counter function
def counter():
c = 0 # Local counter variable
# This function manipulate the
# local c variable, when called
def count():
nonlocal c
c += 1
return c
# Return the count() function to manipulate
# the local c variable on every call
return count
# Assign the result of counter() to
# a variable which we use to count up
my_counter = counter()
for i in range(3):
print(my_counter())
print('End of my_counter')
# Create a new counter
new_counter = counter()
for i in range(3):
print(new_counter())
print('End of new_counter')
আমি মাসুদ আলম, বাংলাদেশের ৩৬ তম Zend Certified Engineer । ২০০৯ সালে কম্পিউটার সাইন্স থেকে বেচেলর ডিগ্রী অর্জন করি। দীর্ঘ ১৫ বছর আমি Winux Soft, SSL Wireless, IBCS-PRIMAX, Max Group, Canadian International Development Agency (CIDA), Care Bangladesh, World Vision, Hellen Keller, Amarbebsha Ltd সহ বিভিন্ন দেশি বিদেশী কোম্পানিতে ডেটা সাইন্স, মেশিন লার্নিং, বিগ ডেটা, ওয়েব ডেভেলপমেন্ট এবং সফটওয়্যার ডেভেলপমেন্ট এর উপর বিভিন্ন লিডিং পজিশন এ চাকরি এবং প্রজেক্ট লিড করি। এছাড়াও বাংলাদেশের ১৮৫ জন জেন্ড সার্টিফাইড ইঞ্জিনিয়ার এর মধ্যে ১২০ এরও অধিক ছাত্র আমার হাতে জেন্ড সার্টিফাইড ইঞ্জিনিয়ার হয়েছেন। বর্তমানে w3programmers ট্রেনিং ইনস্টিটিউট এ PHP এর উপর Professional এবং Advance Zend Certified PHP -8.2 Engineering, Laravel Mastering Course with ReactJS, Python Beginning To Advance with Blockchain, Machine Learning and Data Science, Professional WordPress Plugin Development Beginning to Advance কোর্স করাই। আর অবসর সময়ে w3programmers.com এ ওয়েব টেকনোলজি নিয়ে লেখালেখি করি।