mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
STATUS=CLOSED dropped how-Python-datatypes-compare section and table because it's misleading and I don't care enough to fix it
This commit is contained in:
@@ -806,40 +806,17 @@ buildConnectionString Build a connection string from a dictionary Retur
|
||||
<samp>buildConnectionString Build a connection string from a dictionary
|
||||
|
||||
Returns string.
|
||||
</span></pre><h2 id="apihelper.optional">4.2. Using Optional and Named Arguments</h2>
|
||||
<p>Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default
|
||||
value. Futhermore, arguments can be specified in any order by using named arguments. Stored procedures in SQL Server Transact/<abbr>SQL</abbr> can do this, so if you're a SQL Server scripting guru, you can skim this part.
|
||||
|
||||
<p>Here is an example of <code>info</code>, a function with two optional arguments:<pre><code>
|
||||
def info(object, spacing=10, collapse=1):</pre><p><var>spacing</var> and <var>collapse</var> are optional, because they have default values defined. <var>object</var> is required, because it has no default value. If <code>info</code> is called with only one argument, <var>spacing</var> defaults to <code>10</code> and <var>collapse</var> defaults to <code>1</code>. If <code>info</code> is called with two arguments, <var>collapse</var> still defaults to <code>1</code>.
|
||||
<p>Say you want to specify a value for <var>collapse</var> but want to accept the default value for <var>spacing</var>. In most languages, you would be out of luck, because you would need to call the function with three arguments. But in
|
||||
Python, arguments can be specified by name, in any order.
|
||||
<div class=example><h3>Example 4.4. Valid Calls of <code>info</code></h3><pre><code>
|
||||
info(odbchelper) <span>①</span>
|
||||
info(odbchelper, 12) <span>②</span>
|
||||
info(odbchelper, collapse=0) <span>③</span>
|
||||
info(spacing=15, object=odbchelper) <span>④</span></pre>
|
||||
<ol>
|
||||
<li>With only one argument, <var>spacing</var> gets its default value of <code>10</code> and <var>collapse</var> gets its default value of <code>1</code>.
|
||||
<li>With two arguments, <var>collapse</var> gets its default value of <code>1</code>.
|
||||
<li>Here you are naming the <var>collapse</var> argument explicitly and specifying its value. <var>spacing</var> still gets its default value of <code>10</code>.
|
||||
<li>Even required arguments (like <var>object</var>, which has no default value) can be named, and named arguments can appear in any order.
|
||||
<p>This looks totally whacked until you realize that arguments are simply a dictionary. The “normal” method of calling functions without argument names is actually just a shorthand where Python matches up the values with the argument names in the order they're specified in the function declaration. And most of the
|
||||
time, you'll call functions the “normal” way, but you always have the additional flexibility if you need it.
|
||||
<table id="tip.arguments" class=note border="0" summary="">
|
||||
|
||||
<td rowspan="2" align="center" valign="top" width="1%"><img src="images/note.png" alt="Note" title="" width="24" height="24"><td colspan="2" align="left" valign="top" width="99%">The only thing you need to do to call a function is specify a value (somehow) for each required argument; the manner and order
|
||||
in which you do that is up to you.
|
||||
<div class=itemizedlist>
|
||||
<h3>Further Reading on Optional Arguments</h3>
|
||||
<ul>
|
||||
<li><a href="http://www.python.org/doc/current/tut/tut.html"><i class=citetitle>Python Tutorial</i></a> discusses exactly <a href="http://www.python.org/doc/current/tut/node6.html#SECTION006710000000000000000">when and how default arguments are evaluated</a>, which matters when the default value is a list or an expression with side effects.
|
||||
|
||||
</ul>
|
||||
<h2 id="apihelper.builtin">4.3. Using <code>type</code>, <code>str</code>, <code>dir</code>, and Other Built-In Functions</h2>
|
||||
<p>Python has a small set of extremely useful built-in functions. All other functions are partitioned off into modules. This was
|
||||
actually a conscious design decision, to keep the core language from getting bloated like other scripting languages (cough
|
||||
cough, Visual Basic).
|
||||
|
||||
|
||||
(optional and named arguments stuff was here)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<h3>4.3.1. The <code>type</code> Function</h3>
|
||||
<p>The <code>type</code> function returns the datatype of any arbitrary object. The possible types are listed in the <code>types</code> module. This is useful for helper functions that can handle several types of data.
|
||||
<div class=example><h3 id="apihelper.type.intro">Example 4.5. Introducing <code>type</code></h3><pre class=screen><samp class=p>>>> </samp><kbd>type(1)</kbd> <span>①</span>
|
||||
|
||||
@@ -70,33 +70,11 @@ if __name__ == "__main__":
|
||||
<blockquote class=note>
|
||||
<p><span>☞</span>In some languages, functions (that return a value) start with <code>function</code>, and subroutines (that do not return a value) start with <code>sub</code>. There are no subroutines in Python. Everything is a function, all functions return a value (even if it’s <code>None</code>), and all functions start with <code>def</code>.
|
||||
</blockquote>
|
||||
<p>The <code>approximate_size</code> function takes the two arguments — <var>size</var> and <var>a_kilobyte_is_1024_bytes</var> — but neither argument specifies a datatype. (As you might guess from the <code>=True</code> syntax, the second argument is a boolean. You’ll learn what that syntax does in [FIXME xref-was-#apihelper].) In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally.
|
||||
<p>The <code>approximate_size</code> function takes the two arguments — <var>size</var> and <var>a_kilobyte_is_1024_bytes</var> — but neither argument specifies a datatype. In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally.
|
||||
<blockquote class="note compare java">
|
||||
<p><span>☞</span>In Java and other statically-typed languages, you must specify the datatype of the function return value and each function argument. In Python, you never explicitly specify the datatype of anything. Based on what value you assign, Python keeps track of the datatype internally.
|
||||
</blockquote>
|
||||
<h3 id=datatypes>How Python’s Datatypes Compare to Other Programming Languages</h3>
|
||||
<p>An erudite reader sent me this explanation of how Python compares to other programming languages:
|
||||
<dl>
|
||||
<dt>statically typed language</dt>
|
||||
<dd>A language in which types are fixed at compile time. Most statically typed languages enforce this by requiring you to declare all variables with their datatypes before using them. Java and <abbr>C</abbr> are statically typed languages.
|
||||
</dd>
|
||||
<dt>dynamically typed language</dt>
|
||||
<dd>A language in which types are discovered at execution time; the opposite of statically typed. JavaScript and Python are dynamically typed, because they figure out what type a variable is when you first assign it a value.
|
||||
</dd>
|
||||
<dt>strongly typed language</dt>
|
||||
<dd>A language in which types are always enforced. Java and Python are strongly typed. If you have an integer, you can’t treat it like a string without explicitly converting it.
|
||||
</dd>
|
||||
<dt>weakly typed language</dt>
|
||||
<dd>A language in which types are “automagically” coerced to other types as needed; the opposite of strongly typed. <abbr>PHP</abbr> is weakly typed. In <abbr>PHP</abbr>, you can concatenate the string <code>'12'</code> and the integer <code>3</code> to get the string <code>'123'</code>, then treat that as the integer <code>123</code>, all without any explicit conversion.
|
||||
</dd>
|
||||
</dl>
|
||||
<p>So Python is both <em>dynamically typed</em> (because it doesn’t use explicit datatype declarations) and <em>strongly typed</em> (because once a variable has a datatype, it actually matters).
|
||||
<p>If you have experience in other programming languages, this table may help you visualize how Python compares to them:
|
||||
<table>
|
||||
<tr><th><th>Statically typed<th>Dynamically typed
|
||||
<tr><th>Weakly typed<td>C, Objective-C<td>JavaScript, Perl 5, <abbr>PHP</abbr>
|
||||
<tr><th>Strongly typed<td>Pascal, Java<td>Python, Ruby
|
||||
</table>
|
||||
|
||||
<h2 id=readability>Writing Readable Code</h2>
|
||||
<p>I won’t bore you with a long finger-wagging speech about the importance of documenting your code. Just know that code is written once but read many times, and the most important audience for your code is yourself, six months after writing it (i.e. after you’ve forgotten everything but need to fix something). Python makes it easy to write readable code, so take advantage of it. You’ll thank me in six months.
|
||||
<h3 id=docstrings>Documentation Strings</h3>
|
||||
|
||||
Reference in New Issue
Block a user