Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>we know that we have to declare a function before we use it</p> </blockquote> <p>It sounds like you're coming from a language in the C family. These languages are different in two ways.</p> <p>First, Python doesn't "declare" anything. That makes sense in languages like C, where variables are typed memory locations that values get stored in. But in Python, values have their own independent types and locations, and variables are just names for those values, so there is nothing to declare.</p> <p>Now, Python requires you to <em>define</em> a function before you <em>use</em> it, just like C. </p> <p>But only C <em>also</em> requires you to define, or at least declare, a function before you compile a function whose body uses it. The difference is the same as above—C needs the function to be declared, to get the types, the pointer to the function's actual code, etc.; in Python, the function is just a name for the function-object value. To compile a function, all it needs to know is where to look up the names. And that's easy: it compiles the function so that, at runtime, it will look up the name in the module globals. Since no other information is needed, there's nothing to declare.</p> <p>For example, in C, this is legal:</p> <pre><code>void foo() {} void bar() { foo(); } </code></pre> <p>And so is this:</p> <pre><code>void foo(); void bar() { foo(); } void foo() {} </code></pre> <p>But this is not:</p> <pre><code>void bar() { foo(); } void foo(); </code></pre> <p>In Python, because there are no declarations, the second and third are identical, and therefore both perfectly valid:</p> <pre><code>def bar(): foo() def foo(): pass </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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