Python प्रोग्राम कैसे Execute करता है?
इस लेख में हम सीखेंगे कि Python कैसे किसी प्रोग्राम को execute करता है और यह समझेंगे कि Python के प्रोग्राम को रन होते समय किन किन प्रक्रिया से गुजरना होता है।
हमारा कंप्युटर सिर्फ मशीनी भाषा ही समझता है अर्थात 0 तथा 1 की भाषा। High level programming language (जैसे C, C++, Java, Python) में लिखे गए कोड को को कंप्युटर नहीं समझता है। कंप्युटर को इसमे से किसी भाषा मे लिखे प्रोग्राम को समझने के लिए एक Language Translator की आवश्यकता होती है। कंप्यूटर अनुवादक (Translators) वे प्रोग्राम होते हैं जो high-level programming languages (human-readable form) में लिखे गए कोड को machine language (binary code) में परिवर्तित करते हैं ताकि कंप्यूटर उसे समझ सके और execute कर सके। कंप्यूटर अनुवादकों के तीन मुख्य प्रकार होते हैं – 1. कम्पाइलर (Compiler), 2. इंटरप्रेटर (Interpreter) और 3. एसेंबलर (Assembler)
कम्पाइलर (Compiler) एक ऐसा प्रोग्राम है जो पूरे source code को एक बार में पढ़ता है और उसे machine code में परिवर्तित करता है। यह conversion एक साथ पूरी प्रोग्राम फाइल के लिए होती है और output एक executable file के रूप में उत्पन्न होती है।
इंटरप्रेटर एक ऐसा प्रोग्राम है जो source code को line-by-line पढ़ता है और execute करता है। यह प्रत्येक लाइन को runtime पर machine code में बदलता है और तुरंत execute करता है।
एसेंबलर एक ऐसा प्रोग्राम है जो assembly language (low-level language) में लिखे गए कोड को machine code में परिवर्तित करता है। Assembly language, machine language के बहुत करीब होती है और hardware instructions को represent करती है।
Python: Interpreted और Compiled का मिश्रण
Python को आमतौर पर interpreted language कहा जाता है क्योंकि इसकी final execution interpreter द्वारा होती है। लेकिन इसे पूरी तरह से interpreted language कहना गलत होगा क्योंकि इसमें एक compilation चरण भी शामिल है। इसे hybrid approach कहा जा सकता है जैसा कि निम्न चित्र मे दर्शित हो रहा है –

आइए इसे एक उदाहरण के माध्यम से समझते है –
1. Source Code
सबसे पहले, हम Python source code लिखते हैं, जो .py फाइल में स्टोर होता है। उदाहरण के लिए :
2. Compilation Phase
Python interpreter इस script को पढ़ता है और इसे bytecode में compile करता है। i.e
print("Hello, World!") -> bytecode (hello.pyc).
a. Lexical Analysis
इस चरण में, Python interpreter source code को tokens में तोड़ता है। Tokens basic elements होते हैं: keywords, identifiers, literals, and operators। Lexical analyzer, जिसे lexer या scanner भी कहते हैं, इस काम को करता है।उदाहरण के लिए, print(“Hello, World!”) को tokens में तोड़ा जाएगा:
print("Hello, World!")
b. Syntax Analysis (Parsing)
Tokens को parse किया जाता है और एक Abstract Syntax Tree (AST) में बदल दिया जाता है। Syntax analyzer (parser) यह सुनिश्चित करता है कि code का structure grammar rules के अनुसार है।
AST एक tree structure है जो code की syntactical structure को दिखाता है।
c. Semantic Analysis
Semantic analysis में interpreter यह सुनिश्चित करता है कि parsed code का अर्थ सही है। उदाहरण के लिए, यह जांचता है कि variables को सही तरीके से declare किया गया है या functions को सही arguments के साथ call किया गया है।
d. Intermediate Code Generation – Byte Code
AST को intermediate code में बदल दिया जाता है, जिसे bytecode कहते हैं। यह bytecode .pyc फाइल में स्टोर किया जा सकता है। Bytecode एक low-level, platform-independent representation है।
3 . Interpretation Phase
PVM bytecode को interpret करता है और अंततः machine code में translate करके execute करता है, जिससे output “Hello, World!” प्रकट होता है।
a. Bytecode Interpretation
Python Virtual Machine (PVM) bytecode को execute करता है। PVM एक stack-based virtual machine है जो instructions को execute करने के लिए stack का उपयोग करता है। यह bytecode को machine code में translate करता है जिसे processor समझ सकता है।
b. Execution
PVM bytecode instructions को sequentially execute करता है। इस process में variables का value, function calls, और other operations को manage करता है।
Python में interpreted और compiled दोनों elements होते हैं। यह source code को पहले bytecode में compile करता है और फिर bytecode को interpret करके execute करता है। किन्तु compilation वाला पार्ट programmers के लिए हाइड होता है और इस कारण Python को interpreted भाषा माना जाता है।