Python إذا تم شرح __name__ == __main__ بأمثلة على التعليمات البرمجية
عندما يقرأ مترجم بايثون ملف بايثون ، فإنه يحدد أولاً بعض المتغيرات الخاصة. ثم يقوم بتنفيذ التعليمات البرمجية من الملف.
أحد هذه المتغيرات يسمى __name__
.
إذا اتبعت هذه المقالة خطوة بخطوة وقراءة مقتطفات الشفرة الخاصة بها ، فستتعلم كيفية استخدامها if __name__ == "__main__"
وسبب أهميتها.
شرح وحدات بايثون
تسمى ملفات Python بالوحدات النمطية ويتم تحديدها بواسطة .py
امتداد الملف. يمكن للوحدة أن تحدد الوظائف والفئات والمتغيرات.
لذلك عندما يقوم المترجم الفوري بتشغيل وحدة نمطية ، __name__
سيتم تعيين المتغير كما __main__
لو أن الوحدة النمطية التي يتم تشغيلها هي البرنامج الرئيسي.
ولكن إذا كان الكود يستورد الوحدة من وحدة أخرى ، __name__
فسيتم تعيين المتغير على اسم تلك الوحدة.
دعنا نلقي نظرة على مثال. قم بإنشاء وحدة Python المسماة file_one.py
والصق رمز المستوى الأعلى هذا بالداخل:
# Python file one module print("File one __name__ is set to: {}" .format(__name__))
من خلال تشغيل هذا الملف ، سترى بالضبط ما كنا نتحدث عنه. تم تعيين المتغير __name__
لهذه الوحدة على __main__
:
File one __name__ is set to: __main__
أضف الآن ملفًا آخر باسم file_two.py
والصق هذا الرمز بداخله:
# Python module to import print("File two __name__ is set to: {}" .format(__name__))
أيضًا ، قم بتعديل الكود في file_one.py
مثل هذا حتى نقوم باستيراد file_two
الوحدة:
# Python module to execute import file_two print("File one __name__ is set to: {}" .format(__name__))
file_one
سيظهر تشغيل الكود الخاص بنا مرة أخرى أن __name__
المتغير في the file_one
لم يتغير ، وسيظل مضبوطًا على __main__
. ولكن الآن تم تعيين المتغير __name__
في file_two
كاسم الوحدة ، وبالتالي file_two
.
يجب أن تبدو النتيجة كما يلي:
File two __name__ is set to: file_two File one __name__ is set to: __main__
لكن قم بالركض file_two
مباشرة وسترى أنه تم تعيين اسمه على __main__
:
File two __name__ is set to: __main__
سيكون المتغير __name__
للملف / الوحدة التي يتم تشغيلها دائمًا __main__
. لكن __name__
المتغير لجميع الوحدات الأخرى التي يتم استيرادها سيتم تعيينه على اسم الوحدة الخاصة بهم.
اصطلاحات تسمية ملف بايثون
بالطريقة المعتادة استخدام __name__
و __main__
يشبه هذا:
if __name__ == "__main__": Do something here
دعونا نرى كيف يعمل هذا في الحياة الواقعية ، وكيفية استخدام هذه المتغيرات بالفعل.
تعديل file_one
و file_two
لتبدو مثل هذا:
file_one
:
# Python module to execute import file_two print("File one __name__ is set to: {}" .format(__name__)) if __name__ == "__main__": print("File one executed when ran directly") else: print("File one executed when imported")
file_two
:
# Python module to import print("File two __name__ is set to: {}" .format(__name__)) if __name__ == "__main__": print("File two executed when ran directly") else: print("File two executed when imported")
مرة أخرى ، عند التشغيل file_one
، سترى أن البرنامج تعرف على أي من هاتين الوحدتين يتم __main__
تنفيذه وتنفيذ الكود وفقًا لبياناتنا الأولى if else
.
يجب أن تبدو النتيجة كما يلي:
File two __name__ is set to: file_two File two executed when imported File one __name__ is set to: __main__ File one executed when ran directly
الآن قم بتشغيل file_two
وسترى أن __name__
المتغير مضبوط على __main__
:
File two __name__ is set to: __main__ File two executed when ran directly
عندما يتم استيراد وتشغيل وحدات مثل هذه ، سيتم استيراد وظائفها وتنفيذ كود المستوى الأعلى.
لمشاهدة هذه العملية قيد التنفيذ ، قم بتعديل ملفاتك لتبدو كما يلي:
file_one
:
# Python module to execute import file_two print("File one __name__ is set to: {}" .format(__name__)) def function_one(): print("Function one is executed") def function_two(): print("Function two is executed") if __name__ == "__main__": print("File one executed when ran directly") else: print("File one executed when imported")
file_two
:
# Python module to import print("File two __name__ is set to: {}" .format(__name__)) def function_three(): print("Function three is executed") if __name__ == "__main__": print("File two executed when ran directly") else: print("File two executed when imported")
الآن يتم تحميل الوظائف ولكن لا يتم تشغيلها.
لتشغيل إحدى هذه الوظائف ، قم بتعديل if __name__ == "__main__"
الجزء file_one
ليبدو كما يلي:
if __name__ == "__main__": print("File one executed when ran directly") function_two() else: print("File one executed when imported")
عند الركض file_one
يجب أن ترى مثل هذا:
File two __name__ is set to: file_two File two executed when imported File one __name__ is set to: __main__ File one executed when ran directly Function two is executed
أيضا ، يمكنك تشغيل وظائف من الملفات المستوردة. للقيام بذلك ، قم بتعديل if __name__ == “__main__”
الجزء file_one
ليبدو كما يلي:
if __name__ == "__main__": print("File one executed when ran directly") function_two() file_two.function_three() else: print("File one executed when imported")
ويمكنك توقع نتيجة مثل هذه:
File two __name__ is set to: file_two File two executed when imported File one __name__ is set to: __main__ File one executed when ran directly Function two is executed Function three is executed
لنفترض الآن أن file_two
الوحدة كبيرة جدًا وتحتوي على الكثير من الوظائف (اثنان في حالتنا) ، ولا تريد استيرادها جميعًا. عدّل file_two
لتبدو هكذا:
# Python module to import print("File two __name__ is set to: {}" .format(__name__)) def function_three(): print("Function three is executed") def function_four(): print("Function four is executed") if __name__ == "__main__": print("File two executed when ran directly") else: print("File two executed when imported")
And to import the specific functions from the module, use the from
import block in the file_one
file:
# Python module to execute from file_two import function_three print("File one __name__ is set to: {}" .format(__name__)) def function_one(): print("Function one is executed") def function_two(): print("Function two is executed") if __name__ == "__main__": print("File one executed when ran directly") function_two() function_three() else: print("File one executed when imported")
Conclusion
There is a really nice use case for the __name__
variable, whether you want a file that can be run as the main program or imported by other modules. We can use an if __name__ == "__main__"
block to allow or prevent parts of code from being run when the modules are imported.
When the Python interpreter reads a file, the __name__
variable is set as __main__
if the module being run, or as the module's name if it is imported. Reading the file executes all top level code, but not functions and classes (since they will only get imported).
Bra gjort! (That means "Well done" in Swedish!)
تحقق من المزيد من المقالات مثل هذا في ملف التعريف freeCodeCamp الخاص بي وملف التعريف المتوسط وغير ذلك من الأشياء الممتعة التي أقوم بإنشائها على صفحة GitHub الخاصة بي.