Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your explanation is correct:</p> <blockquote> <p>the class name binding is after the execution of class block and before the execution of function block.</p> </blockquote> <p>This is just saying that a class block is executed right away, while a function block is not executed until the function is called. Note that in both cases the name is not bound until after the object (class or function) is created; it's just that function bodies are executed <em>after</em> the function is created, while class bodies are executed <em>before</em> the class is created (as part of the class creation process).</p> <p>This is because classes and functions are different beasts: when you define a class, you are defining "right now" what the class should contain (i.e., its methods and attributes); when you define a function, you are defining what is to happen at some later point (when you call it).</p> <p><a href="http://docs.python.org/2/reference/compound_stmts.html#class-definitions" rel="nofollow">The documentation</a> makes it clear:</p> <blockquote> <p>A class definition is an executable statement. It first evaluates the inheritance list, if present. [...] The class’s suite is then executed [...]</p> </blockquote> <p>The class body is executed at the time the <code>class</code> statement is executed. This is different from some other languages, where a class definition is a "declaration" which is not executed in the linear order of the source file like other statements. (I trust it's obvious why the function body is <em>not</em> executed when the function is defined -- there wouldn't be much point in putting code in a function if it just ran right away.)</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