# HG changeset patch # User Atul Varma # Date 1213167378 25200 # Node ID 8f019284f1d1b1cd61130d38c4f0ac3198625a3b # Parent 0315da3546b336465284c272fee53ac068edc445 Regenerated html file. diff -r 0315da3546b3 -r 8f019284f1d1 PythonForJsProgrammers.html --- 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() -

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 as enjoyable as possible.

+

Python is technically like JavaScript in that semicolons are optional, +but its community prescribes the opposite convention: that is, +semicolons should never 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 as enjoyable as possible.

The Interactive Shell

@@ -120,16 +123,20 @@

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.

@@ -260,15 +267,7 @@ AttributeError: 'str' object has no attribute 'foo' -

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 hasattr() function:

-
-
->>> hasattr("a string", "foo")
-False
-
-
+

In most cases, this is for the best, as it makes debugging easier.

Python also has an analog to JavaScript's null: it's called None.

@@ -343,7 +342,7 @@ ... pass -

As mentioned earlier, this string is called the docstring, and is +

As mentioned earlier, this string is called the docstring; it's actually attached to the function object as its __doc__ 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 -

Strings are just sequences of characters, so they can be used -similarly:

+

Strings are just sequences of single-character strings, so they can be +used similarly:

 >>> for c in "boof":
@@ -535,12 +534,29 @@
 'Hello bob, I need 5 dollars.'
 
+

Keys for dictionaries can actually be any immutable type; this means +that, for instance, tuples can be used as keys:

+
+
+>>> a = {(1,2) : 1}
+
+
+

But lists can't:

+
+
+>>> b = {[1,2] : 1}
+Traceback (most recent call last):
+...
+TypeError: list objects are unhashable
+
+

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.

+understanding and using them easier. But in exchange, dictionaries do +come pre-packaged with a bevy of useful methods.

Classes

@@ -869,6 +885,11 @@ the tutor mailing list, and a local user group if your area has one.

+
+

Feedback

+

If you have any suggestions or comments regarding this tutorial, +please feel free to post them to this blog post. Thanks!

+