Mercurial > python-for-js-programmers
changeset 38:eccfb8efc186
Added section on string formatting.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Fri, 06 Jun 2008 12:35:18 -0700 |
parents | dea2264f872c |
children | b3fdf83125c2 |
files | PythonForJsProgrammers.html PythonForJsProgrammers.txt |
diffstat | 2 files changed, 62 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- 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&#8230;' </pre> </blockquote> +<p>It's also easy to format strings in Python. If you're familiar with +C's <tt class="docutils literal"><span class="pre">sprintf()</span></tt> function, Python's string interpolation operator, +<tt class="docutils literal"><span class="pre">%</span></tt>, behaves a bit like it:</p> +<blockquote> +<pre class="doctest-block"> +>>> "Hello %s, I need %d dollars." % ("bob", 5) +'Hello bob, I need 5 dollars.' +</pre> +</blockquote> +<p>You can find out more in the <a class="reference" href="http://docs.python.org/lib/typesseq-strings.html">String Formatting Operations</a> section +of the Python Library Reference.</p> </div> <div class="section"> <h1><a id="expressions" name="expressions">Expressions</a></h1> @@ -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.</p> -<p>As in JavaScript, Functions are first-class citizens and can be passed -around as parameters to other functions and so forth.</p> +<p>It's also possible for Python functions to have <a class="reference" href="http://docs.python.org/tut/node6.html#SECTION006730000000000000000">arbitrary argument +lists</a>, which is similar to JavaScript's <tt class="docutils literal"><span class="pre">arguments</span></tt> 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.</p> </div> <div class="section"> <h1><a id="global-variables" name="global-variables">Global Variables</a></h1> @@ -560,11 +574,20 @@ True </pre> </blockquote> -<p>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 +<p>Dictionaries can also be used as operands for string formatting +operations:</p> +<blockquote> +<pre class="doctest-block"> +>>> d = {"name" : "bob", "money" : 5} +>>> "Hello %(name)s, I need %(money)d dollars." % d +'Hello bob, I need 5 dollars.' +</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> </div> <div class="section"> @@ -646,7 +669,7 @@ class by using the <tt class="docutils literal"><span class="pre">type</span></tt> built-in function:</p> <blockquote> <pre class="doctest-block"> ->>> class OldStyle: # No superclass means it's old-style +>>> class OldStyle: # No superclass means it's old-style. ... pass >>> class NewStyle(object): ... pass
--- 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