Mercurial > python-for-js-programmers
changeset 55:8f019284f1d1
Regenerated html file.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Tue, 10 Jun 2008 23:56:18 -0700 |
parents | 0315da3546b3 |
children | 29cb4fab61cb |
files | PythonForJsProgrammers.html |
diffstat | 1 files changed, 46 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/PythonForJsProgrammers.html Tue Jun 10 23:52:50 2008 -0700 +++ b/PythonForJsProgrammers.html Tue Jun 10 23:56:18 2008 -0700 @@ -73,11 +73,14 @@ else: doOtherThing() </pre> -<p>Also note that Python doesn't use semicolons, which is yet another -language feature that reduces the cognitive burden on the programmer. -Indeed, many of the language features covered below were designed with -a very careful eye towards readability, reducing cognitive load, and -making the process of programming <a class="reference" href="http://xkcd.com/353/">as enjoyable as possible</a>.</p> +<p>Python is technically like JavaScript in that semicolons are optional, +but its community prescribes the opposite convention: that is, +semicolons should <em>never</em> be used to delimit statements unless +absolutely necessary. This is yet another decision that reduces the +cognitive burden on the programmer; indeed, many of the language +features covered below were designed with a very careful eye towards +readability, reducing cognitive load, and making the process of +programming <a class="reference" href="http://xkcd.com/353/">as enjoyable as possible</a>.</p> </div> <div class="section"> <h1><a id="the-interactive-shell" name="the-interactive-shell">The Interactive Shell</a></h1> @@ -120,16 +123,20 @@ </blockquote> <p>If there's a function you're interested in learning more about, you can look at the built-in documentation metadata associated with the -object--known as the <cite>docstring</cite>--by querying the object's <tt class="docutils literal"><span class="pre">__doc__</span></tt> -attribute. For instance, here's how to get help on the string -object's <tt class="docutils literal"><span class="pre">join()</span></tt> method:</p> +object--known as the <cite>docstring</cite>--by calling the built-in <tt class="docutils literal"><span class="pre">help()</span></tt> +function on the object. For instance, here's how to get help on the +string object's <tt class="docutils literal"><span class="pre">join()</span></tt> method:</p> <blockquote> <pre class="doctest-block"> ->>> print "a string".join.__doc__ -S.join(sequence) -> string +>>> help("a string".join) +Help on built-in function join: <BLANKLINE> -Return a string which is the concatenation of the strings in the -sequence. The separator between elements is S. +join(...) + S.join(sequence) -> string +<BLANKLINE> + Return a string which is the concatenation of the strings in the + sequence. The separator between elements is S. +<BLANKLINE> </pre> </blockquote> <p>This makes it easy and fun to explore the language and its environs.</p> @@ -260,15 +267,7 @@ AttributeError: 'str' object has no attribute 'foo' </pre> </blockquote> -<p>In most cases, this is for the best, as it makes debugging easier. If -you really need to find out if an object has a particular attribute, -however, you can use the <tt class="docutils literal"><span class="pre">hasattr()</span></tt> function:</p> -<blockquote> -<pre class="doctest-block"> ->>> hasattr("a string", "foo") -False -</pre> -</blockquote> +<p>In most cases, this is for the best, as it makes debugging easier.</p> <p>Python also has an analog to JavaScript's <tt class="docutils literal"><span class="pre">null</span></tt>: it's called <tt class="docutils literal"><span class="pre">None</span></tt>.</p> </div> @@ -343,7 +342,7 @@ ... pass </pre> </blockquote> -<p>As mentioned earlier, this string is called the docstring, and is +<p>As mentioned earlier, this string is called the docstring; it's actually attached to the function object as its <tt class="docutils literal"><span class="pre">__doc__</span></tt> attribute. Creating docstrings for your functions not only helps document your code, but also makes it easier for Python users to interactively @@ -401,8 +400,8 @@ there </pre> </blockquote> -<p>Strings are just sequences of characters, so they can be used -similarly:</p> +<p>Strings are just sequences of single-character strings, so they can be +used similarly:</p> <blockquote> <pre class="doctest-block"> >>> for c in "boof": @@ -535,12 +534,29 @@ 'Hello bob, I need 5 dollars.' </pre> </blockquote> +<p>Keys for dictionaries can actually be any immutable type; this means +that, for instance, tuples can be used as keys:</p> +<blockquote> +<pre class="doctest-block"> +>>> a = {(1,2) : 1} +</pre> +</blockquote> +<p>But lists can't:</p> +<blockquote> +<pre class="doctest-block"> +>>> b = {[1,2] : 1} +Traceback (most recent call last): +... +TypeError: list objects are unhashable +</pre> +</blockquote> <p>Python dictionaries generally aren't used to create arbitrary objects like they are in Javascript; they don't have prototypes, nor do they have meta-methods. Instead, classes are used to do that sort of thing. In some ways, this is unfortunate, since the simplicity of conflating objects with dictionaries, as JavaScript and Lua do, makes -understanding and using them easier.</p> +understanding and using them easier. But in exchange, dictionaries do +come pre-packaged with a bevy of <a class="reference" href="http://docs.python.org/lib/typesmapping.html">useful methods</a>.</p> </div> <div class="section"> <h1><a id="classes" name="classes">Classes</a></h1> @@ -869,6 +885,11 @@ the <a class="reference" href="http://mail.python.org/mailman/listinfo/tutor">tutor mailing list</a>, and a <a class="reference" href="http://wiki.python.org/moin/LocalUserGroups">local user group</a> if your area has one.</p> </div> +<div class="section"> +<h1><a id="feedback" name="feedback">Feedback</a></h1> +<p>If you have any suggestions or comments regarding this tutorial, +please feel free to post them to <a class="reference" href="http://www.toolness.com/wp/?p=45">this blog post</a>. Thanks!</p> +</div> </div> </body> </html>