Insights Blog
-- Browse All Articles --
Physics Articles
Physics Tutorials
Physics Guides
Physics FAQ
Math Articles
Math Tutorials
Math Guides
Math FAQ
Education Articles
Education Guides
Bio/Chem Articles
Technology Guides
Computer Science Tutorials
Forums
Chemistry
Biology and Medical
Earth Sciences
Computer Science
Computing and Technology
DIY Projects
Trending
Featured Threads
Log in
Register
What's new
Search
Search
Search titles only
By:
Chemistry
Biology and Medical
Earth Sciences
Computer Science
Computing and Technology
DIY Projects
Menu
Log in
Register
Navigation
More options
Contact us
Close Menu
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Forums
Other Sciences
Programming and Computer Science
Confused about name binding in python
Reply to thread
Message
[QUOTE="pbuk, post: 6567828, member: 385977"] I'm going to tackle 1 and 2 together. Here is a program that adds 2 and 2: [code=Python] print(2 + 2) [/code] That's great, but if I want to add 2 and 3 I would need to write another program: [code=Python] print(2 + 3) [/code] Instead we use variables (almost all computer languages have this concept). [code=Python] x = int(input()) y = int(input()) print(x + y) [/code] Now I can enter any two integers I want and the program will print the sum. You don't need to know the answer to 3 to write programs in Python, but since you asked, this is what Python effectively does: [LIST=1] [*]Parse the code [icode]x = 1[/icode] and translate it into the following actions: [LIST=1] [*]save a pattern to memory in the form of an integer object with the value 1 [*]create an entry in the [I]symbol table[/I] mapping the variable name [icode]x[/icode] to the memory location of the object above [/LIST] [*]Parse the code [icode]x = 5[/icode] and translate it into the following actions: [LIST=1] [*]save a pattern to memory in the form of an integer object with the value 5 [*]change the entry for [icode]x[/icode] in the symbol table remapping it to the memory location of the new object above [/LIST] [*]Parse the code [icode]y = 2[/icode] and translate it into the following actions: [LIST=1] [*]save a pattern to memory in the form of an integer object with the value 2 [*]create an entry in the symbol table mapping the variable name [icode]y[/icode] to the memory location of the object above [/LIST] [*]Parse the code [icode]x = y[/icode] and translate it into the following action: [LIST=1] [*]look up [icode]y[/icode] in the symbol table and copy it's memory location to the memory location of [icode]x[/icode] [/LIST] [/LIST] Note that for performance reasons, Python in its most common implementation (CPython) doesn't do these things in the order I set them out: it does all of the parsing at once creating an [I]abstract syntax tree[/I] AST, and then works through the AST creating [I]bytecode[/I] which is simply a digital representation of the instructions I wrote above, finally it launches a form of virtual machine (the Python [I]runtime[/I]) which executes the bytecode. Finally note that only truly interpreted languages work the way I first wrote above: in 2021 the only significant such languages are bash scripts (posix)/batch files (windows). In between this and object code compilers such as C++ there is a range from languages that only create an AST (such as Lisp in most implementations) to just in time (JIT) compilers such as Google's V8 engine for JavaScript which turn the byte code into machine level instructions. [/QUOTE]
Insert quotes…
Post reply
Forums
Other Sciences
Programming and Computer Science
Confused about name binding in python
Back
Top