Mercurial > python-for-js-programmers
changeset 47:acb80c6abaa8
Added a new section on equality as per suggestions from Christopher Finke and used help() instead of __doc__ as per suggestions from Brodie Rao and some others.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Tue, 10 Jun 2008 23:09:08 -0700 |
parents | f1b7aa9f7699 |
children | 852f069c23b7 |
files | PythonForJsProgrammers.html PythonForJsProgrammers.txt |
diffstat | 2 files changed, 53 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/PythonForJsProgrammers.html Fri Jun 06 14:44:07 2008 -0700 +++ b/PythonForJsProgrammers.html Tue Jun 10 23:09:08 2008 -0700 @@ -245,9 +245,7 @@ </pre> </blockquote> <p>The <tt class="docutils literal"><span class="pre">++</span></tt> and <tt class="docutils literal"><span class="pre">--</span></tt> unary assignment operators aren't part of the -Python language, and nor is JavaScript's <tt class="docutils literal"><span class="pre">===</span></tt> comparison operator -(Python's <tt class="docutils literal"><span class="pre">==</span></tt> can be considered to behave like JavaScript's -<tt class="docutils literal"><span class="pre">===</span></tt>).</p> +Python language.</p> </div> <div class="section"> <h1><a id="nothingness" name="nothingness">Nothingness</a></h1> @@ -275,6 +273,28 @@ <tt class="docutils literal"><span class="pre">None</span></tt>.</p> </div> <div class="section"> +<h1><a id="equality" name="equality">Equality</a></h1> +<p>There are some differences between Python and JavaScript when it comes +to equality; when using the <tt class="docutils literal"><span class="pre">==</span></tt> operator, for instance, Python +compares the <cite>value</cite> of objects rather than their locations in memory:</p> +<blockquote> +<pre class="doctest-block"> +>>> a = [1, 2, 3] +>>> b = [1, 2, 3] +>>> a == b +True +</pre> +</blockquote> +<p>The above expression is valid JavaScript code, but it would evaluate +to <tt class="docutils literal"><span class="pre">false</span></tt>. Python's <tt class="docutils literal"><span class="pre">is</span></tt> operator compares object identity:</p> +<blockquote> +<pre class="doctest-block"> +>>> a is b +False +</pre> +</blockquote> +</div> +<div class="section"> <h1><a id="functions" name="functions">Functions</a></h1> <p>Functions are defined like so:</p> <blockquote>
--- a/PythonForJsProgrammers.txt Fri Jun 06 14:44:07 2008 -0700 +++ b/PythonForJsProgrammers.txt Tue Jun 10 23:09:08 2008 -0700 @@ -122,15 +122,19 @@ 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 `docstring`--by querying the object's ``__doc__`` -attribute. For instance, here's how to get help on the string -object's ``join()`` method: +object--known as the `docstring`--by calling the built-in ``help()`` +function on the object. For instance, here's how to get help on the +string object's ``join()`` method: - >>> 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> This makes it easy and fun to explore the language and its environs. @@ -240,12 +244,7 @@ SyntaxError: invalid syntax The ``++`` and ``--`` unary assignment operators aren't part of the -Python language, and nor is JavaScript's ``===`` comparison operator -(Python's ``==`` can be considered to behave like JavaScript's -``===``). - -.. TODO: The last parenthetical is false; see Christopher Finke's -comment on my blog: http://www.toolness.com/wp/?p=45#comment-496 +Python language. Nothingness =========== @@ -269,6 +268,24 @@ Python also has an analog to JavaScript's ``null``: it's called ``None``. +Equality +======== + +There are some differences between Python and JavaScript when it comes +to equality; when using the ``==`` operator, for instance, Python +compares the `value` of objects rather than their locations in memory: + + >>> a = [1, 2, 3] + >>> b = [1, 2, 3] + >>> a == b + True + +The above expression is valid JavaScript code, but it would evaluate +to ``false``. Python's ``is`` operator compares object identity: + + >>> a is b + False + Functions =========