comparison source/know-in-python.rst @ 211:4e6cfa066d1a

Use double backticks for inline code everywhere
author Radomir Dopieralski <radomir.dopieralski@allegro.pl>
date Fri, 18 May 2012 16:56:15 +0200
parents 1ba803131dd9
children
comparison
equal deleted inserted replaced
210:622b8eec7dd6 211:4e6cfa066d1a
45 45
46 In Python, when you create a new name, it goes by default to the *local* scope 46 In Python, when you create a new name, it goes by default to the *local* scope
47 (when you are at the module scope, it's equal to the *global* scope). 47 (when you are at the module scope, it's equal to the *global* scope).
48 You don't need to do anything special to make that happen. 48 You don't need to do anything special to make that happen.
49 49
50 In JavaScript, variables go to the global object by default. You have to use the *var* keyword to have them defined locally. 50 In JavaScript, variables go to the global object by default. You have to use
51 the ``var`` keyword to have them defined locally.
51 52
52 53
53 Functions passed as parameters 54 Functions passed as parameters
54 ------------------------------ 55 ------------------------------
55 56
56 Everything is an object in Python. In particular, a function is an object. This 57 Everything is an object in Python. In particular, a function is an object. This
57 means that you can assign a function to a variable or pass it as a function 58 means that you can assign a function to a variable or pass it as a function
58 argument. This may be useful when you want to have some generic algorithms. 59 argument. This may be useful when you want to have some generic algorithms.
59 This is used in the *sort* method so that you can, for example, sort strings in 60 This is used in the ``sort`` method so that you can, for example, sort strings in
60 case-insensitive way: 61 case-insensitive way:
61 62
62 .. code-block:: python 63 .. code-block:: python
63 64
64 def lowercase(s): 65 def lowercase(s):
100 all the variables in the original function's scope are still alive -- because 101 all the variables in the original function's scope are still alive -- because
101 the new function still has access to them. This saved scope is called a 102 the new function still has access to them. This saved scope is called a
102 closure. 103 closure.
103 104
104 An example use of a closure is a "private" variable that can be only accessed 105 An example use of a closure is a "private" variable that can be only accessed
105 by *get* and *set* functions. 106 by ``get`` and ``set`` functions.
106 107
107 .. code-block:: python 108 .. code-block:: python
108 109
109 def create_get_set(): 110 def create_get_set():
110 """ 111 """
120 def set(new_value): 121 def set(new_value):
121 value = new_value 122 value = new_value
122 123
123 return get, set 124 return get, set
124 125
125 The *value* variable stays in the memory even after *create_get_set* finishes, 126 The ``value`` variable stays in the memory even after ``create_get_set`` finishes,
126 because the *get* and *set* functions still hold a reference to it. Because 127 because the ``get`` and ``set`` functions still hold a reference to it. Because
127 they are the only objects holding reference to it, the variable is effectively 128 they are the only objects holding reference to it, the variable is effectively
128 hidden from other code. You could have a similar effect by using an object: 129 hidden from other code. You could have a similar effect by using an object:
129 130
130 .. code-block:: python 131 .. code-block:: python
131 132
147 148
148 getset = GetSet() 149 getset = GetSet()
149 return getset.get, getset.set 150 return getset.get, getset.set
150 151
151 152
152 Here instead of a closure we have an explicit *getset* object. Again, this 153 Here instead of a closure we have an explicit ``getset`` object. Again, this
153 object stays in memory because the bound methods *get* and *set* hold a 154 object stays in memory because the bound methods ``get`` and ``set`` hold a
154 reference to it. This couldn't be done in JavaScript. 155 reference to it. This couldn't be done in JavaScript.
155 156
156 157
157 Star arguments 158 Star arguments
158 -------------- 159 --------------