# HG changeset patch # User Atul Varma # Date 1212706024 25200 # Node ID f8aa8d0494eda74e4a9afef1ed2a262d5c41cf8c # Parent 758a15a83be88c7ab53ef69b9c925c21785e6a5a More stuff... diff -r 758a15a83be8 -r f8aa8d0494ed PythonForJsProgrammers.txt --- a/PythonForJsProgrammers.txt Thu Jun 05 15:24:58 2008 -0700 +++ b/PythonForJsProgrammers.txt Thu Jun 05 15:47:04 2008 -0700 @@ -85,8 +85,9 @@ Python, when executed with no parameters, just presents an interactive interpreter. It's similar to the SpiderMonkey shell and xpcshell if -you're familiar with those. The code examples in this tutorial will -be displayed as though they're being executed in it, like so: +you're familiar with those. All following code examples in this +tutorial will be displayed as though they're being executed in it, +like so: >>> 1 + 2 3 @@ -97,8 +98,25 @@ ... "statement that spans multiple lines." This is a long statement that spans multiple lines. -The shell is also a useful way to explore Python's functionality; feel -free to just use it as a desk calculator to get used to it. +One built-in function in particular that helps explore things in the +built-in shell is ``dir()``, which returns a list of all the +attributes attached to an object: + + >>> dir("a string") + ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] + +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 by querying its ``__doc__`` attribute: + + >>> print "a string".join.__doc__ + S.join(sequence) -> string + + Return a string which is the concatenation of the strings in the + sequence. The separator between elements is S. + +This makes it very easy and fun to explore the language and its +environs. Expressions =========== @@ -157,7 +175,14 @@ ... AttributeError: 'str' object has no attribute 'foo' -Python does have an analog to JavaScript's ``null``: it's called +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 + +Python also has an analog to JavaScript's ``null``: it's called ``None``. Functions @@ -294,7 +319,7 @@ Dictionaries ============ -Dictionaries are a lot like Object literals in JavaScript: +Dictionaries are a bit like Object literals in JavaScript: >>> d = {"foo" : 1, "bar" : 2} >>> d["foo"] @@ -353,6 +378,23 @@ >>> doThing() 6 +Exceptions +========== + +They work as expected, and there's a number of `built-in ones`_. + +Python prefers the term ``raise`` to JavaScript's ``throw``, and +``except`` to JavaScript's ``catch``. Given this, the following +code is fairly self-explanatory: + + >>> try: + ... raise Exception("Oof") + ... except Exception, e: + ... print "Caught an exception: %s" % e + Caught an exception: Oof + +.. _`built-in ones`: http://docs.python.org/lib/module-exceptions.html + Coding Style ============