পাইথনে ফাংশন আর্গুমেন্ট *args ও **kwargs
আমরা মাঝে মাঝেই পাইথনের ফাংশনের প্যারামিটার হিসাবে *args, ** kwargs কে দেখতে পাই । তো এগুলো আসলে কি ? এবং এগুলো কিভাবে কাজ করে ? এই দুইটা বুঝতে হলে প্রথমে আমাদের আর্গুমেন্ট কি সেটা বুঝতে হবে ।আর্গুমেন্ট
কোন ফাংশন কল করার সময় আমার ফাংশনে যে ভেলু pass করি করি সেটা কে বলা হয় আর্গুমেন্ট । পাইথনে দুই ধরনের আর্গুমেন্ট আছে,- Keyword Argument.
- Positional Argument.
Keyword Argument
যে সকল আর্গুমেন্টের সাথে তার আইডেন্টিফায়ার থাকে সেসব আর্গুমেন্ট কে keyword argument বলে । উদাহরণ দিলে পরিষ্কার হয়ে যাবে। মনে করেন আমাদের এইরকম একটা ফাংশন আছে,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_info(name = None, age = '21'): | |
return name, age; | |
name, age = get_info(name = "Arif", age = 24) | |
print("Name:", name) | |
print("Age: ", age) | |
Output: | |
Name: Arif | |
Age: 24 | |
উপরে আমরা ৪ নং লাইনে ফাংশন কল করার সময় যে দুইটা আর্গুমেন্ট pass করলাম সেটাকে বলা হয় keyword argument. এখানে name = 'Arif' এ name এবং age = 24 এর age আর্গুমেন্ট দুইটির আইডেন্টিফায়ার । কারণ name, age দিয়ে আমরা আলাদা ভাবে দুইটা আর্গুমেন্টকে আইডেন্টিফাই করতে পারছি ।
Positional Argument
যে আর্গুমেন্ট গুলো keyword argument না সেগুলো কে positional argument বলে । এই ধরনের আর্গুমেন্টের কোন আইডেন্টিফায়ার থাকে না । উদাহরণ,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_info(name, age): | |
return name, age; | |
name, age = get_info("Arif", 24) | |
print("Name:", name) | |
print("Age: ", age) | |
Output: | |
Name: Arif | |
Age: 24 | |
লক্ষণ করেন, এখানে আর্গুমেন্ট কে আইডেন্টিফাই করার জন্য কোন নাম সেট করা হয় নি । শুধু আর্গুমেন্টের ভেলু পাঠানো হয়েছে । এই ধরনের আর্গুমেন্টকে positional argument বলা হয় ।
মনে করেন আপনার এমন একটা ফাংশন দরকার সেটা তে আপনি যেকোনো সংখ্যক আর্গুমেন্ট pass করতে পারেন অর্থাৎ A function that acceptes variable lenght argument । এই ধরনের ফাংশন লিখতে গেলে আপনাকে *arags, **kwargs ব্যবহার করতে হবে ।
*args এর ব্যবহার
আপনার ফাংশনের আর্গুমেন্ট গুলো যদি non keyword আর্গুমেন্ট অর্থাৎ positional argument হয় সেক্ষেত্রে আপনি *args ব্যবহার করবেন । নিচে একটা উদাহরণ দেখেন তাহলে বিষয়টা পরিষ্কার হয়ে যাবে ।
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def variable_lenghts_argument_function(normal_argument, *argv): | |
print("Normal argument is: ", normal_argument) | |
for argument in argv: | |
print("Argument pass by *argv is: ", argument) | |
variable_lenghts_argument_function("Language", "Python", "Java", "C/C++") | |
Output: | |
Normal argument is: Language | |
Argument pass by *argv is: Python | |
Argument pass by *argv is: Java | |
Argument pass by *argv is: C/C++ |
উপরে আমরা নর্মাল আরগুমেত বাদে ৩ টা আর্গুমেন্ট পাঠিয়েছি । আমরা *args দিয়ে N সংখ্যক আর্গুমেন্ট পাঠাতে পারব ।
**kwargs এর ব্যবহার
আমাদের আর্গুমেন্ট গুলো positional argument না হয়ে যদি keyword argument হয় তাহলে আমাদের কে **kwargs ব্যবহার করতে হবে । নিচে একটা উদারহরন দেখি,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_kwargs_argument(**kwargs): | |
for key, value in kwargs.items(): | |
print("Argument pass by **kwargs is: %s = %s" % (key, value)) | |
test_kwargs_argument(name = "Arif", age = 21, department = "ETE") # call the function | |
Output: | |
Argument pass by **kwargs is: age = 21 | |
Argument pass by **kwargs is: department = ETE | |
Argument pass by **kwargs is: name = Ari |
অর্থাৎ আমারা বলতে পারি **kwargs দিয়ে আমরা আর্গুমেন্টের ডিকশনারি কে কোন ফাংশনে পাস করতে পারি । *args ও **kwargs আরও বিভিন্ন ভাবে ব্যবহার করা যায় নিচে তার কিছু উদাহরণ দেয়া হল,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# using *args | |
args = ("Hello", 1, 2, 3) # set the argument list | |
variable_lenghts_argument_function(*args) # call the function with the argument | |
Output: | |
Normal argument is: Hello | |
Argument pass by *argv is: 1 | |
Argument pass by *argv is: 2 | |
Argument pass by *argv is: 3 | |
# using **kwargs | |
kwargs = {"name" : "Arif", "age" : 21, "department" : "CSE"} # set argument. | |
test_kwargs_argument(**kwargs) # call the function with the argument. | |
Output: | |
Argument pass by **kwargs is: age = 21 | |
Argument pass by **kwargs is: department = CSE | |
Argument pass by **kwargs is: name = Arif | |
এখন আমরা যদি একই ফাংশনে *args, **kwargs ব্যবহার করতে চাই তবে আমাদের কে একটা অর্ডার ফলো করতে হবে । যদি কোন নর্মাল আর্গুমেন্ট থাকে তবে সেটাকে প্রথমে রাখতে হবে, তারপর থাকবে *args এর পরে থাকবে **kwargs
def my_function(normal_arg, *args, **kwargs)
এই সম্পর্কে আপনি আরও জানতে চাইলে পাইথনে অফিশিয়াল ওয়েবসাইট দেখতে পারেন ।
More on define function
Comments
Post a Comment