You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c165064
Browse filesBrowse the repository at this point in the historyBrowse files
``a_class_method`` is defined in the ``Classy`` class (see above).
99
+
And it prints the class that it is called on. But despite being defined in ``Classy``, it gets the ``SubClassy`` class object as the first parameter (``cls``).
100
+
So a classmethod will "do the right thing" when used in a subclass.
105
101
106
102
Alternate Constructors
107
103
-----------------------
@@ -123,7 +119,6 @@ keys:
123
119
TypeError: cannot convert dictionary update sequence element #0 to a sequence
124
120
125
121
126
-
127
122
The stock constructor for a dictionary won't work this way. So the dict object
128
123
implements an alternate constructor that *can*.
129
124
@@ -144,8 +139,7 @@ implements an alternate constructor that *can*.
144
139
See also ``datetime.datetime.now()``, etc....
145
140
146
141
147
-
Properties, Static Methods and Class Methods are powerful features of Python's
148
-
OO model.
142
+
Properties, Static Methods and Class Methods are powerful features of Python's OO model.
149
143
150
144
They are implemented using an underlying structure called *descriptors*
Actually, dealing with numbers rather than bytes is big
86
86
87
-
-- but we take that for granted
87
+
-- but we take that for granted.
88
88
89
89
90
90
Mechanics
@@ -98,11 +98,11 @@ Py2 strings were simply sequences of bytes. When text was one per character tha
98
98
Py3 strings (or Unicode strings in py2) are sequences of "platonic characters".
99
99
100
100
It's almost one code point per character -- there are complications
101
-
with combined characters: accents, etc -- but we can mostly ignore those -- you will get far thiking of a code point as a character.
101
+
with combined characters: accents, etc -- but we can mostly ignore those -- you will get far thinking of a code point as a character.
102
102
103
103
Platonic characters cannot be written to disk or network!
104
104
105
-
(ANSI: one character == one byte -- so easy!)
105
+
(ANSI: one character == one byte -- it was so easy!)
106
106
107
107
108
108
Strings vs Unicode
@@ -132,7 +132,7 @@ And two ways to work with binary data:
132
132
py3 is more clear:
133
133
134
134
``str`` for text
135
-
``byte`` for binary data
135
+
``bytes`` for binary data
136
136
137
137
Unicode
138
138
--------
@@ -154,12 +154,12 @@ If you need to deal with the actual bytes for some reason, you may need to conve
154
154
155
155
And can get even more confusing with py2 strings being *both* text and bytes!
156
156
157
-
This is actually one of the biggest differences between Python 2 and Python 3. As an ordinary user (particularly one that used English...), you may not notice -- text is text, and things generally "just work", but under the hood it is very different, and folks writing libraries for things like Internet protocols struggle with the differences.
157
+
This is actually one of the biggest differences between Python 2 and Python 3. As an ordinary user (particularly one that used English...), you may not notice -- text is text, and things generally "just work", but under the hood it is very different, and folks writting libraries for things like Internet protocols struggled with the differences.
158
158
159
159
Using Unicode in Py2
160
-
---------------------
160
+
--------------------
161
161
162
-
If you do need to write Python2 code, you really should use Unicode.
162
+
If you do need to write Python2 code, you really should use Unicode. If you don't -- skip ahead to "Unicode Literals".
163
163
164
164
Here are the basics:
165
165
@@ -214,7 +214,9 @@ Encoding and Decoding
214
214
215
215
216
216
Unicode Literals
217
-
------------------
217
+
----------------
218
+
219
+
How do you get text that isn't plain English text?
218
220
219
221
1) Use Unicode in your source files:
220
222
@@ -226,6 +228,8 @@ Unicode Literals
226
228
227
229
2) Escape the Unicode characters:
228
230
231
+
Either by its hexadecimal value, or its name:
232
+
229
233
.. code-block:: python
230
234
231
235
printu"The integral sign: \u222B"
@@ -239,7 +243,7 @@ One example: http://inamidst.com/stuff/unidata/
239
243
240
244
241
245
Using Unicode
242
-
--------------
246
+
-------------
243
247
244
248
Use ``unicode`` objects in all your code
245
249
@@ -251,14 +255,17 @@ Many packages do this for you: *XML processing, databases, ...*
251
255
252
256
**Gotcha:**
253
257
254
-
Python has a default encoding (usually ascii)
258
+
Python has a default encoding. On Mac and Unix systems, it's usually utf-8 -- Windows? not nearly as consistent.
255
259
256
260
.. code-block:: ipython
257
261
258
-
In [2]: sys.getdefaultencoding()
259
-
Out[2]: 'ascii'
262
+
In [7]: sys.getdefaultencoding()
263
+
Out[7]: 'utf-8'
264
+
265
+
Try this on your machine, and see what you get.
260
266
261
-
The default encoding will get used in unexpected places!
267
+
The default encoding will get used in unexpected places!
268
+
Notably in text files by default.
262
269
263
270
Using Unicode Everywhere
264
271
-------------------------
@@ -329,12 +336,13 @@ UTF-16
329
336
330
337
Kind of like UTF-8, except it uses at least 16bits (2 bytes) for each character: NOT ASCII compatible.
331
338
332
-
But is still needs more than two bytes for some code points, so you still can't process it as two bytes per character.
339
+
But it still needs more than two bytes for some code points, so you still can't simply process it as two bytes per character.
333
340
334
-
In C/C++ held in a "wide char" or "wide string".
341
+
In C/C++, it is held in a "wide char" or "wide string".
335
342
336
343
MS Windows uses UTF-16, as does (I think) Java.
337
344
345
+
338
346
UTF-16 criticism
339
347
-----------------
340
348
@@ -362,7 +370,7 @@ A 1-byte per char encoding.
362
370
363
371
* The most common one-byte per char encoding for European text.
364
372
365
-
* Nice property -- every byte value from 0 to 255 is a valid character ( at least in Python )
373
+
* Nice property -- every byte value from 1 to 255 is a valid character ( at least in Python )
366
374
367
375
* You will never get an UnicodeDecodeError if you try to decode arbitrary bytes with latin-1.
(by the way, the recent implementations are very efficient...)
457
463
458
464
So you can pretty much ignore encodings and all that for most basic text processing.
459
-
If you do find yourself needing to deal with binary data, you ay need to encode/decode stuff yourself. IN which case, Python provides an ``.encode()`` method on strings that encode the string to a bytes object with the encoding you select:
465
+
If you do find yourself needing to deal with binary data, you ay need to encode/decode stuff yourself.
466
+
In which case, Python provides an ``.encode()`` method on strings that encode the string to a bytes object with the encoding you select:
460
467
461
468
.. code-block:: ipython
462
469
@@ -477,7 +484,7 @@ It's all quite simple an robust.
477
484
478
485
But there are a couple key points to remember:
479
486
480
-
* The primary people struggling were those that wrote (or wored with) libraries that had to deal with protocols that used both binary and text data in the same data stream.
487
+
* The primary people struggling were those that wrote (or worked with) libraries that had to deal with protocols that used both binary and text data in the same data stream.
481
488
482
489
* As of Python 3.4 or so, the python string object had grown the features it needed to support even those ugly binary+text use cases.
But what happens if you do a checkout with a commit, and no specific file?
252
252
@@ -266,7 +266,7 @@ It does what you might expect -- puts ALL the files back the way they were at th
266
266
267
267
HEAD is now at c03bb5b adding print_grid from class
268
268
269
-
So the files are set to the old state -- but now there is that note about "detached HEAD" -- this means that changes you make, even commits, will not effect the git repo. IF you want to start from here and make changes that will stick, you need to do what it says, and make a new branch. But what it DOESN'T tell you is how to simpel "re-attach" the HEAD. Turns out there is an easy way::
269
+
So the files are set to the old state -- but now there is that note about "detached HEAD" -- this means that changes you make, even commits, will not effect the git repo. If you want to start from here and make changes that will stick, you need to do what it says, and make a new branch. But what it DOESN'T tell you is how to simply "re-attach" the HEAD. Turns out there is an easy way::
270
270
271
271
$ git checkout -
272
272
Previous HEAD position was c03bb5b adding print_grid from class
@@ -277,7 +277,7 @@ the dash means "the branch or commit you were on before your last checkout comma
0 commit comments