# HG changeset patch # User Atul Varma # Date 1212780918 25200 # Node ID eccfb8efc18655af0ca2f00af35095dd32a5e2db # Parent dea2264f872c0053e4392dce33522053aa21655d Added section on string formatting. diff -r dea2264f872c -r eccfb8efc186 PythonForJsProgrammers.html --- a/PythonForJsProgrammers.html Fri Jun 06 12:06:01 2008 -0700 +++ b/PythonForJsProgrammers.html Fri Jun 06 12:35:18 2008 -0700 @@ -207,6 +207,17 @@ 'hello…' +

It's also easy to format strings in Python. If you're familiar with +C's sprintf() function, Python's string interpolation operator, +%, behaves a bit like it:

+
+
+>>> "Hello %s, I need %d dollars." % ("bob", 5)
+'Hello bob, I need 5 dollars.'
+
+
+

You can find out more in the String Formatting Operations section +of the Python Library Reference.

Expressions

@@ -337,8 +348,11 @@ Creating docstrings for your functions not only helps document your code, but also makes it easier for Python users to interactively explore your code, too.

-

As in JavaScript, Functions are first-class citizens and can be passed -around as parameters to other functions and so forth.

+

It's also possible for Python functions to have arbitrary argument +lists, which is similar to JavaScript's arguments array. And as +in JavaScript, functions are first-class citizens and can be passed +around as parameters to other functions, returned by functions, and so +forth.

Global Variables

@@ -560,11 +574,20 @@ True -

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 +

Dictionaries can also be used as operands for string formatting +operations:

+
+
+>>> d = {"name" : "bob", "money" : 5}
+>>> "Hello %(name)s, I need %(money)d dollars." % d
+'Hello bob, I need 5 dollars.'
+
+
+

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.

@@ -646,7 +669,7 @@ class by using the type built-in function:

->>> class OldStyle:            # No superclass means it's old-style
+>>> class OldStyle:            # No superclass means it's old-style.
 ...     pass
 >>> class NewStyle(object):
 ...     pass
diff -r dea2264f872c -r eccfb8efc186 PythonForJsProgrammers.txt
--- a/PythonForJsProgrammers.txt	Fri Jun 06 12:06:01 2008 -0700
+++ b/PythonForJsProgrammers.txt	Fri Jun 06 12:35:18 2008 -0700
@@ -200,7 +200,18 @@
     >>> u"hello\u2026".encode("ascii", "xmlcharrefreplace")
     'hello…'
 
+It's also easy to format strings in Python.  If you're familiar with
+C's ``sprintf()`` function, Python's string interpolation operator,
+``%``, behaves a bit like it:
+
+    >>> "Hello %s, I need %d dollars." % ("bob", 5)
+    'Hello bob, I need 5 dollars.'
+
+You can find out more in the `String Formatting Operations`_ section
+of the Python Library Reference.
+
 .. _`Python 3000`: http://www.python.org/dev/peps/pep-3000/
+.. _`String Formatting Operations`: http://docs.python.org/lib/typesseq-strings.html
 
 Expressions
 ===========
@@ -313,8 +324,13 @@
 code, but also makes it easier for Python users to interactively
 explore your code, too.
 
-As in JavaScript, Functions are first-class citizens and can be passed
-around as parameters to other functions and so forth.
+It's also possible for Python functions to have `arbitrary argument
+lists`_, which is similar to JavaScript's ``arguments`` array.  And as
+in JavaScript, functions are first-class citizens and can be passed
+around as parameters to other functions, returned by functions, and so
+forth.
+
+.. _`arbitrary argument lists`: http://docs.python.org/tut/node6.html#SECTION006730000000000000000
 
 Global Variables
 ================
@@ -505,11 +521,18 @@
     >>> "a" in {"a" : 1, "b" : 2}
     True
 
-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
+Dictionaries can also be used as operands for string formatting
+operations:
+
+    >>> d = {"name" : "bob", "money" : 5}
+    >>> "Hello %(name)s, I need %(money)d dollars." % d
+    'Hello bob, I need 5 dollars.'
+
+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.
 
 Classes
@@ -581,7 +604,7 @@
 can tell that an object is an instance of an old-style or new-style
 class by using the ``type`` built-in function:
 
-    >>> class OldStyle:            # No superclass means it's old-style
+    >>> class OldStyle:            # No superclass means it's old-style.
     ...     pass
     >>> class NewStyle(object):
     ...     pass