Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>What happens now? line_count = 3 or will it be line_count= current_line = 3?</p> </blockquote> <p><code>line_count</code> is bound to 3 inside the call to <code>print_a_line</code>. The variable <code>current_line</code> is unaffected by the call.</p> <blockquote> <p>Also, is it a bad thing if I pass different argument names(like I did here with current_line instead of line_count?)</p> </blockquote> <p>It's not a bad thing at all. You are using positional arguments rather than named arguments.</p> <p>In Python, there are two kinds of arguments: positional arguments and named arguments. A function declared like your <code>print_a_line</code> function above is using only positional arguments. A function with only named arguments might look like</p> <pre><code>def print_a_line_2(line_count=3, f=None): if f is not None: print line_count, f.readline() </code></pre> <p>A function with both positional and named arguments might look like</p> <pre><code>def print_a_line_2(line_count, f, append_newline=True): if append_newline: print line_count, f.readline() else: print line_count, f.readline(), </code></pre> <p>The reason that positional arguments are called positional is that only the position of the arguments you pass matters. So you can write any two expression you like as the arguments to <code>print_a_line</code>, and whichever argument is passed first will be bound to <code>line_count</code>, and whichever argument is passed second will be bound to <code>f</code> during the execution of <code>print_a_line</code>.</p> <p>That's not the case for <em>named arguments</em>, but the syntax is different there. To pass a named argument, you write <code>name=expression</code> instead of simply writing <code>expression</code>. So, to call <code>print_a_line_2</code>, you could write</p> <pre><code>print_a_line_2(line_count=3, f=current_file) </code></pre> <p>and the names of the arguments come from what is <em>before</em> the equals sign, not what is after the equals sign. </p> <p>For more on named and positional arguments, I would recommend checking out <a href="http://docs.python.org/tutorial/controlflow.html#more-on-defining-functions" rel="nofollow">the Python tutorial</a>.</p> <p><strong>EDIT</strong>: What happens under the hood whenever you call <code>line_count(current_line, current_file)</code></p> <p>The Python interpreter maintains several pieces of information about your program, and some of the important things it maintains are a symbol table, which binds names (variables) to their values, and a current statement pointer. Whenever the current statement pointer reaches the line</p> <pre><code>print_a_line(current_line, current_file) </code></pre> <p>the Python interpreter looks up <code>print_a_line</code>, <code>current_line</code>, and <code>current_file</code> in the symbol table. It finds that <code>print_a_line</code> is bound to the function that you defined in your question, that <code>current_line</code> is bound to 3, and that <code>current_file</code> is bound to a file object (this is a big implementation-defined data structure, which for ease of notation I'll call F, with the uppercase F distinguished from the lowercase <code>f</code> we will meet in a bit). Since <code>print_a_line</code> is a function, the interpreter calls the function with arguments 3 and F. To accomplish this, it saves the current state of execution, binds variable <code>line_count</code> to 3 and <code>f</code> to F in the symbol table, and moves the current statement pointer to the first line of the <code>print_a_line</code> function, which is</p> <pre><code>print line_count, f.readline() </code></pre> <p>It then executes the built-in <code>print</code> statement in much the same way as it executed the original function call, looking up all variables in the symbol table and making a function call to <code>f.readline()</code> in the same way as the overall function call to <code>print_a_line</code>. Then, when the <code>print_a_line</code> function returns, the Python interpreter removes <code>line_count</code> and <code>f</code> from the symbol table, and moves the statement pointer back to the location it saved earlier. It then continues execution with the line of code after the call to <code>print_a_line</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload