├── LICENSE ├── README.md ├── chapter ├── 10_python_syntax.md ├── 11_python_assignment.md ├── 12_python_print.md ├── 13_python_if.md ├── 14_python_loop.md ├── 15_python_iterator.md ├── 16_python_doc.md ├── 17_python_function.md ├── 18_python_scope.md ├── 19_python_arguments.md ├── 1_python_type_introduction.md ├── 20_python_function_advanced.md ├── 21_python_module.md ├── 22_python_package.md ├── 23_python_module_advanced.md ├── 24_python_class.md ├── 25_python_namespace.md ├── 26_python_operator.md ├── 27_python_class_design_pattern.md ├── 28_python_class_advanced.md ├── 29_python_try_except.md ├── 2_python_number_type.md ├── 30_python_exception_type.md ├── 31_python_unicode_bytes.md ├── 32_python_attribute_management.md ├── 33_python_decorator.md ├── 34_python_metaclass.md ├── 35_python_execution.md ├── 3_python_set_type.md ├── 4_python_string_type.md ├── 5_python_list_type.md ├── 6_python_tuple_type.md ├── 7_python_collections.md ├── 8_python_dictionary.md └── 9_python_file.md └── imgs ├── picture_all.JPG ├── picture_notes.JPG ├── python_11_1.JPG ├── python_11_2.JPG ├── python_11_3.JPG ├── python_11_4.JPG ├── python_12_1.JPG ├── python_12_2.JPG ├── python_12_3.JPG ├── python_13_1.JPG ├── python_14_1.JPG ├── python_14_2.JPG ├── python_14_3.JPG ├── python_14_4.JPG ├── python_14_5.JPG ├── python_14_6.JPG ├── python_15_1.JPG ├── python_15_10.JPG ├── python_15_11.JPG ├── python_15_12.JPG ├── python_15_13.JPG ├── python_15_14.JPG ├── python_15_15.JPG ├── python_15_16.JPG ├── python_15_17.JPG ├── python_15_18.JPG ├── python_15_19.JPG ├── python_15_2.JPG ├── python_15_20.JPG ├── python_15_20_pre.JPG ├── python_15_21.JPG ├── python_15_22.JPG ├── python_15_23.JPG ├── python_15_3.JPG ├── python_15_4.JPG ├── python_15_5.JPG ├── python_15_6.JPG ├── python_15_7.JPG ├── python_15_8.JPG ├── python_15_9.JPG ├── python_16_1.JPG ├── python_16_2.JPG ├── python_17_1.JPG ├── python_17_2.JPG ├── python_17_3.JPG ├── python_18_1.JPG ├── python_18_2.JPG ├── python_18_3.JPG ├── python_18_4.JPG ├── python_18_5.JPG ├── python_18_6.JPG ├── python_18_7.JPG ├── python_18_8.JPG ├── python_18_9.JPG ├── python_19_1.JPG ├── python_19_10.JPG ├── python_19_2.JPG ├── python_19_3.JPG ├── python_19_4.JPG ├── python_19_5.JPG ├── python_19_6.JPG ├── python_19_7.JPG ├── python_19_8.JPG ├── python_19_9.JPG ├── python_1_1.JPG ├── python_1_2.JPG ├── python_1_3.JPG ├── python_1_4.JPG ├── python_1_5.JPG ├── python_20_1.JPG ├── python_20_2.JPG ├── python_20_3.JPG ├── python_20_4.JPG ├── python_21_1.JPG ├── python_21_2.JPG ├── python_21_3.JPG ├── python_21_4.JPG ├── python_21_4_cross_import.JPG ├── python_21_4_dict.JPG ├── python_21_5.JPG ├── python_21_6.JPG ├── python_21_7.JPG ├── python_21_8.JPG ├── python_21_9.JPG ├── python_22_1.JPG ├── python_22_2.JPG ├── python_22_3.JPG ├── python_22_4.JPG ├── python_22_5.JPG ├── python_23_1.JPG ├── python_23_2.JPG ├── python_23_3.JPG ├── python_23_4.JPG ├── python_24_1.JPG ├── python_24_10.JPG ├── python_24_2.JPG ├── python_24_3.JPG ├── python_24_4.JPG ├── python_24_5.JPG ├── python_24_6.JPG ├── python_24_7.JPG ├── python_24_8.JPG ├── python_24_9.JPG ├── python_25_1.JPG ├── python_26_1.JPG ├── python_26_10.JPG ├── python_26_11.JPG ├── python_26_12.JPG ├── python_26_13.JPG ├── python_26_14.JPG ├── python_26_15.JPG ├── python_26_16.JPG ├── python_26_17.JPG ├── python_26_18.JPG ├── python_26_19.JPG ├── python_26_2.JPG ├── python_26_20.JPG ├── python_26_3.JPG ├── python_26_4.JPG ├── python_26_5.JPG ├── python_26_6.JPG ├── python_26_7.JPG ├── python_26_8.JPG ├── python_26_9.JPG ├── python_27_1.JPG ├── python_27_2.JPG ├── python_27_3.JPG ├── python_27_4.JPG ├── python_27_5.JPG ├── python_27_6.JPG ├── python_27_7.JPG ├── python_28_1.JPG ├── python_28_10.JPG ├── python_28_11.JPG ├── python_28_12.JPG ├── python_28_13.JPG ├── python_28_14.JPG ├── python_28_2.JPG ├── python_28_3.JPG ├── python_28_4.JPG ├── python_28_5.JPG ├── python_28_6.JPG ├── python_28_7.JPG ├── python_28_8.JPG ├── python_28_9.JPG ├── python_29_1.JPG ├── python_29_10.JPG ├── python_29_2.JPG ├── python_29_3.JPG ├── python_29_4.JPG ├── python_29_5.JPG ├── python_29_6.JPG ├── python_29_7.JPG ├── python_29_8.JPG ├── python_29_9.JPG ├── python_2_1.JPG ├── python_2_10.JPG ├── python_2_11.JPG ├── python_2_2.JPG ├── python_2_3.JPG ├── python_2_4.JPG ├── python_2_5.JPG ├── python_2_6.JPG ├── python_2_7.JPG ├── python_2_8.JPG ├── python_2_9.JPG ├── python_30_1.JPG ├── python_30_2.JPG ├── python_30_3.JPG ├── python_30_4.JPG ├── python_31_1.JPG ├── python_31_10.JPG ├── python_31_11.JPG ├── python_31_12.JPG ├── python_31_2.JPG ├── python_31_3.JPG ├── python_31_4.JPG ├── python_31_5.JPG ├── python_31_6.JPG ├── python_31_7.JPG ├── python_31_8.JPG ├── python_31_9.JPG ├── python_32_1.JPG ├── python_32_2.JPG ├── python_32_3.JPG ├── python_32_4.JPG ├── python_32_5.JPG ├── python_32_6.JPG ├── python_32_7.JPG ├── python_33_1.JPG ├── python_33_10.JPG ├── python_33_2.JPG ├── python_33_3.JPG ├── python_33_4.JPG ├── python_33_5.JPG ├── python_33_6.JPG ├── python_33_7.JPG ├── python_33_8.JPG ├── python_33_9.JPG ├── python_34_1.JPG ├── python_34_2.JPG ├── python_34_3.JPG ├── python_34_4.JPG ├── python_34_5.JPG ├── python_3_1.JPG ├── python_3_2.JPG ├── python_3_3.JPG ├── python_3_4.JPG ├── python_3_5.JPG ├── python_3_6.JPG ├── python_3_7.JPG ├── python_4_1.JPG ├── python_4_10.JPG ├── python_4_11.JPG ├── python_4_12.JPG ├── python_4_13.JPG ├── python_4_14.JPG ├── python_4_15.JPG ├── python_4_2.JPG ├── python_4_3.JPG ├── python_4_4.JPG ├── python_4_5.JPG ├── python_4_6.JPG ├── python_4_7.JPG ├── python_4_8.JPG ├── python_4_9.JPG ├── python_5_1.JPG ├── python_5_2.JPG ├── python_5_3.JPG ├── python_5_4.JPG ├── python_5_5.JPG ├── python_5_6.JPG ├── python_5_7.JPG ├── python_5_8.JPG ├── python_5_9.JPG ├── python_6_1.JPG ├── python_7_1.JPG ├── python_7_2.JPG ├── python_7_3.JPG ├── python_7_4.JPG ├── python_7_5.JPG ├── python_7_6.JPG ├── python_7_7.JPG ├── python_8_1.JPG ├── python_8_2.JPG ├── python_8_3.JPG ├── python_8_4.JPG ├── python_8_5.JPG ├── python_9_1.JPG ├── python_9_2.JPG ├── python_9_3.JPG ├── python_9_4.JPG ├── python_9_5.JPG ├── python_9_6.JPG ├── python_9_7.JPG ├── python_9_8.JPG └── python_9_9.JPG /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python 学习笔记 2 | 7 | 本文档是《learning python》中文第四版的学习笔记,是由我个人做的纸质读书笔记而来。 8 | 9 | 另:本人搜集了个人笔记并整理成册,命名为《AI算法工程师手册》,详见:www.huaxiaozhuan.com 10 | 11 | >* 文档PDF版本可能会滞后于项目进度 12 | >* 纸质笔记可以从百度网盘下载:http://pan.baidu.com/s/1miEkaBu 13 | >* 个人所作的所有笔记也可以从百度网盘下载:http://pan.baidu.com/s/1boSzlx1 14 | 15 | ![文档截图](imgs/picture_all.JPG) 16 | 17 | # 目标 18 | 这份笔记的目标是为了给出一份比较精炼,但是又要浅显易懂的Python教程。《Python学习手册》中文第四版虽然比较简单,但是措辞比较罗嗦,而且一个语法点往往散落在多个章节,不方便读者总结。 19 | 20 | 我在做笔记时,将一个知识点的内容都统筹在一个章节里面,因此提炼性大大提高。而且还有《Python学习手册》中文第四版的翻译在某些章节(可能难度较大?)措辞可能前后矛盾。当知识点提炼之后就能够很快的找到一些难以理解的概念的上下文,方便吃透这些难点。 21 | 22 | # 内容介绍 23 | 本文档一共35个章节。分别为: 24 | 25 | 1. Python的动态类型介绍 26 | 2. 数字类型 27 | 3. Set类型 28 | 4. 字符串 29 | 5. 列表 30 | 6. 元组 31 | 7. 通用序列 32 | 8. 字典 33 | 9. 文件 34 | 10. 语句和语法 35 | 11. 赋值语句 36 | 12. 打印 37 | 13. if语句 38 | 14. while/for循环 39 | 15. 迭代器和生成器 40 | 16. 文档 41 | 17. 函数 42 | 18. 作用域 43 | 19. 参数 44 | 20. 函数的高级特性 45 | 21. 模块 46 | 22. 包 47 | 23. 模块的高级用法 48 | 24. 类class 49 | 25. Python名字空间 50 | 26. 运算符重载 51 | 27. 类的设计模式 52 | 28. 类的高级主题 53 | 29. 异常机制 54 | 30. 异常对象 55 | 32. Unicode与字节串 56 | 32. 管理属性 57 | 33. 装饰器 58 | 34. 元类 59 | 35. Python的执行 60 | 61 | # 基础知识 62 | 看这份笔记最好有面向对象的概念。因为这份文档里大量使用了面向对象的术语,比如”实例对象“、”类对象“、”名字空间“等等。如果没有面向对象的概念,将会对理解后面的大部分章节有困难。 63 | 64 | 对于Python新手,这份文档难度不大。但是如果您对Python有一定的熟练程度,您会发现这份文档行云流水,一气呵成,阅读起来就没有停顿感。如果您对于Python完全陌生,那么本文档阅读过程中,对于一些复杂的概念建议您动手练手而不要完全依赖于文档中的示例。 65 | 66 | # 补充 67 | 68 | * 本文档所有的Python代码都是基于Python 3.5 64bit,在Python IDLE上运行的效果。操作系统为 Win7 64bit Professional 69 | 70 | * 本文档由` markdown` 语法制作而成,其中使用到了扩展的非标准`markdown`语法(流程图功能)。本人制作了一份`markdown`教程,项目地址在:https://github.com/huaxz1986/markdown_documentation_chinese .你也可以在百度网盘下载PDF版本:http://pan.baidu.com/s/1qWXaJRA 71 | * 本文档编辑器使用`cutemarked`软件。它是一款开源的`markdown`文件编辑器,支持流程图扩展与`LATEX`数学公式扩展。本人fork了一个版本,然后改装成了一个本地化版本,项目地址: https://github.com/huaxz1986/CuteMarkEd 。你也可以在百度网盘下载我编译打包好的版本(解压缩直接双击运行即可),地址:http://pan.baidu.com/s/1dEqupZ7 ,运行环境为 Win7 64bit Professional 72 | >`cutemarked`原版需要联网下载所需要的脚本文件,但是由于国内的网络问题,以及存在断网情况下使用等特殊需求 73 | >,我将它改造成了一个无需联网的本地化版本 74 | 75 | **本文档仅用于个人学习目的,未经许可不得用于商业目的,转载请注明出处** 76 | 77 | **email: huaxz1986@163.com** 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /chapter/10_python_syntax.md: -------------------------------------------------------------------------------- 1 | 6 | # 语句和语法 7 | 1.所有Python的复合语句,都是以冒号`:`结尾,下一行缩进开始进入代码块。同一个级别代码块的缩进形式相同 8 | 9 | 2.测试(`if`|`while`)中的一对圆括号`()`是可选的 10 | 11 | 3.可以省略行尾的分号`;` 12 | 13 | 4.代码块的范围由缩进来决定。同一个块中,所有语句向右缩进相同的距离(块内语句垂直左对齐)。 14 | 15 | * 缩进可以用空格或者制表符。不应该在同一段代码中混合使用制表符和空格 16 | * 若缩进出现不一致,则会导致语法错误 17 | 18 | 5.若将多行语句列入一行,则必须用分号隔离`;` 19 | 20 | 6.任何在括号中的多行语句都视为一行。括号包括圆括号`()`,中括号`[]`,大括号`{}`。也可以用反斜线`\`转义换行符来跨多行 21 | 22 | 7.当复合语句不包含任何复合从句时,复合语句的主体可以出现在Python首行冒号之后: 23 | `if x>y : print(x)` 24 | 25 | 8.变量名由:下划线或字母开头,后面接任意字母、数字、下划线 26 | 27 | * 以单下划线开头的变量名不会被`from module import *`语句导入,如变量名`_x` 28 | * 前后双下划线的变量名是系统预定义的,对解析器有着特殊的意义,如变量名`__x__` 29 | * 仅前面有双下划线的变量名视为类的本地变量,如变量名`__x` 30 | 31 | 9.表达式可以作为语句,但是其结果不会存储。因此只有当表达式工作产生副作用时,这种用法才有意义。 32 | 33 | * 表达式可以作为语句,但是语句不能作为表达式。如 Python中不支持(C和C++可以,因为在C/C++中赋值表达式产生左值): 34 | 35 | ``` 36 | if( a=file.read() ): 37 | pass 38 | ``` 39 | * 列表的原地修改表达式返回`None`对象,因此以下的意图不正确:`L=L.append('a')`,会导致`L`为`None`。 40 | 41 | 10.Python的语句是逐条运行的,除非遇上控制流语句。 42 | 43 | * 块|语句的边界是自动检测的。缩进定义了块边界,换行定义了语句边界 44 | * 首行+`:`+缩进语句定义了复合语句 45 | * 空白行、空格、注释通常被解释器忽略 46 | * 文档字符串会被解释器忽略,但是会保存它并由工具显示 47 | 48 | 11.Python顶层程序代码必须不能有缩进。缩进发生在复合语句的字块中。 49 | 50 | -------------------------------------------------------------------------------- /chapter/11_python_assignment.md: -------------------------------------------------------------------------------- 1 | 6 | # 赋值语句 7 | 1.赋值的左侧可以为变量名或者对象元素,右侧为任何表达式 8 | 9 | * 赋值总是建立对象的引用值,而不是复制对象 10 | * 变量名会在首次被赋值时创建。此后,每当这个变量名出现在表达式中时,会被它引用的对象所替代 11 | * 变量名必须先赋值后引用,否则报错 12 | * 模块导入、函数定义、类定义、`for`循环、函数参数传递 等过程都会触发隐式赋值,原理、规则同显式赋值 13 | 14 | 2.赋值语句的形式: 15 | 16 | * 基本形式: `x='abcd'` 17 | * 元组赋值: `a,b="ab","cd"`,按照位置一一对应赋值 18 | * 列表赋值: `[a,b]=["ab","cd"]`,按照位置一一对应赋值 19 | * 扩展的序列解包赋值: `a,*b="abcd"`,结果是 `b`等于`['b','c','d']` 20 | * 多目标赋值: `a=b="abcd"` 21 | >注意此时`a`和`b`都引用同一个对象。如果这个对象是个可变对象,则使用`a`或者`b` 22 | 对它进行原地修改可能导致陷阱 23 | * 增强赋值: `a+='ef'`,等价于`a=a+`ef` 24 | >若`a`指向的是可变对象,则`+=`很有可能是原地操作 25 | > 所有的二元表达式运算符均有增强赋值语句 26 | ![赋值语句](../imgs/python_11_1.JPG) 27 | 28 | 3.Python3中,元组和列表赋值统一化为序列赋值:`a,b=['ab','cd']`,左侧为任何类型的变量名序列,右侧为任何类型的值序列,只需要变量名序列和值序列长度相等。赋值时根据位置一一对应赋值。 29 | 30 | * 若变量名序列和值序列长度不等,则抛出`ValueError`异常 31 | * 支持嵌套的赋值序列,Python会自动根据实际情况分解成其组成部分,然后递归赋值。要求左侧变量名序列的嵌套形状必须符合右侧值序列的嵌套形状。 32 | ![序列赋值和嵌套序列赋值](../imgs/python_11_2.JPG) 33 | 34 | 4.扩展的序列解包赋值:收集右侧值序列中未赋值的项为一个列表,将该列表赋值给带星号`*`的变量 35 | 36 | * 左边的变量名序列长度不需要与值序列的长度相等,其中只能有一个变量名带星号`*` 37 | * 若带星号`*`变量名只匹配一项,则也是产生一个列表,列表中只有一个元素,如`a,*b="12"`,`b`为`[2]` 38 | * 若带星号`*`变量名没有匹配项,则也是产生空列表,如`a,*b="1"`,`b`为`[]` 39 | * 带星号`*`的变量名可以出现在变量名序列中的任何位置如`*a,b="1234"`,`a`为`[1,2,3]` 40 | * 匹配过程优先考虑不带星号的变量名,剩下的才匹配带星号的变量名 41 | * 以下情况会引发错误: 42 | * 左侧变量名序列有两个星号,如`*a,*b="abcd"` 43 | * 左侧变量名序列无星号但是左右长度不匹配,如`a,b="abcd"` 44 | * 左侧变量名序列星号的名称不在序列中,如`*a='abcd'` 45 | * 可以用手动分片来模拟扩展赋值行为 46 | ![扩展的序列解包赋值](../imgs/python_11_3.JPG) 47 | 48 | 5.增强赋值的优点: 49 | 50 | * `X+=Y`中,`X`可以是复杂的对象表达式,只需要求值一次。而`X=X+Y`中,要对`X`求值两次 51 | * 对支持原地修改的对象而言,增强形式的赋值会自动执行原地修改的预算: 52 | * `L=L+[1,2]`会生成新对象 53 | * `L+[1,2]`会执行原地修改 54 | ![增强赋值](../imgs/python_11_4.JPG) 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /chapter/12_python_print.md: -------------------------------------------------------------------------------- 1 | 6 | # 打印 7 | 1.打印`print`类似于文件`.write()`方法,它将默认地把对象打印到`stdout`流中。它会自动添加一些自动化的格式。 8 | > 和文件的`.write()`方法不同的是,`print`不需要将对象转换为字符串 9 | 10 | 2.在Python3中,`print`是一个内置函数,用关键字参数表示特定模式,其语法格式为: 11 | `print(obj1,obj2,sep=' ',end='\n',file=sys.stdout)`,返回值为`None` 12 | 13 | * 参数意义依次为: 14 | * 待打印对象作为位置参数依次给出 15 | * `sep`关键字参数指定分隔符,默认为空格 16 | * `end`关键字参数指定结尾字符串,默认为换行符 17 | * `file`指定输出位置,默认为标准输出文件,它必须是一个写打开的文件对象 18 | * 每个被打印的对象依次自动通过内置的`str()`函数取得其文本表示 19 | * 当没有打印对象时,`print()`会把一个换行符(或者由`end`指定的其他字符串)打印到标准输出流中(或者由`file`指定的文件中) 20 | * 关键字参数可以以任何顺序出现,但必须在位置参数之后 21 | * 如果想指定对齐或者位宽,则可以先构建格式化表达式来生成字符串,然后在`print`这个字符串 22 | ![print示例](../imgs/python_12_1.JPG) 23 | 24 | >Python2中,`print`是语句,有自己的特定语法:`print x,y`,如果想指定结尾字符串(默认为换行),则用`print x,y, '\t'` 25 | 26 | 3.`print`实际上是向文件对象中写文本字符串,因此对于字符串常量的一对引号实际上是不输出的,它只是输出字符串的内容。而交互式命令中,为了显示指定字符串,输出中带有一对引号。 27 | ![print与交互式显示区别](../imgs/python_12_2.JPG) 28 | 29 | 4.你也可以用`sys.stdout.write(str(x)+' '+str(y)+'\n')`代替`print(x,y)` 30 | 31 | 5.`file`关键字指定的对象可以是文件对象,也可以是拥有`.write()`方法的其他对象 32 | 33 | 6.指定了默认的`file`关键字参数时,也可以输出重定向,如 34 | 35 | ``` 36 | temp=sys.stdout #保存旧值 37 | sys.stdout=open('test.txt','a') #重新对stdout赋值,该文件对象必须写打开 38 | print(obj1,obj2) 39 | sys.stdout.close() #此句可以不要,此时文件对象自动回收,文件自动关闭 40 | sys.stdout=temp #恢复旧值 41 | ``` 42 | 7.在Python3中,可以通过`from __future __import print_function`使用Python3中的`print()`函数 43 | 44 | 8.Python3的`input()`直接把输入的文本作为一个字符串返回,不会求值 45 | (输入什么,字符串中就是什么)。 46 | >Python2中的`input()`会对字符串求职,就像他们是输入到一个脚本的程序代码一样 47 | 48 | ![input函数](../imgs/python_12_3.JPG) 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /chapter/13_python_if.md: -------------------------------------------------------------------------------- 1 | 6 | # `if`语句 7 | 1.`if`语句的的通用格式: 8 | 9 | ``` 10 | if test_expr1: #必选 11 | statement1#必选 12 | elif test_expr2: #可选 13 | statement2 14 | else: #可选 15 | statement3 16 | ``` 17 | 18 | * 注意`if`、`elif`、`else`的缩进一致 19 | * 除了开头的`if`以及关联的子句外,`elif`、`else`均可选 20 | 21 | 2.Python中多路分支必须写成一系列的`if/elif`测试,因为Python中没有`switch-case`语句 22 | >字典也可以执行多路分支的逻辑,如: 23 | > 24 | >``` 25 | > mydict={'a':func1,'b':func2} 26 | > choice='a' 27 | > print(mydict[choice]) 28 | >``` 29 | 30 | 3.Python中的真值测试: 31 | 32 | * 任何非0数字或者非空对象为`True`,数字0、空对象(如空列表,空字典、空元组、空`set`、空字符串)、`None`对象为`False` 33 | * 比较、相等测试会递归地应用在嵌套的数据结构中,他们返回`True`或`False` 34 | * 布尔`and`和`or`运算符会返回真或假的操作对象,而不是`True`或`Flase`,并且它们是短路计算 35 | * `and`:从左到右依次对操作对象求值,停在第一个为假的对象上并返回它,或者当前面所有操作对象为真时返回最后一个操作对象 36 | * `or`:从左到右依次对操作对象求值,停在第一个为真的对象上并返回它,或者当前面所有操作对象为假时返回最后一个操作对象 37 | 38 | ![Python中的真假测试](../imgs/python_13_1.JPG) 39 | 40 | 4.Python支持`if|else`三元表达式:`Y if X else Z`: 41 | 当`X`为真时,表达式的值为`Y`;当`X`为假时,表达式的值为`Z`。 42 | 注意这里为短路计算,并不会同时对`Y`和`Z`求值。 43 | 44 | * 三元表达式`X? Y:Z`也能得到同样的效果 45 | * 还有一种模拟方法:`[Z,Y][bool(X)]`。但是它会同时对`Z`、`Y`求值,可能会有副作用。 46 | > `bool(X)`将`X`转换成对应的1或者0 47 | 48 | 49 | -------------------------------------------------------------------------------- /chapter/14_python_loop.md: -------------------------------------------------------------------------------- 1 | 6 | # `while`、`for`循环 7 | 1.`while`语句格式: 8 | 9 | ``` 10 | while test_expr: 11 | statement1 12 | else:#可选 13 | statement2 14 | ``` 15 | * `while`和`else`缩进必须一致。 16 | * `else`可选。`else`子句在控制权离开循环且未碰到`break`语句时执行。即在正常离开循环时执行(`break`是非正常离开循环) 17 | * 在`while`子句中可以使用下列语句: 18 | * `break`:跳出最近所在的循环到循环外部 19 | * `continute`:跳过本次循环后续部分,直接掉转到下一轮循环起始处 20 | * `pass`:占位符,什么都不做 21 | >在Python3中,允许在使用表达式的地方使用`...`代表, 22 | >这是`pass`语句的一种替代方案。它表示代码随后填充,是未确定的内容 23 | 24 | ![while语句](../imgs/python_14_1.JPG) 25 | 26 | 2.`for`语句格式: 27 | 28 | ``` 29 | for target_var in iter_obj: 30 | statement1 31 | else:#可选 32 | statement2 33 | ``` 34 | * `for`和`else`缩进必须一致。 35 | * `else`可选。`else`子句在控制权离开循环且未碰到`break`语句时执行。即在正常离开循环时执行(`break`是非正常离开循环) 36 | * 在`for`子句中可以使用`break`、`continute`、`pass`语句 37 | * `target_var`是赋值目标,`iter_obj`是任何可迭代对象。每一轮迭代时将迭代获得的值赋值给`target_var`,然后执行`statement1` 38 | >任何赋值目标在语法上均有效,即使是嵌套的结构也能自动解包: 39 | > 40 | ``` 41 | for ((a,b),c) in [((1,2),3),((4,5),6)]:#自动解包 42 | print(a,b,c) 43 | ``` 44 | >当然你也可以手动解包: 45 | > 46 | ``` 47 | for both in [((1,2),3),((4,5),6)]:#手动解包 48 | ((a,b),c)=both 49 | print(a,b,c) 50 | ``` 51 | 52 | ![for语句](../imgs/python_14_2.JPG) 53 | 54 | 3.`for`扫描文件时,直接使用文件对象迭代,每次迭代时读取一行,执行速度快,占用内存少: 55 | 56 | ``` 57 | for line in open('test.txt'): 58 | print(line) 59 | ``` 60 | 61 | 4.`for`语句通常比对应的`while`语句执行速度快 62 | 63 | 5.Python3中,`range()`返回一个迭代器对象。用法为:`range(0,10,2)`,其中`0`为起始数,`10`为终止数(不包含),`2`为步长。默认步长为1,起始为0. 64 | >Python2中,`range()`返回一个列表对象 65 | 66 | * 要得到列表,用`list(range(0,10,2))` 67 | * 通常`range()`用于`for`循环: 68 | 69 | ``` 70 | S="abcdefg" 71 | for i in range(0,len(S),2): 72 | print(S[i],end=" ") 73 | ``` 74 | 它也等价于下列循环: 75 | 76 | ``` 77 | S="abcdefg" 78 | for c in S[::2] 79 | print(c,end=" ") 80 | ``` 81 | 用range()优点是它并未复制字符串 82 | * 当我们遍历列表且对其进行修改时,要用到`range()`和`len()`组合。直接用`for`遍历列表不能修改列表,因为`for x in L:`遍历的是列表元素,不是列表中元素的位置。 83 | ![range()函数](../imgs/python_14_3.JPG) 84 | 85 | 6.Python3中,`zip()`函数返回一个迭代器对象。 86 | >Python2中,`zip()`返回一个元组对的列表 87 | 88 | * `list(zip(L1,L2))`创建一个列表,列表元素为元组对,元组对的第一个元素来自于`L1`,第二个元素来自于`L2`,列表长度为`L1`与`L2`的最小值 89 | * `zip()`可以有两个以上的参数,且这些参数可以是任意的可迭代对象 90 | * 可以在循环中用自动列表解包: 91 | 92 | ``` 93 | for x,y,z in zip(iter_obj1,iter_obj2,iter_obj3): 94 | pass 95 | ``` 96 | ![zip()函数](../imgs/python_14_4.JPG) 97 | 98 | 7.Python3中,`map()`函数生成一个可迭代对象,用法为:`map(func,iter_obj)`。每一次迭代则在迭代得到的元素上应用函数`func`。 99 | >Python2中,`map()`执行的是另一种语意 100 | 101 | ![map()函数](../imgs/python_14_5.JPG) 102 | 103 | 8.`enumerate()`函数生成一个可迭代对象,用法为:`enumerate(iter_obj)`。每一次迭代生成一个`(index,value)`元组,`index`表示迭代次数,从0开始计数,`value`代表迭代获得的元素值。 104 | 105 | ![enumerate()函数](../imgs/python_14_6.JPG) 106 | 107 | 108 | -------------------------------------------------------------------------------- /chapter/15_python_iterator.md: -------------------------------------------------------------------------------- 1 | 6 | # 迭代器和生成器 7 | 1.可迭代对象:在逻辑上它保存了一个序列,在迭代环境中依次返回序列中的一个元素值。 8 | >可迭代对象不一定是序列,但是序列一定是可迭代对象 9 | 10 | 2.迭代协议:`.__next__()`方法。 11 | 12 | * 任何对象只要实现了迭代协议,则它就是一个迭代器对象 13 | * 迭代器对象调用`.__next__()`方法,会得到下一个迭代结果 14 | * 在一系列迭代之后到达迭代器尾部,若再次调用`.__next__()`方法,则会触发`StopIteration`异常 15 | * 迭代器在Python中是用C语言的速度运行的,因此速度最快 16 | ![迭代器](../imgs/python_15_1.JPG) 17 | 18 | 3.Python3提供了一个内置的`next()`函数,它自动调用迭代器的`.__next__()`方法。即给定一个迭代器对象`x`,`next(x)`等同于`x.__next__()` 19 | ![next()函数](../imgs/python_15_2.JPG) 20 | 21 | 4.内置的`iter()`函数用于从序列、字典、`set`以及其他可迭代对象中获取迭代器。 22 | 23 | * 对任何迭代器对象`iterator`,调用`iter(iterator)`返回它本身 24 | * 迭代器对象实现了迭代协议 25 | * 文件对象本身是一个迭代器对象。即文件对象实现了迭代协议,因此打开多个文件会返回同一个文件对象 26 | * 列表、元组、字典、`set`、字符串等不适迭代器对象,他们没有实现迭代协议。因此每次调用`iter()`均返回一个新迭代器对象。他们支持安装多个迭代器,每个迭代器状态不同 27 | * 在原地修改列表、`set`、字典时,会实时反映到它们的迭代器上 28 | 29 | ![iter()](../imgs/python_15_3.JPG) 30 | 31 | 5.文件迭代器:文件对象本身是一个迭代器(这里文件对象要是读打开)。它的`.__next__()`方法每次调用时,返回文件中的下一行。当到达文件末尾时,`.__next__()`方法会引发`StopIteration`异常。 32 | >`.readline()`方法在到达文件末尾时返回空字符串 33 | 34 | ![文件迭代器](../imgs/python_15_4.JPG) 35 | 36 | 6.字典的迭代器:字典的迭代器在迭代环境中,每次迭代返回的是一个键。 37 | ![字典的迭代器](../imgs/python_15_5.JPG) 38 | 39 | 7.扩展的列表解析表达式: 40 | `[ x+y for x in 'abc' if x!='a' for y in 'lmn' if y!='l']` 41 | 其通用结构为: 42 | 43 | ``` 44 | [ expression for target1 in iterable1 [if condition1] 45 | for target2 in iterable2 [if condition2] 46 | ....] 47 | ``` 48 | >我们总是可以用`for`循环手动构建列表解析表达式的结果,但是列表解析表达式性能更好 49 | 50 | ![扩展的列表解析](../imgs/python_15_6.JPG) 51 | 52 | 8.`for`循环、列表解析、`in`成员关系测试、`map()`内置函数、`sorted()`内置函数、`zip()`内置函数等都是用迭代协议来完成工作 53 | 54 | 9.常见的迭代函数: 55 | 56 | * `map(func,iterable)`:它将函数`func`应用于传入的迭代器的每个迭代返回元素,返回一个新的迭代器,函数执行结果作为新迭代器的迭代值 57 | > `map()`可以用于多个可迭代对象:`map(func,[1,2,3],[2,3,4])`,其中`func(first,second)` 的两个参数分别从两个可迭代对象中获取,函数结果作为新迭代器的迭代值 58 | 59 | ![map函数](../imgs/python_15_7.JPG) 60 | * `zip(iterable1,iterable2,...)`:它组合可迭代对象`iterable1`、`iterable2`、`...`中的各项,返回一个新的迭代器。新迭代器长度由`iterable1`、`iterable2`、`...`最短的那个决定。 61 | ![zip函数](../imgs/python_15_8.JPG) 62 | * `enumerate(iterable,start)`:返回一个迭代器对象,它迭代结果是每次迭代返回一个`(index,value)`元组 63 | ![enumerate函数](../imgs/python_15_9.JPG) 64 | * `filter(func,iterable)`:返回一个迭代器对象,它的迭代结果得到`iterable`中部分元素,其中这些元素使得`func()`函数返回为真 65 | ![filter函数](../imgs/python_15_10.JPG) 66 | * `reduce(func,iterable,initial)`:对`iterable`中每一项成对地运行`func`,返回最终值 67 | > `reduce`函数位于`functools`包内 68 | 69 | ![reduce函数](../imgs/python_15_11.JPG) 70 | 71 | * `sorted(iterable,key=None,reverse=False)`:排序并返回排好的新列表 72 | ![sorted函数](../imgs/python_15_12.JPG) 73 | 74 | * `sum(iterable,start)`:返回可迭代对象中的累加值 75 | ![sum函数](../imgs/python_15_13.JPG) 76 | 77 | * `any(iterable)`:只要可迭代对象`iterable`迭代返回的某个元素为真则返回`True` 78 | * `all(iterable)`:只有可迭代对象`iterable`迭代返回的所有元素为真则返回`True` 79 | ![any函数和all函数](../imgs/python_15_14.JPG) 80 | 81 | * `max(iterable,key=func)`:返回最大元素。若指定`func`,则返回是`func(num)`最大的那个元素 82 | * `min(iterable,key=func)`:返回最小元素。若指定`func`,则返回是`func(num)`最小的那个元素 83 | ![max函数和min函数](../imgs/python_15_15.JPG) 84 | 85 | 10.`set`解析、字典解析支持列表解析的扩展语法 86 | ![扩展解析语法](../imgs/python_15_16.JPG) 87 | 88 | 11.Python3中,`range`对象不支持`.__next__()`,因此它本身不是迭代器,而`map`、`zip`、`filter`对象都是迭代器。 89 | 90 | * `range`不直接生成列表的优点是节约内存空间 91 | * 由于`map`、`zip`、`filter`对象都是迭代器,因此不支持在它们身上安装多个活跃的迭代器。对他们调用`iter()`其实返回的是它们本身。 92 | ![range对象](../imgs/python_15_17.JPG) 93 | 94 | 12.字典的视图:键视图、值视图、字典视图都没有`.__next__()`方法,因此他们都不是迭代器 95 | ![字典的视图都不是迭代器](../imgs/python_15_18.JPG) 96 | 97 | 13.通常列表解析执行速度最快,`map()`速度次之,`for`循环速度最慢。前两者以C语言速度执行、后者在Python虚拟机上执行 98 | 99 | 14.生成器函数:编写为常规的`def`语句,但是用`yield`语句一次返回一个结果。每次使用生成器函数时会继续上一轮的状态。 100 | >生成器函数会保存上次执行的状态 101 | 102 | 定义生成器函数的语法为: 103 | 104 | ``` 105 | def genFunc(num): 106 | for i in range(num): 107 | yield i**2 108 | ``` 109 | 110 | * 生成器函数执行时,得到一个生成器对象,它`yield`一个值,而不是返回一个值。 111 | * 生成器对象自动实现迭代协议,它有一个`.__next__()`方法 112 | * 对生成器对象调用`.__next__()`方法会继续生成器函数的运行到下一个`yield` 113 | 结果或引发一个`StopIteration`异常 114 | * `yield`语句会挂起生成器函数并向调用者发送一个值。当下一轮继续时,函数会在上一个`yield`表达式返回后继续执行,其本地变量根据上一轮保持的状态继续使用 115 | * 生成器函数运行代码随时间产生一系列的值,而不是一次性计算它们。这会节约内存并允许计算时间分散。 116 | ![生成器函数](../imgs/python_15_19.JPG) 117 | 118 | 15.`for`循环(及其它迭代环境)通过重复调用`.__next__()`方法直到捕获一个异常。若一个不支持迭代协议的对象想用工作在这种环境中,`for`循环会尝试使用索引协议迭代。 119 | 120 | 16.生成器对象有一个`.send(arg)`方法。该方法会将`arg`参数发送给生成器作为`yield`表达式的返回值,同时生成器会触发生成动作(相当于调用了一次`.__next__()`方法。 121 | > `yield`表达式的返回值和生成值是不同的。 122 | > 返回值是用于生成器函数内部,`yield`表达式默认返回值为`None`; 123 | > 而生成值是用于生成器函数外部的迭代返回。 124 | 125 | * 生成器对象必须先启动。启动意味着它第一次运行到`yield`之前挂起 126 | ![启动生成器](../imgs/python_15_20_pre.JPG) 127 | * 要想启动生成器,可以直接使用`next(generatorable)`函数,也可以使用`generatorable.send(None)`方法,或者 128 | `generatorable.__next__()`方法 129 | >`next(generatorable)`函数相当于使用`generatorable.send(None)`方法 130 | * `generatorable.send(None)`方法会在传递`yield`表达式的值(默认为`None`返回值),下一轮迭代从`yield`表达式返回开始 131 | >每一轮挂起时,`yield`表达式 yield 一个数,但是并没有返回(挂起了该`yield`表达式) 132 | 133 | ![.send()方法](../imgs/python_15_20.JPG) 134 | 135 | 17.生成器表达式:类似于列表解析,但是它是在圆括号中的,而不是方括号中的。 136 | 137 | * 生成器表达式返回的是一个生成器对象 138 | * 列表解析的结果等同于`list()`内参数为一个生成器表达式 139 | * 当生成器表达式在其他括号之内时,它本身的圆括号可以不写 140 | * 同样的迭代可以用生成器函数或者一个生成器表达式 141 | * 生成器函数可以包含更多的逻辑 142 | * 生成器表达式更简洁,没有函数调用产生生成器的过程 143 | * 生成器函数与生成器表达式支持自动迭代与手动迭代 144 | * 生成器对象本身是迭代器,因此只支持一个活跃的迭代器。 145 | ![生成器表达式](../imgs/python_15_21.JPG) 146 | 147 | 18.生成器函数可以有`return`,它可以出现在函数内任何地方。生成器函数内遇到`return`则触发`StopIteration`异常,同时`return`的值作为异常说明 148 | ![生成器函数的return](../imgs/python_15_22.JPG) 149 | 150 | 19.可以调用生成器对象的`.close()`方法强制关闭它。这样再次给它`send()`任何信息,都会抛出`StopIteration`异常,表明没有什么可以生成的了 151 | ![生成器的close()方法](../imgs/python_15_23.JPG) 152 | 153 | 20.`yield from`:从`PEP 380`引入的新特性。 154 | 155 | * `yield from`可以将一个大的生成器切分成小生成器: 156 | 157 | ``` 158 | def generator(): #该生成器yield数字[0~19] 159 | for i in range(10): 160 | yield i 161 | for j in range(10,20): 162 | yield j 163 | ``` 164 | 如果你想切分成两个迭代器,可以这么做: 165 | 166 | ``` 167 | def generator2(): 168 | for i in range(10): 169 | yield i 170 | def generator3(): 171 | for j in range(10): 172 | yield j 173 | def generator(): 174 | for i in generator2(): 175 | yield i 176 | for j in generator3(): 177 | yield j 178 | ``` 179 | 引入`yield from`之后你可以这么做: 180 | 181 | ``` 182 | def generator2(): 183 | for i in range(10): 184 | yield i 185 | def generator3(): 186 | for j in range(10): 187 | yield j 188 | def generator(): 189 | yield from generator2() 190 | yield from generator3() 191 | ``` 192 | 193 | * `yield from`能实现代理生成器: 194 | 195 | ``` 196 | def generator(): 197 | inner_gen=generator2() 198 | yield from inner_gen #为了便于说明,这里分两行写 199 | gen=generator() 200 | ``` 201 | * 对`inner_gen`迭代产生的每个值都直接作为`gen` yield值 202 | * 所有`gen.send(val)`发送到`gen`的值`val`都会被直接传递给`inner_gen`。 203 | * `inner_gen`抛出异常: 204 | * 如果`inner_gen`产生了`StopIteration`异常, 205 | 则`gen`会继续执行`yield from`之后的语句 206 | * 如果对`inner_gen`产生了非`StopIteration`异常,则传导至`gen`中, 207 | 导致`gen`在执行`yield from`的时候抛出异常 208 | * `gen`抛出异常: 209 | * 如果`gen`产生了除`GeneratorExit`以外的异常,则该异常直接 throw 到`inner_gen`中 210 | * 如果`gen`产生了`GeneratorExit`异常,或者`gen`的`.close()`方法被调用, 211 | 则`inner_gen`的`.close()`方法被调用。 212 | * `gen`中`yield from`表达式求职结果是`inner_gen`迭代结束时抛出的`StopIteration`异常的第一个参数 213 | * `inner_gen`中的`return xxx`语句实际上会抛出一个`StopIteration(xxx)`异常, 214 | 所以`inner_gen`中的`return`值会成为`gen`中的`yield from`表达式的返回值。 -------------------------------------------------------------------------------- /chapter/16_python_doc.md: -------------------------------------------------------------------------------- 1 | 6 | # 文档 7 | 1.`#`是注释符,它之后的文字到行尾均为注释。Python解释器会忽略`#`之后的所有字符 8 | 9 | 2.`dir(obj)`可以返回对象内所有的属性列表。它能够调用任何有属性的对象 10 | > 也可以把类型名称传给`dir()`,可以得到相应的结果 11 | 12 | ![dir()函数](../imgs/python_16_1.JPG) 13 | 14 | 3.文档字符串:对象的`.__doc__`属性。 15 | 文档字符串是放在模块文件、函数、类定义的顶端,在任何可执行代码之前。Python会自动封装这个字符串,使其成为相应对象的`.__doc__`属性。 16 | 17 | * 模块文件、函数、类定义的多行注释一般用三重引号字符串 18 | 19 | 4.标准PyDoc工具用Python代码编写,它提取文档字符串并自动格式化其信息,展示成排列友好的报表。 20 | 21 | * Python在标准库中附带了PyDoc工具 22 | * 有两种最主要的PyDoc接口: 23 | * 内置的`help()`函数。可以对函数、方法、类型、对象使用`help()`函数。 24 | 对较大的对象(如模块、类型),`help()`输出的内容会分成几段。 25 | 报表中的信息有些是文档字符串,有些是PyDoc自动查看对象内部而收集的结构化信息。 26 | * PyDoc的 GUI|HTML 接口。启动 `Tools/scripts` 目录下的`pydoc3.py` 脚本: 27 | `pydoc3.py -b` 28 | ![PyDoc HTML接口](../imgs/python_16_2.JPG) 29 | 30 | 5.Python标准手册随Python发布。手册以HTML和其他格式实现。最重要的项目是: 31 | 32 | * Library Reference:标准库文档,说明了内置类型、函数、异常等等 33 | * Language Reference:官方给出的语言层次的细节 -------------------------------------------------------------------------------- /chapter/17_python_function.md: -------------------------------------------------------------------------------- 1 | 6 | # 函数 7 | 1.Python的函数是由`def`语句编写。Python中,函数也是一种对象类型 8 | 9 | * `def`是一个可执行语句。Python与行了`def`语句后函数对象才存在,之前该函数对象不存在 10 | * Python运行到`def`语句时,它将会生成一个新的函数对象,并将该函数对象赋值给这个函数名。函数名成了这个函数对象的引用 11 | * `lambda`表达式创建一个函数对象并返回它,但是它并没有绑定一个名字即它是匿名的没有函数名 12 | * `return`语句将一个结果对象发送给调用者 13 | * `yield`语句使得函数成为一个生成函数 14 | ![函数定义](../imgs/python_17_1.JPG) 15 | 16 | 2.函数参数没有任何类型约束,返回类型也没有任何类型约束 17 | 18 | 3.Python函数定义语句: 19 | 20 | ``` 21 | def func_name(arg1,arg2): 22 | statement 23 | ``` 24 | 25 | 在执行完`def`语句时,Python创建一个函数对象并将它赋值给`func_name`变量。 26 | 27 | * `return`语句是可选的。若无`return`,则默认自动返回`None`对象 28 | * `def`语句可以出现在任何语句可以出现的地方,甚至是嵌套在其他语句中。 29 | * 与C语言不同,Python函数在程序运行之前不需要全部定义。`def`在它定义的地方时才评估,而`def`的之内的代码在函数调用的时候才求值。 30 | ![函数定义与执行区别](../imgs/python_17_2.JPG) 31 | 32 | 4.函数仅仅是对象类型,函数名也仅仅是变量名,它们没有任何特殊之处。 33 | 34 | * 函数对象有函数调用方法`operator ()` 35 | * 函数对象允许任意的属性添加`func.attr=value` 36 | ![函数对象](../imgs/python_17_3.JPG) 37 | 38 | 5.函数主体内的代码直到函数被调用时才运行。函数内的变量名在函数实际执行之前都不会解析。 -------------------------------------------------------------------------------- /chapter/18_python_scope.md: -------------------------------------------------------------------------------- 1 | 6 | # 作用域 7 | 1.代码中变量名被赋值的位置决定了这个变量名的作用域(即可见范围)。因为变量是在首次赋值的时候定义的。 8 | 9 | * Python将一个变量名首次被赋值的地点关联为一个特定的命名空间 10 | 11 | 2.变量可以在3个不同的地方定义,对应三种不同的作用域: 12 | 13 | * `def`内赋值定义的变量:作用域为本函数 14 | >这里的赋值包括显式`=`赋值和隐式赋值。隐式赋值包括 15 | > 16 | >* `import`语句隐式赋值 17 | >* `def`函数定义来隐式赋值(函数名这个变量) 18 | >* 形参匹配来隐式赋值 19 | * 嵌套的`def`中赋值定义的变量:对于父函数来说,该变量不是本地的 20 | * `def`之外赋值,作用域为整个文件全局的 21 | ![三种作用域](../imgs/python_18_1.JPG) 22 | 23 | 3.作用域法则: 24 | 25 | * 每个模块文件就是一个全局作用域。从外面看,模块的全局变量就成为该模块对象的属性;从内部看,模块的全局变量就是普通的、作用域为全局的变量 26 | * 全局作用域的范围仅限于变量所在的单个文件 27 | * 每次调用函数,都创建一个新的局部作用域 28 | * 默认情况下,所有函数定义内部的变量都是局部变量 29 | * `global`语句声明会将变量名作用域提升至全局 30 | * `nonlocal`语句声明会将变量名作用域提升至外层的`def` 31 | * Python预定义的`_builtin_`模块提供了一些预定义的内置变量 32 | ![作用域法则](../imgs/python_18_2.JPG) 33 | 34 | 4.交互式运行的代码实际上位于`__main__`的内置模块中 35 | 5.变量名查找规则:`LGBE` 36 | 37 | * 首先查找本地作用域`L` 38 | * 接着查找上一层`def`或`lambda`的本地作用域`E` 39 | * 接着查找全局作用域`G` 40 | * 最后查找内置作用域`B` 41 | 42 | 如果均未找到变量名则报错。 43 | 当前面作用域的变量名覆盖内置的作用域名字时,可以手动`import builtins`模块,在用`builtins.name`来直接使用这个变量名。注意不要随意修改`builtins`模块内变量的值。 44 | 45 | ``` 46 | a=1 47 | b=2 48 | def func(): 49 | global a,b,c #一个名字或逗号分隔的多个名字,这些变量名被提升到全局作用域内 50 | a=2 51 | b=0 52 | c=0 #虽然c没有在def外定义,但这里的global 和 c=0会在全局作用域内定义c 53 | ``` 54 | ![global用法](../imgs/python_18_3.JPG) 55 | 56 | 7.要在局部作用域中修改全局变量,方法有以下几种: 57 | 58 | * global声明 59 | * 通过`import modname`然后利用`modname.attr`来访问 60 | * 通过`import sys`然后利用`sys.modules['modname'].attr`来访问 61 | ![global用法](../imgs/python_18_4.JPG) 62 | 63 | 8.作用域示例: 64 | 65 | ``` 66 | x='global_x' #全局作用域 67 | def f1(): 68 | x='f1_x' #f1的局部作用域 69 | z='f1_z' #f1的局部作用域 70 | def f2(): 71 | global y #全局作用域 72 | y='f2_y' 73 | print('in f2',x) #LGBE法则,找到的是f1局部作用域中的x 74 | nonlocal z #f2的nonlocal作用域,是f1的局部作用域 75 | z='f2_z' 76 | t='f2_t' #f2的本地作用域 77 | f2() 78 | ``` 79 | ![作用域示例](../imgs/python_18_5.JPG) 80 | 81 | 9.嵌套的函数返回后也是有效的。 82 | 83 | ``` 84 | def f1(): 85 | x=99 86 | def f2(): 87 | print(x) 88 | return f2 # f2是个局部变量,仅在f1执行期间因为新建了局部作用域所以才存在。 89 | action=f1() #f2指向的函数对象,由于action引用它,因此不会被收回。 90 | #但是f2这个位于局部作用域中的变量名被销毁了 91 | action() #此时f1执行结束,但是f2记住了在f1嵌套作用域中的x。这种行为叫闭包 92 | ``` 93 | >类比闭包在语义上更明确,因为类可以明确定义自己的状态 94 | 95 | ![闭包](../imgs/python_18_6.JPG) 96 | 97 | 10.在函数内部调用一个未定义的函数是可行的,只要在函数调用是,该未定义函数已定义即可。 98 | ![调用未定义函数](../imgs/python_18_7.JPG) 99 | 100 | 11.嵌套作用域中的变量在嵌套的函数调用时才进行查找,而不是定义时。 、 101 | 102 | ``` 103 | def func(): 104 | acts=[] 105 | for i in range(5): 106 | acts.append(lambda x:i**x) #添加匿名函数对象 107 | return acts 108 | acts=func() 109 | acts[0](2) #调用时才开始查找i,此时i最后被记住的值是4 110 | ``` 111 | ![嵌套作用域中变量名查找](../imgs/python_18_8.JPG) 112 | 113 | 要解决这个这个陷阱,可以用默认参数: 114 | 115 | ``` 116 | def func(): 117 | acts=[] 118 | for i in range(5): 119 | acts.append(lambda x,i=i:i**x) #每次循环,形参i的默认实参均不同 120 | return acts 121 | acts=func() 122 | acts[0](2) 123 | ``` 124 | 125 | 12.`nonlocal`是Python3中引入的。 126 | 127 | ``` 128 | def func(): 129 | a=0 130 | b=1 131 | def func1(): 132 | nonlocal a,b#限定了查找只能在func1所在的作用域(即func的本地作用域),且要求名字a,b已经存在。 133 | a='a' #对a,b的赋值会影响到func中的a,b 134 | ``` 135 | 136 | * 如果没有`nonlocal`,则在`func1`中的赋值会创建本地变量,而无法修改`func`中的局部变量 137 | * `global`使得作用域查找先从全局作用域开始,跳过了局部作用域以及`nonlocal`作用域。 138 | * Python在函数创建的时候解析`nonlocal`,而不是在函数调用时,因此要求`nonlocal`的名字已经存在 139 | 140 | 13.全局变量、`nonlocal`变量、类、函数属性都提供了状态保持能力 141 | 142 | 14.`global`、`nonlocal`只是修改了变量作用域(即作用域提升),但是并未给出变量定义 143 | ![global|nonlocal不是定义](../imgs/python_18_9.JPG) 144 | -------------------------------------------------------------------------------- /chapter/19_python_arguments.md: -------------------------------------------------------------------------------- 1 | 6 | # 参数 7 | 1.参数传递的性质: 8 | 9 | * 参数的传递是通过自动将对象赋值给本地变量名来实现的(自动的隐式赋值) 10 | >被传递的参数对象本身并不会被拷贝 11 | * 在函数内部的参数名赋值不会影响到实参对象,只是将变量名重新引用到另一个对象 12 | * 若实参对象为可变对象,则在函数内原地修改参数对象会影响所有的指向该实参对象的引用 13 | >可变参数实参对象对函数来说,既可以作为输入也可以作为输出 14 | > 15 | >如果想限制对可变实参对象的修改,可以创建一个可变对象的拷贝作为参数; 16 | >或者直接转换成不可变对象作为参数 17 | 18 | ![参数传递](../imgs/python_19_1.JPG) 19 | 20 | 2.默认情况下,参数匹配是通过其位置进行匹配的,从左到右一一匹配。必须精确传递和函数签名中参数名一样多的实参。 21 | 22 | 3.关键字参数:允许通过变量名进行匹配,而不是通过位置。其中关键字顺序可以任意。 23 | ![关键字参数](../imgs/python_19_2.JPG) 24 | 25 | 4.默认参数:函数定义时,可以为参数设定默认值,这样允许调用时传递较少的参数。 26 | >对于默认实参,可以不用给它传入实参 27 | 28 | * 默认实参后面不能跟随非默认实参。如果出现这种情况则报语法错误。 29 | ![默认参数](../imgs/python_19_3.JPG) 30 | 31 | 5.可变参数: 32 | 33 | * 函数能用特定的参数(以`*`开头),收集任意多的额外位置参数,将收集到的位置相关的参数到一个新元组中。 34 | >若出现了额外的关键字参数,则报错 35 | 36 | ![可变位置参数](../imgs/python_19_4.JPG) 37 | * 函数能用特定的参数(以`**`开头),收集任意多的额外关键字参数,将收集关键字相关的参数到一个新字典中。 38 | >若出现了额外的位置参数,则报错 39 | 40 | ![可变关键字参数](../imgs/python_19_5.JPG) 41 | 42 | 6.可变参数解包: 43 | 44 | * 调用者可以用`*`语法将实参(如元组、列表、`set`)打散,形成位置参数 45 | * 调用者可以用`**`语法将字典实参打散,形成关键字参数 46 | >这里的`*`和`**`均是在函数调用中出现,而不是在函数定义中出现 47 | 48 | ![可变参数解包](../imgs/python_19_6.JPG) 49 | 50 | 7.`keyword-only`参数:在Python3中,它是一种命名参数,出现在`*`参数之后,在`**`参数之前。所有的`keyword-only`参数必须使用关键字语法传递。 51 | >如果不这么做,则没有办法传递该实参 52 | 53 | ![keyword-only参数](../imgs/python_19_7.JPG) 54 | 55 | 8.函数调用时,位置参数与关键字参数可以组合 56 | 57 | * 不能为同一个形参同时指定位置实参与关键字实参 58 | * 任何关键字实参必须位于任何位置实参之后 59 | ![位置实参与关键字实参的组合](../imgs/python_19_8.JPG) 60 | 61 | 9.函数定义时的参数类型顺序: 62 | 63 | ``` 64 | def func(a,b,c='c',*d,e,f='f',**g): 65 | pass 66 | # a,b:为一般参数 67 | # c:指定了默认实参 68 | # d:为可变位置参数 69 | # e,f:为 keyword-only参数,其中f指定了默认参数 70 | # g:为可变关键字参数 71 | ``` 72 | * 调用时必须先赋值形参`c`,才能进入`d`。无法跳过`c`去赋值`d` 73 | * `e,f,g`调用时必须都是关键字实参 74 | 75 | 10.函数调用时实参类型顺序: 76 | 77 | ``` 78 | func('a','b',e='e',*seq,**dic) 79 | #seq是一个序列,它解包之后优先覆盖c,剩下的再收集成元组传给d 80 | #dic是一个字典,它解包之后优先考虑e,f,剩下的在收集成字典传递给g 81 | #e='e'这个关键字实参也可以位于'b'之后的任何位置 82 | #关键字实参必须位于位置实参之后 83 | ``` 84 | ![函数定义与调用时参数匹配顺序](../imgs/python_19_9.JPG) 85 | 86 | * 通过位置分配位置参数 87 | * 通过匹配变量名在分配关键字参数 88 | * 额外的非关键字参数分配到 `d`引用的元组中 89 | * 额外的关键字参数分配到`g`引用的字典中 90 | * 默认值分配给剩下未赋值的参数 91 | 92 | Python最后检测确保每一个参数只传入了一个值 93 | 94 | 11.定义函数时,形参列表中`*`可以单独出现。此时函数并不表示接受一个可变长度的实参列表,而是表示`*`后面的所有实参必须作为关键字实参传入: 95 | >并不能用`**`像`*`一样单独出现在函数定义中。 96 | 97 | ![*定义的keyword-only参数](../imgs/python_19_10.JPG) 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /chapter/1_python_type_introduction.md: -------------------------------------------------------------------------------- 1 | 6 | # Python 的动态类型介绍 7 | 8 | 1.在Python中没有变量的声明以及变量的类型: 9 | 10 | * 变量的类型实在运行过程中自动决定的,而不是通过声明来决定的 11 | * 变量首次赋值时,创建它。之后的再次赋值会改变该变量的值 12 | 13 | 2.类型的概念是存在于对象中而不是变量名中。变量名是通用的,它只是在特定时刻引用某个特定的对象而已。 14 | 15 | * 当变量出现在表达式中时,它会马上被当前引用的对象所代替 16 | * 任何变量名在使用前必须明确地赋值。用未赋值的变量会产生错误。 17 | 18 | > 在Python内部,变量实际上是指向对象内存空间的一个指针 19 | 20 | ~~~mermaid 21 | graph LR 22 | id1(变量名)-->|引用|id2(对象); 23 | style id2 fill:#ccf,stroke:#f66,stroke-width:2px,stroke-dasharray: 5, 5; 24 | ~~~ 25 | 3.每个表达式生成的结果,python都创建了一个新的对象去表示这个值 26 | 27 | 4.Python对象缓存了不变的对象并对其进行复用,如小整数和小字符串。但是逻辑上看,每个表达式的结果值都是不同的对象,占用不同的内存。 28 | 29 | 5.每个对象都有两个标准的头部信息: 30 | 31 | * 类型标志符:标识了该对象的类型(见第10条) 32 | * 引用计数器:决定了是否可以回收这个对象(见第8条) 33 | >引用计数器记录了当前指向该对象的引用的数目。一旦它为0, 34 | 则该对象的内存空间就会自动回收 35 | 36 | 6.给一个变量赋新值,并不是替换原始的对象,而是让这个变量去引用完全不同的一个对象。 37 | 38 | 7.多个变量名引用同一个对象时,称为共享引用: 39 | 40 | ~~~mermaid 41 | graph LR 42 | id1(变量a) -->|引用|id2(对象) 43 | id3(变量b) -->|引用|id2(对象) 44 | style id2 fill:#ccf,stroke:#f66,stroke-width:2px,stroke-dasharray: 5, 5; 45 | ~~~ 46 | 47 | * 在共享引用中,对象的原地修改操作会影响到所有的引用该对象的变量 48 | * 共享引用的多个变量之间,`is`比较的结果为`True` 49 | >`==`操作符比较的是两个变量引用的对象是否具有相同的值 50 | 51 | >`is`操作符比较的是两个变量是否引用的是同一个对象 52 | 53 | 8.`sys`模块的`getrefcount`函数会返回对象的引用次数,如: 54 | ![getrefcount()运行结果](../imgs/python_1_1.JPG ) 55 | 56 | > 这里可以看到Python对象缓存了不变的对象并对其进行复用 57 | 58 | 9.动态类型是Python多态的基础,因为没有类型约束。Python的多态是`x.method`的方法运行时,`method`的意义取决于`x`的类型,**属性总是在运行期解析**。 59 | 60 | 10.查看对象的类型用`type()`函数,如: 61 | ![type()运行结果](../imgs/python_1_2.JPG ) 62 | 63 | 11.Python提供了几种方法来查看对象的属性以及详细信息: 64 | 65 | * dir(x):查看x所指对象的属性 66 | * help(x.attr):查看x所指对象的attr属性 67 | * help(x):查看x所指对象的详细信息 68 | ![dir、help运行结果](../imgs/python_1_3.JPG ) 69 | 70 | 12.`None`对象是一个特殊的Python对象,它总是`False`,一般用于占位。它有一块内存,是一个真正的对象。它不代表未定义,事实上它有定义。 71 | 72 | * `None`是所有函数和方法的默认返回值 73 | ![type(None)](../imgs/python_1_4.JPG ) 74 | 75 | 13.Python中,任何东西都是对象类型(见第10条),类型本身也是对象类型: 76 | 77 | * `type(x)`返回变量`x`指向对象的类型 78 | * `isinstance(x,typename)`用于测试`x`所指对象是否是`typename`类型 79 | * 调用类型名是对这些对象构造函数的调用,而不仅仅是类型转换 80 | ![任何东西都是对象类型](../imgs/python_1_5.JPG ) 81 | 82 | 14.基本上在代码中进行类型检查是错误的,着破坏了代码的灵活性,限制了代码的类型。Python代码不关心特定的数据类型,只关心接口,这就是Python的多态设计 83 | 84 | * 一个操作的意义取决于被操作对象的类型。同样的操作对不同的对象来说意义可能不同,前提是该对象支持该操作 85 | * 若对象不支持某种操作,则Python会在运行时检测到错误并自动抛出一个异常 86 | 87 | 15.Python可以在旧版本中开启新版本的特性,只需用在模块文件中使用: 88 | `from __future__ import 特性名` -------------------------------------------------------------------------------- /chapter/20_python_function_advanced.md: -------------------------------------------------------------------------------- 1 | 6 | # 函数的高级特性 7 | 1.在Python中,函数的递归通常比`for`循环要慢而且空间消耗大,但是递归的优点是可以遍历任意形状的结构。 8 | 9 | 2.Python函数是对象,自身存储在内存块中,它可以自由地传递与引用。 10 | 11 | * 函数对象支持一个特殊操作:有括号`()`以及参数列表执行调用行为 12 | * 我们可以通用地检查函数对象的某些属性:如`.__name__`属性、`.__code__`属性 13 | * 可以向函数对象附加任意的用户自定义属性,如`func.count=0`。这样的属性可以用来直接将状态信息附加到函数对象上 14 | * Python3中,可以给函数对象附加注解。注解不作任何事情,而且注解是可选的,它被附加在函数对象的`.__annotaions__`属性中。注解的格式为: 15 | 16 | ``` 17 | def func(a:'a',b:(1,10),c:float) -> int: 18 | return a+b+c 19 | ``` 20 | * 注解分两种:参数注解紧随形参名字的冒号`:`之后;返回值注解紧随参数列表的`->`之后 21 | * 当出现注解时,Python将它们收集到字典中并附加到`.__annotations__`属性中 22 | * 注解可以与默认值同时出现,此时形参形式为`c:float=4.0` 23 | * 注解只有在`def`中有效,在`lambda`表达式中无效 24 | 25 | ![函数对象](../imgs/python_20_1.JPG) 26 | 27 | 3.匿名函数:`lambda`表达式 28 | `lambda`表达式创建了一个函数对象,它返回该函数对象而不是将其赋值给一个变量名。 29 | 创建`lambda`表达式的语法为: 30 | 31 | ``` 32 | labda arg1,arg2,...argN: expression using args 33 | ``` 34 | * `lambda`表达式是个表达式而不是语句,它能出现在不允许`def`出现的某些地方,比如参数中 35 | * `lambda`表达式返回一个值(一个新的函数对象),可以将它手动赋值给一个变量名 36 | >`def`总是将一个新的函数对象自动赋值给一个变量名(函数名) 37 | 38 | * `lambda`的主体是一个单一的表达式,而不是一个代码块。因此`lambda`通常比`def`功能简单 39 | >`lambda`内部甚至不能使用`if`语句 40 | * `lambda`主体中表达式的值就是调用时的返回值,不需要显式`return` 41 | * `lambda`表达式也能使用默认实参 42 | * `lambda`表达式主体中遵循`def`内一样的名字作用域查找法则 43 | ![lambda表达式](../imgs/python_20_2.JPG) 44 | 45 | 4.出于可读性要求,最好不要嵌套使用`lambda`表达式 46 | 47 | 5.`lambda`表达式应用于`map()`、`filter()`、`reduce()`等函数中较多 48 | 49 | 6.Python是静态检测局部变量的: 50 | 51 | ``` 52 | x='global' 53 | def func(): 54 | print(x) 55 | x=3 56 | ``` 57 | 编译时,Python看到了赋值语句`x=3`,因此决定了在函数内的任何地方,`x`都是本地变量。但是`print(x)`时赋值语句并未发生,此时即使全局中有全局的`x`,也会报错。 58 | 59 | * 任何在函数体内的赋值、`import`,嵌套`def`,嵌套类等都受这种行为影响 60 | >即只要有局部变量的定义,无论在函数内哪个地方,其作用域都是本局部作用域全域 61 | 62 | ![局部作用域的全域效果](../imgs/python_20_3.JPG) 63 | 64 | 7.Python在内部会将每个默认实参保存成对应的对象,附加在这个函数本身。在不同的函数调用期间,这些默认实参会保存同一个对象。因此对于可变对象作为默认实参注意保持警惕。 65 | ![可变的默认实参](../imgs/python_20_4.JPG) 66 | 67 | 68 | -------------------------------------------------------------------------------- /chapter/21_python_module.md: -------------------------------------------------------------------------------- 1 | 6 | # 模块 7 | 1.从实际角度,模块对应Python程序文件(或者用外部语言如C|C#编写的扩展)。从逻辑上看,模块是最高级别的程序组织单元 8 | 9 | * 每个Python程序文件都是一个模块 10 | * 模块导入另一个模块后,可以直接使用被导模块定义的全局变量名 11 | ![模块](../imgs/python_21_1.JPG) 12 | 13 | 2.Python程序是作为一个主体的、顶层文件来构造,配合零个或者多个支持的模块文件 14 | 15 | 3.Python自带了很多模块,称为标准链接库。他们提供了很多常用功能 16 | 17 | 4.导入模块用`import`。其通用格式为`import modname`。其中`modname`为模块名,它没有文件后缀名`.py`,也没有文件路径名。 18 | 导入并非是C语言的`#include`。导入其实是运行时的运算。程序首次导入一个模块时,执行三个步骤: 19 | 20 | * 找到模块文件 21 | * 编译成字节码(即`.pyc`文件)。如果字节码文件不存在或者字节码文件比源代码文件旧, 22 | 则执行该步骤。否则跳过该步骤直接加载字节码 23 | * 执行模块代码来创建其定义的对象 24 | 25 | 在这之后导入相同模块时,会跳过这三步而只是提取内存中已经加载的模块对象。 26 | > 从内部看,Python将加载的模块存储到一个名为`sys.modules`的字典中,键就是模块名字符串。在每次导入模块开始时都检查这个字典,若模块不存在则执行上述三步。 27 | 28 | ![模块导入过程](../imgs/python_21_2.JPG) 29 | 30 | 5.当文件`import`时,会进行编译产生字节码文件`.pyc`,因此只有被导入文件才会在机器上留下`.pyc`文件。顶层文件的字节码在内部使用后就丢弃了,并未保留下来。 31 | > 顶层文件通常设计成直接执行,而不是被导入的 32 | 33 | 6.Python模块文件搜索路径: 34 | 35 | * 程序主目录 36 | * 环境变量`PYTHONPATH`指定的目录 37 | * 标准链接库目录(这个一般不动它) 38 | * 任何`.pth`文件的内容,其中`.path`文件在前三者中查找到的。 39 | >Python会将每个`.pth`文件的每行目录从头到尾添加到`sys.path`列表的最后 40 | >(在此期间Python会过滤`.pth`文件中目录列表中重复的和不存在的目录) 41 | 42 | 以上四者优先级从高到低。这四部分组合起来就是`sys.path`列表的内容 43 | ![sys.path](../imgs/python_21_3.JPG) 44 | 45 | 7.`sys.path`列表就是模块的搜索路径。Python在程序启动时配置它,自动将顶级文件的主目录(或代表当前工作目录的一个空字符串)、环境变量`PYTHONPATH`指定的目录、标准库目录以及已创建的任何`.pth`文件的内容合并 46 | 47 | * 模块搜索时,从左到右搜索`sys.path`,直到第一次找到要`import`的文件 48 | 49 | 8.`import`模块时,省略文件后缀名因为模块可能是`.py`文件、`.pyc`文件,或者扩展的C模块等。 50 | 51 | 9.创建模块:任何保存为`.py`文件的文件均被自动认为是Python模块。所有该模块顶层指定的变量均为模块属性。 52 | > 可执行但不会被导入的顶层文件不必保存为.py文件 53 | 54 | * 因为模块名在Python中会变成变量名,因此模块名必须遵守普通变量名的命名规则 55 | 56 | 10.`import`和`from`语句: 57 | 58 | * `import`使得一个变量名引用整个模块对象:`import module1` 59 | * `from`将一个变量名赋值给另一个模块中同名的对象:`from module1 import printer`。在本模块内`printer`名字引用了`module1.printer`对象 60 | * `from *`语句将多个变量名赋值给了另一个模块中同名的对象:`from module1 import *`。在本模块内,所有`module1.name`对象赋值给了`name`变量名 61 | ![import与from语句](../imgs/python_21_4.JPG) 62 | 63 | 有几点需要注意: 64 | 65 | * `from`语句首先与`import`一样导入模块文件。但它多了一步:定义一个或多个变量名指向被导入模块中的同名对象 66 | * `from`与`import`都是隐性赋值语句 67 | * `from`与`import`对本模块的命名空间影响不同:`from`会在命名空间中引入`from import`的变量名而不会引入模块名, 68 | `import`会在命名空间中引入模块名 69 | ![import与from语句](../imgs/python_21_4_dict.JPG) 70 | * `from`、`import`与`def`一样是可执行语句,而不是编译器声明 71 | * 当出现交叉导入时,可以使用`import` ,用`from`可能出现死锁的问题:`modA`需要`from import` `modB`的变量 72 | ,而此时`modB`也在`from import` `modA`的变量 73 | ![import与from语句](../imgs/python_21_4_cross_import.JPG) 74 | 75 | 11.要修改被导入的全局变量,必须用`import`,然后用模块名的属性修改它;不能用以`from`隐式创建的变量名来修改。 76 | ![修改模块属性](../imgs/python_21_5.JPG) 77 | 78 | 12.用`from`时,被导入模块对象并没有赋值给变量名: 79 | 80 | * `import module1`:`module1`既是模块名,也是一个变量名(引用被导入模块对象) 81 | * `from module1 import func`:`module1`仅仅是模块名,而不是变量名 82 | ![模块名与变量名](../imgs/python_21_6.JPG) 83 | 84 | 13.`from`语句陷阱: 85 | 86 | * `from`语句可能破坏命名空间 87 | * `from`后跟随`reload`时,`from`导入的变量名还是原始的对象 88 | 89 | 14.模块的命名空间可以通过属性`.__dict__`或者`dir(modname)`来获取 90 | 91 | * 在Python内部,模块命名空间是作为字典对象存储的 92 | * 我们在模块文件中赋值的变量名在Python内部称为命名空间字典的键 93 | ![模块命名空间](../imgs/python_21_7.JPG) 94 | 95 | 15.一个模块内无法使用其他模块内的变量,除非明确地进行了导入操作 96 | 97 | 17.重载函数`reload()`:它会强制已加载的模块代码重新载入并重新执行 98 | 99 | * `reload`函数可以修改程序的一部分,而无需停止整个程序 100 | * `reload`函数只能用于Python编写的模块,而无法用于其它语言编写的扩展模块 101 | ![reload函数](../imgs/python_21_8.JPG) 102 | 103 | 18.`reload()`与`import`和`from`的差异: 104 | 105 | * `reload`是Python内置函数,返回值为模块对象,`import`与`from`是语句 106 | * 传递给`reload`是已经存在的模块对象,而不是一个变量名 107 | * `reload`在Python3.0中位于`imp`标准库模块中,必须首先导入才可用。 108 | 109 | 19.`reload`工作细节: 110 | 111 | * `reload`并不会删除并重建模块对象,它只是修改模块对象。即原来模块的每个属性对象内存空间还在,所有旧的引用指向他们,新的引用指向修改后的属性对象内存空间 112 | * `reload`会在模块当前命名空间内执行模块文件的新代码 113 | * `reload`会影响所有使用`import`读取了模块的用户,用户会发现模块的属性已变 114 | * `reload`只会对以后使用`from`的代码造成影响,之前用`from`的代码并不受影响。之前的名字还可用,且引用的是旧对象 115 | ![reload与import、from](../imgs/python_21_9.JPG) -------------------------------------------------------------------------------- /chapter/22_python_package.md: -------------------------------------------------------------------------------- 1 | 6 | # 模块包 7 | 1.`import`时,也可以指定目录。目录称为包,这类的导入称为包导入。 8 | 9 | * 包导入是将计算机上的目录变成另一个Python命名空间,它的属性对应于目录中包含的子目录和模块文件 10 | * 包导入的语法: 11 | 12 | ``` 13 | import dir1.dir2.modname 14 | from dir1.dir2.modname import x 15 | ``` 16 | 17 | ![包导入](../imgs/python_22_1.JPG) 18 | 19 | * 包导入语句的路径中,每个目录内部必须要有`__init__.py`这个文件。否则包导入会失败 20 | * `__init__.py`就像普通模块文件,它可以为空的 21 | * Python首次导入某个目录时,会自动执行该目录下`__init__.py`文件的所有程序代码 22 | * `import dir1.dir2.modname`包导入后,每个目录名都成为模块对象 23 | (模块对象的命名空间由该目录下的`__init__.py`中所有的全局变量定义 24 | (包含显式定义和隐式定义)决定) 25 | * `__init__.py`中的全局变量称为对应目录包的属性 26 | 27 | ![__init__.py](../imgs/python_22_2.JPG) 28 | 29 | 2.任何已导入的目录包也可以用`reload`重新加载,来强制该目录包重新加载 30 | >`reload`一个目录包的用法与细节与`reload`一个模块相同 31 | 32 | ![reload包](../imgs/python_22_3.JPG) 33 | 34 | 3.包与`import`使用时输入字数较长,每次使用时需要输入完整包路径。可以用from语句来避免 35 | ![import包与from包区别](../imgs/python_22_4.JPG) 36 | 37 | 4.包相对导入:`from`语句可以用`.`与`..`: 38 | 39 | ``` 40 | from . import modname1 #modname1与本模块在同一包中(即与本文件在同一目录下) 41 | from .modname1 import name #modname1与本模块在同一包中(即与本文件在同一目录下) 42 | from .. import modname2 #modname2在本模块的父目录中(即在本文件上层) 43 | ``` 44 | >Python2中,`import modname`会优先在本模块所在目录下加载`modname`以执行相对导入。 45 | >因此局部的模块可能会因此屏蔽`sys.path`上的另一个模块 46 | >要想启用相对导入功能,使用`from __future__ import absolute_import` 47 | 48 | * Python3中,没有点号的导入均为绝对导入。`import`总是优先在包外查找模块 49 | ![包相对导入](../imgs/python_22_5.JPG) 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /chapter/23_python_module_advanced.md: -------------------------------------------------------------------------------- 1 | 6 | # 模块高级用法 7 | 1.Python模块会默认导出其模块文件顶层所赋值的所有变量名,不存在私有变量名。所有的私有数据更像是一个约定,而不是语法约束: 8 | 9 | * 下划线开始的变量名`_x`:`from *`导入该模块时,这类变量名不会被复制出去 10 | ![私有模块变量](../imgs/python_23_1.JPG) 11 | * 模块文件顶层的变量名列表`__all__`:它是一个变量名的字符串列表。`from *`语句只会把列在`__all__`列表中的这些变量名复制出来。 12 | ![__all__变量名列表](../imgs/python_23_2.JPG) 13 | >Python会首先查找模块内的`__all__`列表;否该列表未定义,则`from *`会复制那些非 14 | >`_`开头的所有变量名 15 | >所有这些隐藏变量名的方法都可以通过模块的属性直接绕开 16 | 17 | 2.当文件是以顶层程序文件执行时,该模块的`__name__`属性会设为字符串`"__main__"`。若文件被导入,则`__name__`属性就成为文件名去掉后缀的名字 18 | 19 | * 模块可以检测自己的`__name__`属性,以确定它是在执行还是被导入 20 | * 使用`__name__`最常见的是用于自我测试代码:在文件末尾添加测试部分: 21 | 22 | ``` 23 | if __name__=='__main__': 24 | #pass 25 | ``` 26 | 27 | 3.在程序中修改`sys.path`内置列表,会对修改点之后的所有导入产生影响。因为所有导入都使用同一个`sys.path`列表 28 | 29 | 4.`import`和`from`可以使用`as`扩展,通过这种方法解决变量名冲突: 30 | 31 | ``` 32 | import modname as name1 33 | from modname import attr as name2 34 | ``` 35 | 在使用`as`扩展之后,必须用`name1`、`name2`访问,而不能用`modname`或者`attr`,因为它们事实上被`del`掉了 36 | ![import、from as语句](../imgs/python_23_3.JPG) 37 | 38 | 5.在`import`与`from`时有个问题,即必须编写变量名,而无法通过字符串指定。有两种方法: 39 | 40 | * 使用`exec: `exec("import "+modname_string)` 41 | * 使用内置的`__import__`函数:`__import__(modname_string)`,它返回一个模块对象 42 | > 这种方法速度较快 43 | 44 | ![通过字符串指定导入包名](../imgs/python_23_4.JPG) 45 | 46 | 6.`reload(modname)`只会重载模块`modname`,而对于模块`modname`文件中`import`的模块,`reload`函数不会自动加载。 47 | 要想`reload`模块`A`以及`A` `import`的所有模块,可以手工递归扫描`A`模块的`__dict__`属性,并检查每一项的`type`以找到所有`import`的模块然后`reload`这些模块 48 | 49 | 7.可以通过下列几种办法获取模块的某个属性: 50 | 51 | * `modname.attr`:直接通过模块对象访问 52 | * `modname.__dict__['attr']`:通过模块对象的`__dict__`属性字典访问 53 | * `sys.modules['modname'].name`:通过Python的`sys.modules`获取模块对象来访问 54 | * `getattr(modname,'attr')`:通过模块对象的`.getattr()`方法来访问 55 | -------------------------------------------------------------------------------- /chapter/24_python_class.md: -------------------------------------------------------------------------------- 1 | 6 | # 类 class 7 | 1.Python中,类`class`与实例`instance`是两种不同的对象类型 8 | 9 | * 类对象是实例对象的工厂 10 | * 类对象与实例对象都有各自独立的命名空间 11 | * 类对象来自于类定义语句,实例对象来自于函数调用语句 12 | * 实例对象可自动存取类对象中的变量名 13 | 14 | 2.类的定义: 15 | 16 | ``` 17 | class class_name: 18 | name1=val 19 | def method(self): 20 | pass 21 | ``` 22 | 23 | * `class`语句类似于`def`语句,也是可执行语句。执行时会产生新的类对象,并将该类对象赋值给变量名(即类名) 24 | * `class`语句内的顶层赋值语句会创建类的属性。`class`语句创建的作用域会成为类属性的命名空间 25 | > 如果是`class`内的`def`中的赋值语句,则并不会创建类的属性 26 | * 类属性为所有的实例对象提供状态和行为,它是由该类创建的所有实例对象共享的 27 | 28 | 3.生成实例对象: 29 | 30 | ``` 31 | instance_name=class_name() 32 | ``` 33 | 34 | * 像函数那样调用类对象会创建新的实例对象。 35 | * 每次调用时,均会返回新的实例对象 36 | * 每个实例对象都有自己的命名空间。同一个类的实例对象不一定属性都相同 37 | * 每一个实例对象继承类的属性并创建了自己的命名空间 38 | * 类创建的实例对象是有新的命名空间。刚开始该命名空间是空的, 39 | 但它会继承创建该实例所属类对象的属性。 40 | * 继承的意思是,虽然实例对象的命名空间是空的。 41 | 但是名字查找会自动上升到类对象的名字空间去查找 42 | * 在类的`def`中,第一个参数(根据惯例称为`self`)会引用调用该函数的实例对象。对`self`的属性赋值,会创建或修改实例对象的属性,而非类的属性。 43 | * 可以通过方法调用: `instance_name.func()` 44 | * 也可以通过类调用: `class_name.func(instance_name)` 45 | 46 | ![类与实例对象](../imgs/python_24_1.JPG) 47 | 48 | 4.与C++不同,Python的`class`语句是一个可执行语句,且是隐式赋值的。 49 | 50 | 5.可以在`class`语句外创建类对象的新属性,通过向类对象直接赋值来实现。 51 | 52 | ``` 53 | classA.attr=val 54 | ``` 55 | 56 | 可以在`class`语句外创建实例对象的新属性,通过向实例对象直接赋值来实现。 57 | 58 | ``` 59 | a=classA() 60 | a.attr=val 61 | ``` 62 | 63 | ![类与实例对象新属性](../imgs/python_24_2.JPG) 64 | 65 | 6.类可以继承。被继承的类称为超类,继承类称为子类。 66 | 67 | ``` 68 | class childC (parentC): # childC为子类,parentC为超类 69 | pass 70 | ``` 71 | 72 | * 类对象会继承其超类对象中定义的所有类属性名称。读属性时,若该属性不存在于本类中,Python会自动在超类的命名空间中寻找 73 | >若是写属性,则会创建新属性(此时仅操作本类的命名空间) 74 | * 实例对象会继承所有可访问到的类的属性。读变量名时,Python会首先检查实例的命名空间,然后是类的命名空间,最后是沿继承链查找所有超类的命名空间 75 | * 若子类重新定义了超类的变量名(无论是在`class`内部定义,还是在`class`外部通过赋值来定义),子类会取代并定制所继承的行为。这称为重载 76 | ![类的继承](../imgs/python_24_3.JPG) 77 | 78 | 7.类对象的 `.__dict__`属性是类对象的命名空间,是一个类字典对象`mappingproxy`对象 ; 79 | 实例对象的 `.__dict__`属性是实例对象的命名空间,是一个字典; 80 | 通过查看它们可以轻易地查看到继承树的各属性 81 | ![类与实例的命名空间](../imgs/python_24_4.JPG) 82 | 83 | 8.实例对象的`.__class__`属性是它所属的类 84 | 类对象的`__bases__`属性是它超类对象的元组 85 | 类对象的`__name__`属性是类名 86 | ![实例所属类,以及类的超类u元组](../imgs/python_24_5.JPG) 87 | 88 | 9.在子类中调用超类的方法:`superClass.func(obj,args)`,其中`obj`通常为`self` 89 | ![子类中调用超类方法](../imgs/python_24_6.JPG) 90 | 91 | 10.Python的继承与C++继承不同。在Python中,当对象通过点号运算读取属性值时就会发生继承,而且涉及了搜索属性定义树。 92 | 93 | * 每次使用`name.attr`时(`name`为实例对象或者类对象),Python会从底部向上搜索命名空间树。先从本对象的命名空间开始,一直搜索到第一个找到的`attr`名字就停止 94 | * 命名空间树中较低位置处的定义会覆盖较高位置处的定义 95 | * 继承树的搜索仅仅发生在读取属性值的时候。在写属性值时,执行的是属性的定义(当前命名空间中该名字不存在)或赋值(当前命名空间中该名字已存在)语义。 96 | 97 | ~~~mermaid 98 | graph BT; 99 | A(实例命名空间)-->B[类命名空间]; 100 | B-->C[超类1命名空间]; 101 | B-->D[超类2命名空间]; 102 | style A fill:#f9f,stroke:#333; 103 | ~~~ 104 | 105 | 11.类对象与实例对象都是内存中的临时对象。可以通过对象持久化来把他们保存在磁盘中。 106 | 107 | * `pickle`模块:通用的对象序列化与反序列化工具。它可以将任何对象转换为字节串,以及将该字节串在内存中重建为最初的对象。`pickle`常用接口为: 108 | * 序列化: 109 | * `pickle.dump(obj, file, protocol=None, *, fix_imports=True) `: 将`obj`对象序列化并写入`file`文件对象中 110 | * `pickle.dumps(obj, protocol=None, *, fix_imports=True)`:将`obj`对象序列化并返回对应的字节串对象(并不写入文件中) 111 | * 反序列化: 112 | * `pickle.load(file, *, fix_imports=True, encoding="ASCII", errors="strict") `:从`file`对象中保存的字节串中读取序列化数据,反序列化为对象 113 | * `pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict")` :从字节串中读取序列化数据,反序列化为对象 114 | 115 | ![pickle模块](../imgs/python_24_7.JPG) 116 | 117 | * `dbm`模块:一个可以通过键访问的文件系统.它的键、值都是字节串或者字符串。它以类似字典的方式访问(但是首先要`open`)。 118 | ![dbm模块](../imgs/python_24_8.JPG) 119 | 120 | * `shelve`模块:以上两个模块按照键将Python对象存/取到一个文件中。`shelve`模块提供了一个额外的结构层。允许按照键来存储`pickle`处理后的对象 121 | 122 | 12.`shelve`模块用法:它用`pickle`把对象转换为字节串,并将其存储在一个`dbm`文件的键之下;它通过键获取`pickle`化的字节串,并用`pickle`在内存中重新创建最初的对象 123 | 124 | * 一个`shelve`的`pickle`化对象看上去就像字典。`shelve`自动把字典操作映射到存储|读取在文件中的对象 125 | > 一个`shelve`和常规字典用法上的唯一区别: 126 | >一开始必须打开`shelve`并且在最后关闭它。另外`shelve`不支持`SQL`查询工具 127 | * 存取语法 128 | * 存储的语法: 129 | 130 | ``` 131 | import shelve 132 | db=shelve.open('filename') #打开 133 | for obj in objList: 134 | db[obj.name]=obj #写入 135 | db.close() #关闭 136 | ``` 137 | * 读取的语法: 138 | 139 | ``` 140 | import shelve 141 | db=shelve.open('filename') #打开 142 | for key in db:#像字典一样访问 143 | print(key,'=>',db[key]) #读取 144 | db.close() #关闭 145 | ``` 146 | * 载入重建存储的对象时,不必`import`对象所属类。因为Python对一个对象进行`pickle`操作时,记录了`self`实例属性,以及实例所属类的名字和类的位置。当`shelve`获取实例对象并对其进行`unpickle`时,Python会自动重新`import`该类。 147 | * 优缺点: 148 | * 缺点:`pickle`作用的类必须在一个模块文件顶部编码,且该模块文件可通过`sys.path`找到 149 | * 优点:当类实例对象再次重建时,对类的源代码文件的修改会自动选取。 150 | >这一般发生在两次运行时。若仅仅在一次运行中,则前面已import,再次import无效果 151 | 152 | ![shelve模块](../imgs/python_24_9.JPG) 153 | 154 | 13.类可以有文档字符串。文档字符串是位于各种结构顶部的字符串常量。 155 | 156 | * 文档字符串在运行时能保持 157 | * 可以通过`.__doc__`属性获取 158 | ![文档说明字符串](../imgs/python_24_10.JPG) 159 | 160 | 161 | 162 | -------------------------------------------------------------------------------- /chapter/25_python_namespace.md: -------------------------------------------------------------------------------- 1 | 6 | # Python命名空间 7 | 1.有点号和无点号的变量名,会使用不同的方式处理 8 | 9 | * 无点号运算符的变量名,使用`LEGB`作用域规则查找名字 10 | * 有点好运算符的变量名,使用的是对象的命名空间查找名字 11 | * 模块的作用域以及类的作用域会对对象的命名空间进行初始化 12 | 13 | 2.模块、类、实例对象的命名空间实际上是以字典的形式实现的,并由内置属性`.__dict__`表示 14 | 15 | * 属性点号运算其实内部就是字典的索引运算 16 | * 属性继承其实就是搜索链接的字典 17 | >每个实例都有各自独立的命名空间字典。初始时为空字典。 18 | >随着对`self`或者对象的属性赋值,命名空间字典不断扩张 19 | 20 | 3.读取属性可以通过点运算符或者直接通过键索引: 21 | 22 | ``` 23 | obj.attr #通过点运算符 24 | obj.__dict__['attr']#通过键索引 25 | ``` 26 | 通过键索引时必须给出属性名字符串;通过点运算符时给出的是属性名(不是字符串) 27 | 28 | 4.`dir`函数可以输出对象的所有可访问属性,包括它继承的名字(以及其他一些内置的系统属性) 29 | ![dir展示所有可访问属性](../imgs/python_25_1.JPG) 30 | 31 | -------------------------------------------------------------------------------- /chapter/26_python_operator.md: -------------------------------------------------------------------------------- 1 | 6 | # 运算符重载 7 | 1.运算符重载只是意味着在类方法中拦截内置的操作 8 | 9 | * 运算符重载让类拦截常规的Python运算 10 | * 类可以重载所有的Python表达式运算符。当实例对象作为内置运算符的操作数时,这些方法会自动调用 11 | * 类也可以重载打印、函数调用、属性点号运算符等内置运算 12 | * 重载可以使得实例对象的行为更像内置类型 13 | * 重载运算符通常并不是必须的,也不是默认的行为 14 | 15 | ![运算符重载](../imgs/python_26_1.JPG) 16 | 17 | 2.Python中所有可以被重载的方法名称前、后均有两个下划线字符,以便将它与其他类内定义的名字区分开来,如`__add__` 18 | 19 | 3.若使用未定义运算符重载方法,则它可能继承自超类。若超类中也没有则说明你的类不支持该运算,强势使用该运算符则抛出异常 20 | 21 | ![运算符重载](../imgs/python_26_2.JPG) 22 | 23 | 4.`.__init__(self,args)`方法:称为构造函数。当新的实例对象构造时,会调用`.__init__(self,args)`方法。它用于初始化实例的状态 24 | 25 | ![运算符重载](../imgs/python_26_3.JPG) 26 | 27 | 5.`.__getitem__(self,index)`和`.__setitem(self,index,value)`方法: 28 | 29 | * 对于实例对象的索引运算,会自动调用`.__getitem__(self,index)`方法,将实例对象作为第一个参数传递,方括号内的索引值传递给第二个参数 30 | 31 | * 对于分片表达式也调用`.__getitem__(self,index)`方法。实际上分片边界如`[2:4]`绑定到了一个`slice`分片对象上,该对象传递给了`.__getitem__`方法。 32 | 对于带有一个`.__getitem__`方法的类,该方法必须既能针对基本索引(一个整数),又能针对分片调用(一个`slice`对象作为参数) 33 | * `.__setitem(self,index,value)`方法类似地拦截索引赋值和分片赋值。第一个参数为实例对象,第二个参数为基本索引或者分片对象,第三个参数为值 34 | 35 | ![重载索引和分片](../imgs/python_26_4.JPG) 36 | 37 | 6.`.__index__(self)`方法:该方法将实例对象转换为整数值。即当要求整数值的地方出现了实例对象时自行调用。 38 | 39 | ![重载对象转整数](../imgs/python_26_5.JPG) 40 | 41 | 7.`.__getitem__(self,index)`也是Python的重载迭代方式之一。一旦定义了这个方法,`for`循环每一次循环时可以调用`.__getitem__(self,index)`方法。因此任何响应了索引运算的内置或者用户自定义的实例对象通用可以响应迭代。 42 | 43 | * `for`可以检测到超出边界异常`IndexError`,从而终止迭代 44 | 45 | ![.__getitem__用于迭代](../imgs/python_26_6.JPG) 46 | 47 | 8.目前Python的所有迭代环境都会首先尝试调用`.__iter__(self)`方法,再尝试调用`.__getitem__(self,index)`方法。 48 | 49 | * `.__iter__(self)`方法必须返回一个迭代器对象。Python的迭代环境通过重复调用这个迭代器对象的`.__next__(self)`方法,直到发生了`StopIteration`异常 50 | * `.__iter__(self)`返回的迭代器对象会在调用`.__next__(self)` 51 | 的过程中明确保留状态信息,因此比`.__getitem__(self,index)`方法具有更好的通用性 52 | * 迭代器对象没有重载索引表达式,因此不支持随机的索引运算 53 | * `.__iter__(self)`返回的迭代器只能顺序迭代一次。 54 | 因此每次要进行新的一轮循环时必须创建一个新的迭代器对象 55 | * 对于调用`.__getitem__(self,index)`的环境,Python的迭代环境通过重复调用该方法,其中`index`每轮迭代中从 0 依次递增,直到发生了`IndexError`异常 56 | 57 | ![Python迭代环境原理](../imgs/python_26_7.JPG) 58 | 59 | 9.要让实例对象支持多个迭代器,`.__iter__(self)`方法必须创建并返回新的迭代器对象。 60 | >每个激活状态下的迭代器都有自己的状态信息,而不管其他激活状态下的迭代器 61 | 62 | ![实例安装多个迭代器](../imgs/python_26_8.JPG) 63 | 64 | 10.类通常把`in`成员关系运算符实现为一个迭代,用`.__iter__(self)`方法或`.__getitem__(self,index)`方法。也能实现`.__contains__(self,value)`方法来实现特定成员关系。 65 | 66 | * `.__contains__(self,value)`方法优先于`.__iter__(self)`方法,`.__iter__(self)`方法优先于`.__getitem__(self,index)`方法采纳 67 | 68 | ![in 运算符的实现原理](../imgs/python_26_9.JPG) 69 | 70 | 11.`.__getattr__(self,'name')`方法:拦截属性点号运算`obj.name`。只有当对未定义(即不存在)的属性名称进行点号运算时,实例对象会调用此方法 71 | > 当Python可以从继承树中找到该属性名时,并不会调用`.__getattr__(self,'name')`方法 72 | > 73 | >属性不仅仅是变量名,也可以是方法名 74 | 75 | * 内置的`getattr(obj,'name')`函数等价于调用`obj.name`,它执行继承搜索。搜不到时调用`.__getattr__(self,“name”)`方法 76 | * 如果没有定义`.__getattr__(self,“name”)`方法,则对于不知道如何处理的属性(即找不到的),则Python抛出内置的`AttributeError`异常 77 | 78 | ![重载.__getattr__方法](../imgs/python_26_10.JPG) 79 | 80 | 12.`.__setattr__(self,'name',value)`方法:拦截所有的属性赋值语句(无论该属性名是否存在) 81 | > 对于属性赋值语句,因为如果该属性曾经不存在,则一旦赋值就增加了一个新的属性 82 | > 83 | > 属性不仅仅是变量名,也可以是方法名 84 | 85 | 注意:`.__setattr__(self,'name',value)`方法的函数体内,任何对`self`属性赋值语句(`self.name=value`)都会再次递归调用`.__setattr__(self,'name',value)`函数 86 | 87 | * 为了防止`.__setattr__(self,'name',value)`函数体内的无穷递归,在该方法内的`self`属性赋值要采用属性字典索引的方法:`self.__dict__['name']=value` 88 | >属性不仅仅是变量名,也可以是方法名 89 | 90 | * 内置的`setattr(obj,'name',value)`函数等价于调用`obj.name=value` 91 | 92 | ![重载.__setattr__方法](../imgs/python_26_11.JPG) 93 | 94 | 13.`.__getattribute__(self,'name')`方法:拦截所有的属性读取,而不仅仅是那些未定义的。 95 | 注意:`.__getattribute__(self,'name')`方法的函数体内,任何对`self`属性读取语句(`self.name`)都会再次递归调用`.__getattribute__(self,'name')`函数。尽量不要重载`.__getattribute__(self,'name')`方法避免无穷递归 96 | >属性不仅仅是变量名,也可以是方法名 97 | 98 | ![重载.__getattribute__方法](../imgs/python_26_12.JPG) 99 | 100 | ![.__getattribute__方法递归调用](../imgs/python_26_13.JPG) 101 | 102 | 14.通过`.__getattr__`与`.__setattr__`方法混合使用可以模拟实例对象的私有属性: 103 | 104 | * 实例对象保存一个`self.private`变量名列表 105 | * 对`.__setattr__`与`.__getattr__`,判断属性名是否在`self.private`变量名列表中。 106 | 若是,则抛出异常 107 | > 对于通过`obj.__dict__['name']`访问,可以绕过这种机制 108 | 109 | 15.`.__add__(self,value)`方法:当实例对象在加法中时调用 110 | 111 | 16.`.__repr__(self)`和`.__str__(self)`方法:当实例对象在打印或者转成字符串时调用 112 | 113 | * 打印会首先尝试`.__str__(self)`方法和`str(x)`内置函数。如果没有,则使用 114 | `.__repr__(self)`方法 115 | * `.__repr__(self)`主要应用于交互式下提示回应以及`repr`函数。如果没有,它不会调用 116 | `.__str__(self)`方法 117 | 118 | ![重载.__repr__方法和.__str__方法](../imgs/python_26_14.JPG) 119 | 120 | 17.加法有三种: 121 | 122 | * 常规加法:实例对象在`+`左侧,由`.__add__(self,value)`拦截 123 | * 右侧加法:实例对象在`+`右侧,由`.__radd__(self,value)`拦截 124 | * 原地加法:实例对西在`+=`左侧,由`.__iadd__(self,value)`拦截 125 | 126 | 要实现满足交换律的运算符,要同时重载`.__add__(self,value)`与`.__radd__(self,value)`方法。 127 | 128 | * 当不同类的实例对象混合出现在`+`两侧时,Python优先选择左侧的那个类来拦截`+` 129 | >注意当在这三种加法函数体内出现了`+`或者`+=`时可能出现递归调用 130 | * 原地`+=`优先采用`.__iadd__(self,value)`,如果它没有重载,则采用`.__add__(self,value)` 131 | 132 | ![三种加法](../imgs/python_26_15.JPG) 133 | 134 | ![三种加法注意事项](../imgs/python_26_16.JPG) 135 | 136 | 18.每个二元运算符都有类似`+`的右侧和原地重载方法。他们以相似的方式工作。 137 | 138 | * 右侧方法通常只有在需要满足交换律时用得到,一般较少使用 139 | * 在实现这些方法时,函数体内注意不要出现递归调用 140 | 141 | 19.`.__call__(self,*pargs,**kwargs)`方法:函数调用方法。当调用实例对象时,由`.__call__(self,*pargs,**kwargs)`方法拦截。 142 | 143 | * `.__call__(self,*pargs,**kwargs)`方法支持所有的参数传递方式 144 | 145 | ![重载函数调用.__call__方法](../imgs/python_26_17.JPG) 146 | 147 | 20.实例对象可以拦截6种比较运算符:`< > <= >= == !=`,对应于 148 | 149 | ``` 150 | .__lt__(self, other) # < 151 | .__le__(self, other) # <= 152 | .__gt__(self, other) # > 153 | .__ge__(self, other) # >= 154 | .__eq__(self, other) # == 155 | .__ne__(self, other) # != 156 | ``` 157 | 158 | * 比较运算符全部是左端形式,无右端形式:`3<=obj`会转换成`obj>=3` 159 | * 比较运算符并没有隐式关系。`==`为真,并不意味着`!=`为假。 160 | 因此`.__eq__(self, other)`与`.__ne__(self, other)`必须同时实现而且语义要一致。 161 | 162 | ![重载比较运算符](../imgs/python_26_18.JPG) 163 | 164 | 21.在布尔环境中,Python会首先尝试`.__bool__(self)`方法来获取一个直接的布尔值。如果没有这个方法,则 尝试`.__len__(self)`方法根据其结果确定实例对象的真值(非0则为真,0为假) 165 | 166 | ![重载.__bool__方法](../imgs/python_26_19.JPG) 167 | 168 | 22.每当实例对象空间被收回时(在垃圾收集时),析构函数`.__del__(self)`自动调用。 169 | >Python的析构函数不常用。原因有二: 170 | > 171 | >* 对于空间管理来说,通常不需要用户手动管理 172 | >* 用户无法预测实例对象的具体回收时机,这个时机有Python自动调度 173 | 174 | ![重载.__del__方法](../imgs/python_26_20.JPG) -------------------------------------------------------------------------------- /chapter/27_python_class_design_pattern.md: -------------------------------------------------------------------------------- 1 | 6 | # 类的设计模式 7 | 1.Python不会执行同名函数的重载,而只会用新对象覆盖旧对象 8 | 9 | ``` 10 | class A: 11 | def func(self,x): 12 | pass 13 | def func(self,x,y): 14 | pass 15 | ``` 16 | 由于`class`、`def`均为可执行代码,因此`func`变量名这里被重新赋值了。 17 | 18 | ![同名函数的覆盖](../imgs/python_27_1.JPG) 19 | 20 | 2.委托设计:在Python中委托通常以拦截`.__getattr__(self,'name')`来实现。该方法会拦截对不存在属性的读取 21 | 22 | * 代理类实例对象可以利用`.__getattr__(self,'name')`将任意的属性读取转发给被包装的对象 23 | * 代理类可以有被包装对象的借口,且自己还可以有其他接口 24 | 25 | ![代理类](../imgs/python_27_2.JPG) 26 | 27 | 3.Python支持变量名压缩的概念:`class`语句内以`__`(两个下划线)开头但是结尾没有`__`(两个下划线)的变量名(如`__x`)会自动扩张为包含所在类的名称(如`_classname__x`) 28 | 29 | * 变量名压缩只发生在`class`语句内,且仅仅针对`__x`这种以`__`开头的变量名 30 | * 该做法常用于避免实例中潜在的变量名冲突 31 | 32 | ![class的变量名压缩](../imgs/python_27_3.JPG) 33 | 34 | 4.Python3中,实例方法有两种形式: 35 | 36 | * 普通函数方法:通过对类名进行点号运算而获得类的函数属性,如`classname.func`,会返回普通函数方法。 37 | * 若调用的是实例方法,必须明确提供实例对象作为第一个参数,如`classname.func(obj,arg)` 38 | * 若调用的是一般的方法,则遵守普通函数调用规则即可`classname.func(arg)` 39 | * 绑定方法:通过对实例对象进行点号运算而获得类的函数属性,如`obj.func`,会返回绑定方法对象。Python在绑定方法对象中,自动将实例和函数打包 40 | * 绑定方法调用时,不需要手动传入实例对象,如`obj.func(arg)` 41 | * 绑定方法的`__self__`属性引用被绑定的实例对象,`__func__`属性引用该类的该函数对象 42 | 43 | ![普通方法和绑定方法](../imgs/python_27_4.JPG) 44 | 45 | 5.多重继承:子类可以继承一个以上的超类。超类在`class`语句首行括号内列出,以逗号分隔 46 | 47 | * 子类与其实例继承了列出的所有超类的命名空间 48 | * 搜索属性时,Python会从左到右搜索`class`首行中的超类,直到找到匹配的名字 49 | 50 | ![多重继承](../imgs/python_27_5.JPG) 51 | 52 | 6.工厂函数:通过传入类对象和初始化参数来产生新的实例对象: 53 | 54 | ``` 55 | def factory(classname,*args,**kwargs): 56 | return classname(*args,**kwargs) 57 | ``` 58 | 59 | ![多重继承](../imgs/python_27_6.JPG) 60 | 61 | 7.抽象超类:类的部分行为未定义,必须由其子类提供 62 | 63 | * 若子类也未定义预期的方法,则Python会引发未定义变量名的异常 64 | * 类的编写者也可以用`assert`语句或者`raise`异常来显式提示这是一个抽象类 65 | 66 | ``` 67 | class A: 68 | def func(self): 69 | self.act() #该方法未实现 70 | def act(self): 71 | assert False, 'act must be defined!' 72 | class ChildA(A): 73 | def act(self): 74 | print('in ChildA act') 75 | x=child() 76 | x.func() 77 | ``` 78 | 79 | 这里的核心在于:超类中`self.act()`调用时,`self`指向的有可能是真实的实例对象(子类对象) 80 | 81 | ![抽象超类](../imgs/python_27_7.JPG) 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /chapter/28_python_class_advanced.md: -------------------------------------------------------------------------------- 1 | # 类的高级主题 2 | 1.通过在自定义类中嵌入内置类型,类似委托;这样自定义类就可以实现内置类型的接口。 3 | 这些接口在内部通过操作嵌入的内置类型来实现。 4 | >这是一种扩展内置类型的方式 5 | 6 | 2.通过子类扩展内置类型:所有内置类型都可以直接创建子类,如`list`、`str`、`dict`、`tuple`、`set`等 7 | 8 | * 内置类型的子类实例可以用于内置类型对象能够出现的任何地方 9 | 10 | 3.在Python3中所有的类都是新式类。 11 | > Python2.2之前的类不是新式类 12 | 13 | * 所有的类都是从`object`内置类派生而来 14 | * `type(obj)`返回对象实例所属的类对象 15 | >对于实例对象的`type(obj)`返回值也是`obj.__class__` 16 | * `type(classname)`返回`"type"`,因为所有`class`对象都是`type`的实例 17 | * 由于所有`class`均直接或者间接地派生自`object`类,因此每个实例对象都是`object`类的实例 18 | * `object`是`type`类的实例,但是同时`type`又派生自`object` 19 | 20 | ![新式类](../imgs/python_28_1.JPG) 21 | 22 | 4.Python3中的类有一个`.__slots__`属性,它是一个字符串列表。这个列表限定了类的实例对象的合法属性名。如果给实例赋了一个`.__slots__`列表之外的属性名会引发异常 23 | 24 | * 虽然有了`.__slots__`列表,但是实例对象的属性还是必须先赋值才存在 25 | 26 | ![.__slots__属性](../imgs/python_28_2.JPG) 27 | 28 | * 当有`.__slots__`列表存在时,默认会删除`.__dict__`属性,而`getattr()`,`setattr()` 29 | 以及`dir()`等函数均使用`.__slots__`属性,因此仍旧可以正常工作 30 | * 可以在`.__slots__`列表中添加`.__dict__`字符串, 31 | 因此对于使用`.__dict__`的地方均能正常工作 32 | 33 | ![.__slots__和 .__dict__](../imgs/python_28_3.JPG) 34 | 35 | * `.__slots__`属性的使用可以优化内存和读取速度 36 | * 在继承中: 37 | * 若子类继承自一个没有`.__slots__`的超类,则超类的`.__dict__`属性可用,则子类中的`.__slots__` 38 | 没有意义。因为子类继承了超类的`.__dict__`属性 39 | 40 | ![.__slots__和 .__dict__](../imgs/python_28_4.JPG) 41 | 42 | * 若子类有`.__slots__`,超类也有`.__slots__`,子类的合法属性名为父类和子类的`.__slots__`列表的并集 43 | 44 | ![超类子类都有`.__slots__`](../imgs/python_28_5.JPG) 45 | 46 | * 若超类有`.__slots__`,子类未定义`.__slots__`,则子类将会有一个`.__dict__`属性 47 | 48 | ![超类有`.__slots__`子类没有](../imgs/python_28_6.JPG) 49 | 50 | 5.Python3的`property`机制: 51 | `property`是一个对象,通过它给类变量名赋值。 52 | 53 | ``` 54 | class A: 55 | age=property(getMethod,setMethod,delMethod,docMethod) 56 | # 或者直接指定docstring 57 | def getMethod(self): 58 | pass 59 | def setMethod(self,val): 60 | pass 61 | def delMethod(self): 62 | pass 63 | def docMethod(self): 64 | ... 65 | # return a string 66 | ``` 67 | 68 | * `property`优点:代码简单,运行速度更快;缺点:当类编写时可能还无法确定`property`名字,因此无法提供动态的接口 69 | * 如果`property`的`docstring`或者`docMethod`为`None`,则Python使用`getMethod`的`docstring`。 70 | 71 | ![property](../imgs/python_28_7.JPG) 72 | 73 | 一个添加了语法糖的方案为: 74 | 75 | ``` 76 | class A: 77 | def __init__(self): 78 | self._x = None 79 | @property #定义了一个property get函数,必选 80 | def x(self): # property name 就是 get函数的函数名 81 | """I'm the 'x' property.""" 82 | return self._x 83 | @x.setter #定义了一个property set函数,可选 84 | def x(self, value): 85 | self._x = value 86 | @x.deleter #定义了一个property del函数,可选 87 | def x(self): 88 | del self._x 89 | ``` 90 | 91 | 6.Python类中有两种特殊的方法:`staticmethod`方法和`classmethod`方法 92 | 93 | * `staticmethod`方法:当以实例对象调用`staticmethod`方法时,Python并不会将实例对象传入作为参数;而普通的实例方法,通过实例对象调用时,Python将实例对象作为第一个参数传入 94 | * 定义`staticmethod`方法: 95 | 96 | ``` 97 | class A: 98 | @staticmethod #定义一个staticmethod 99 | def func(*args,**kwargs) 100 | pass 101 | ``` 102 | * `classmethod`方法:当以实例对象或者类对象调用`classmethod`方法时,Python将类对象(如果是实例对象调用,则提取该实例所属的类对象)传入函数的第一个参数`cls`中 103 | * 定义`classmethod`方法: 104 | 105 | ``` 106 | class A: 107 | @classmethod #classmethod 108 | def func(cls,*args,**kwargs) 109 | pass 110 | ``` 111 | 112 | 总结一下,类中可以定义四种方法: 113 | 114 | * 普通方法:方法就是类对象的一个属性,执行常规函数调用语义`classname.method(args)` 115 | * 实例方法:传入一个实例作为方法的第一个实参。调用时可以: 116 | * `obj.method(args)`:通过实例调用 117 | * `classname.method(obj,args)`:通过类调用 118 | * `staticmethod`方法:* `obj.method(args)`通过实例调用时,执行的是`classname.method(args)`语义 119 | * `classmethod`方法:* `obj.method(args)`执行的是`classname.method(classname,args)`语义 120 | 121 | ![Python类中四种方法](../imgs/python_28_8.JPG) 122 | 123 | 7.类的实例方法中,用哪个实例调用的该方法,`self`就是指向那个实例对象 124 | 类的`classmethod`方法中,用哪个类调用该方法,`cls`就指向那个类对象 125 | 126 | ![实例方法和类方法中的self、cls](../imgs/python_28_9.JPG) 127 | 128 | 8.类对象与实例对象都是可变对象,可以给类属性、实例属性进行赋值,这就是原地修改。这种行为会影响对它的多处引用 129 | 任何在类层次所作的修改都会反映到所有实例对象中 130 | 131 | ![类对象与实例对象都是可变对象](../imgs/python_28_10.JPG) 132 | 133 | 9.若类的某个属性是可变对象(如列表、字典),则对它的修改会立即影响所有的实例对象 134 | 135 | ![类属性为可变对象](../imgs/python_28_11.JPG) 136 | 137 | 10.多重继承中,超类在`class`语句首行内的顺序很重要。Python搜索继承树时总是根据超类的顺序,从左到右搜索超类。 138 | 139 | ![多重继承超类定义顺序](../imgs/python_28_12.JPG) 140 | 141 | 142 | 11.类的`.__mro__`属性:类对象的`.__mro__`属性。它是一个`tuple`,里面存放的是类的实例方法名解析时需要查找的类。Python根据该元组中类的前后顺序进行查找。类对象的`.__mro__`列出了`getattr()`函数以及`super()`函数对实例方法名字解析时的类查找顺序。 143 | 144 | ![类的 __mro__属性](../imgs/python_28_13.JPG) 145 | 146 | 147 | * 类的`.__mro__`是动态的,当继承层次改变时它也随之改变 148 | * 元类可以重写一个类的`.mro()`方法来定义该类的`__.mro__`属性。该方法在类被创建时调用,结果存放在类的`.__mro__`属性中 149 | 150 | 12.`super()`函数:`super()`返回一个`super`实例对象,它用于代理实例方法/类方法的执行 151 | 152 | * `super(class,an_object)`:要求`isinstance(an_object,class)`为真。代理执行了实例方法调用 153 | * `super(class,class2)`:要求 `issubclass(class2,class)`为真。代理执行了类方法调用 154 | 155 | 有两种特殊用法: 156 | 157 | * `super(class)`:返回一个非绑定的`super`对象 158 | * 在类的实例方法中,直接调用`super()`,等价于`super(classname,self)`(这里`self`可能是`classname`子类实例) 159 | * 在类的类方法中,直接调用`super()`,等价于`super(classname,cls)`(这里`cls`可能是`classname`子类) 160 | 161 | 原理:`super`的原理类似于: 162 | 163 | ``` 164 | def super(cls,instance): 165 | mro=instance.__class__.__mro__ #通过 instance生成 mro 166 | return mro[mro.index(cls)+1] #查找cls在当前mro中的index,饭后返回cls的下一个元素 167 | ``` 168 | 169 | 示例: 170 | 171 | ``` 172 | class Root: 173 | def method1(self): 174 | print("this is Root") 175 | class B(Root): 176 | def method1(self): 177 | print("enter B") 178 | print(self) 179 | super(B,self).method1() #也可以简写为 super().method1() 180 | print("leave B") 181 | class C(Root): 182 | def method1(self): 183 | print("enter C") 184 | print(self) 185 | super().method1() #也可以写成super(C,self).method1() 186 | print("leave C") 187 | class D(B,C): 188 | pass 189 | ``` 190 | 191 | * 调用`D().method1()`--> `D`中没有`method1` 192 | * `B`中找到(查找规则:`D.__mro__`) --> 执行`B`中的`method1`。此时`self`为D实例。`D.__mro__`中,`B`的下一个是`C`,因此`super(B,self).method1()`从类`C`中查找`method1`。 193 | * 执行`C`的`method1`。此时`self`为D实例。`D.__mro__`中,`C`的下一个是`Root`,因此`super(C,self).method1()`从类`Root`中查找`method1`。 194 | * 执行`Root`的`method1`。 195 | * `print(self)`可以看到,这里的`self`全部为 `D`的实例 196 | > 类的`classmethod`依次类推 197 | > 198 | > 类、实例的属性查找规则没有那么复杂。因为属性变量只是一个变量,它没办法调用`super(...)`函数。 199 | >只有实例方法和类方法有能力调用`super(...)`函数,才会导致这种规则诞生 200 | 201 | ![super()和self](../imgs/python_28_14.JPG) 202 | 203 | 204 | -------------------------------------------------------------------------------- /chapter/29_python_try_except.md: -------------------------------------------------------------------------------- 1 | 6 | # 异常 7 | 1.Python中,异常会根据错误自动地被触发,也能由代码主动触发和截获 8 | 9 | 2.捕捉异常的代码: 10 | 11 | ``` 12 | try: 13 | statements #该代码执行主要的工作,并有可能引起异常 14 | except ExceptionType1: #except子句定义异常处理,这里捕捉特定的ExceptionType1类型的异常 15 | statements 16 | except (ExceptionType2,ExceptionType3): #except子句定义异常处理, 17 | #这里捕捉任何列出的异常(即只要是ExceptionType2类型或者ExceptionType3类型) 18 | statements 19 | except ExceptionType4 as excp: #这里捕捉特定的ExceptionType4类型异常,但是用变量名excp引用异常对象 20 | statements #这里可以使用excp引用捕捉的异常对象 21 | except: # 该子句捕获所有异常 22 | statements 23 | else: #如果没有发生异常,这来到这里;当发生了异常则不执行else子句 24 | statements 25 | ``` 26 | 27 | * 当`try`子句执行时发生异常,则Python会执行第一个匹配该异常的`except`子句。当`except`子句执行完毕之后(除非该`except`子句 又引发了另一个异常),程序会跳转到整体语句之后执行。 28 | >整体语句就是指上面的`try..except..else` 29 | * 如果异常发生在`try`代码块内,且无匹配的`except`子句,则异常向上传递到本`try`块外层的`try`块中。如果已经传递到了顶层了异常还没有被捕捉,则Python会终止程序并且打印默认的出错消息 30 | * 如果`try`代码块内语句未产生异常,则Python会执行`else`子句,然后程序会在整体语句之后继续执行 31 | 32 | ![try..except..else语句](../imgs/python_29_1.JPG) 33 | 34 | 3.`try/finally`语句: 35 | 36 | ``` 37 | try: 38 | statements 39 | finally: 40 | statements 41 | ``` 42 | 无论`try`代码块执行时是否发生了异常,`finally`子句一定会被执行 43 | 44 | * 若`try`子句无异常,则Python会接着执行`finally`子句,执行完之后程序会跳转到整体语句之后执行 45 | * 若`try`子句有异常,则Python会跳转到`finally`子句中,并接着把异常向上传递 46 | 47 | ![try..finally语句](../imgs/python_29_2.JPG) 48 | 49 | 4.Python中的`try|except|finally`统一格式: 50 | 51 | ``` 52 | try: 53 | statements #该代码执行主要的工作,并有可能引起异常 54 | except ExceptionType1: #except子句定义异常处理,这里捕捉特定的ExceptionType1类型的异常 55 | statements 56 | except (ExceptionType2,ExceptionType3): #except子句定义异常处理, 57 | #这里捕捉任何列出的异常(即只要是ExceptionType2类型或者ExceptionType3类型) 58 | statements 59 | except ExceptionType4 as excp: #这里捕捉特定的ExceptionType4类型异常,但是用变量名excp引用异常对象 60 | statements #这里可以使用excp引用捕捉的异常对象 61 | except: # 该子句捕获所有异常 62 | statements 63 | else: # 如果没有发生异常,这来到这里;当发生了异常则不执行else子句 64 | statements 65 | finally: # 一定会执行这个子句 66 | statements 67 | ``` 68 | * `else`、`finally`子句可选;`except`子句可能有0个或者多个。但是如果有`else`子句,则至少有一个`except` 69 | * `finally`执行时机:无论有没有异常抛出,在程序跳出整体语句之前的最后时刻一定会执行 70 | >整体语句就是指上面的`try..except..else...finally` 71 | 72 | ![try..except...finally语句](../imgs/python_29_3.JPG) 73 | 74 | 5.要显式触发异常,可以用`raise`语句。有三种形式的形式: 75 | 76 | * `raise exception_obj`:抛出一个异常实例 77 | * `raise Exception_type`:抛出一个指定异常类型的实例,调用`Exception_type()`获得 78 | * `raise from `: 79 | 第二个异常实例会附加到第一个异常实例的`.__cause__`属性中并抛出第一个异常实例 80 | * `raise`:转发当前作用域中激活的异常实例。若当前作用域中没有激活的异常实例,则抛出`RuntimeError`实例对象 81 | 82 | * 一旦异常在程序中由某个`except`子句捕获,则它就死掉了不会再传递 83 | * `raise`抛出的必须是一个`BaseException`实例或者`BaseException`子类,否则抛出`TypeError` 84 | >`BaseException`类是所有内建异常的父类。 85 | > 86 | >`Exception`类是所有内建异常、`non-system-exiting`异常的父类。用于自定义的异常类也应该从该类派生 87 | 88 | ![raise语句](../imgs/python_29_4.JPG) 89 | 90 | ![异常的死亡与活跃](../imgs/python_29_5.JPG) 91 | 92 | 6.在一个异常处理器内部`raise`一个异常时,前一个异常会附加到新异常的`__context__`属性 93 | 94 | * 如果在异常处理器内部`raise`被捕获的异常自己,则并不会添加到`__context__`属性 95 | 96 | ![异常处理器内的raise](../imgs/python_29_6.JPG) 97 | 98 | * 在异常处理器内部`raise`与`raise e`效果相同 99 | 100 | ![raise与raise e](../imgs/python_29_7.JPG) 101 | 102 | 7.`assert`语句可能会引起`AssertionError`。其用法为:`assert ,`。这等价于: 103 | 104 | ``` 105 | if __debug__: 106 | if not : 107 | raise AssertionError() 108 | ``` 109 | 110 | * ``表达式用于计算真假,``表达式用于作为异常的参数。若``计算为假,则抛出`AssertionError` 111 | * 若执行时用命令行 `-0`标志位,则关闭`assert`功能(默认是打开的)。 112 | > `__debug__`是内置变量名。当有`-0`标志位时,它为0;否则为1 113 | * 通常`assert`用于给定约束条件,而不是用于捕捉程序的错误。 114 | 115 | ![assert](../imgs/python_29_8.JPG) 116 | 117 | 8.Python3中有一种新的异常相关语句:`with/as`语句。它是作为`try/finally`的替代方案。用法为: 118 | 119 | ``` 120 | with expression [as var]: 121 | statements 122 | ``` 123 | `expression`必须返回一个对象,该对象必须支持环境管理协议。其工作方式为: 124 | 125 | * 计算`expression`表达式的值,得到环境管理器对象。环境管理器对象必须有`.__enter__(self)`方法和`.__exit__(self, exc_type, exc_value, traceback)`方法 126 | * 调用环境管理器对象的`.__enter__(self)`方法。如果有`as`子句,`.__enter__(self)`方法返回值赋值给`as`子句中的变量`var`;如果没有`as`子句,则`.__enter__(self)`方法返回值直接丢弃。**并不是将环境管理器对象赋值给`var`** 127 | * 执行`statements`代码块 128 | * 如果`statements`代码块抛出异常,则`.__exit__(self, exc_type, exc_value, traceback)`方法自动被调用 129 | >在内部这几个实参由`sys.exc_info()`返回`(exc_type, exc_value, traceback)`信息, 130 | 131 | * 若`.__exit__()`方法返回值为`False`,则重新抛出异常到`with`语句之外 132 | * 若`.__exit__()`方法返回值为`True`,则异常终止于此,并不会抛出`with`语句之外 133 | * 如果`statements`代码块未抛出异常,则`.__exit__(self, exc_type, exc_value, traceback)`方法自动被调用,调用参数为:`.__exit__(self,None,None,None)` 134 | 135 | ![with语句](../imgs/python_29_9.JPG) 136 | 137 | 9.Python3.1之后,`with`语句可以指定多个环境管理器,以逗号分隔。根据定义的顺序这些环境管理器对象的`.__enter__(self)`方法顺序调用,`.__exit__(self, exc_type, exc_value, traceback)`方法逆序调用 138 | >如果对象要支持环境管理协议,则必须实现`.__enter__(self)`方法和`.__exit__(self, exc_type, exc_value, traceback)`方法 139 | 140 | ![多个with](../imgs/python_29_10.JPG) 141 | 142 | 143 | -------------------------------------------------------------------------------- /chapter/2_python_number_type.md: -------------------------------------------------------------------------------- 1 | 6 | # 数字类型 7 | 1.Python的数字常量: 8 | 9 | * 整数:`1234`,`-1234`,`0`。Python支持无穷整数大小 10 | >Python3中,整数不再区分一般整数与长整数 11 | > 12 | >Python2.7中整数分为一般整数(32位)与长整数(支持无穷整数大小)。 13 | 整数以`l`或者`L`结尾时为长整数。当整数值超过32位时自动转换为长整数 14 | 15 | * 浮点数:`1.23`,`1.`,`.3`,`3.14e-10`,`4E10`,`4.0e+10` 16 | * 八/十六/二进制整数:`0o177`(小写的字符`o`或者大写的`O`),`0x9ff`,`0b101010` 17 | > 对于Python2.7来讲,八进制整数为`0177`(没有小写的字符`o`) 18 | * 复数:`3+4j`,`3.0+4.0j`,`3j`,`3J` 19 | 20 | 2.数字类型转换: 21 | 22 | * `hex(intx)`、`oct(intx)`、`bin(intx)`、`str(intx)`将整数`intx`转换成十六/八/二/十进制表示的字符串 23 | * `int(strx,base)`将字符串`strx`根据指定的`base`进制转换成整数。`base`默认为10 24 | * `float(strx)`将字符串`strx`转换成浮点数 25 | * `complex(num_real,num_imag)`创建一个复数,实部为数字`num_real`, 26 | 虚部为数字`num_imag` 27 | ![数字类型转换](../imgs/python_2_1.JPG) 28 | 29 | 3.混合类型表达式中,Python先将被操作对象转换成其中最复杂的操作对象的类型。 30 | 31 | * 整数与浮点数混合操作时,将整数自动转换成浮点数 32 | * 浮点数与复数混合操作时,将浮点数自动转换成复数 33 | >也可以通过`int()`,`float()`,以及`complex()`执行手动转换 34 | 35 | 4.Python允许执行连续比较,且比较链可以任意长: 36 | 37 | * `ac`结果等同于`ac` 39 | > 这二者并不完全等价,因为`a 即使是浮点的Floor除法,结果也是取整的浮点数 49 | 50 | ![除法和Floor除法](../imgs/python_2_2.JPG) 51 | 52 | 6.将字符串转为整数除了用`int()`函数外,也可以通过`eval()`函数将字符串转为整数; 53 | 整数转字符串除了用`str()/hex()`等函数外,也可以用格式化字符串。 54 | ![字符串和整数的转换](../imgs/python_2_3.JPG) 55 | 56 | 7.Python支持将整数当作二进制位串对待的位操作。 57 | ![整数的按位异或](../imgs/python_2_4.JPG) 58 | 59 | 8.Python支持许多对数字处理的内置函数与内置模块: 60 | > 内置函数位于一个隐性的命名空间内,对应于`builtins`模块(python2.7叫做 61 | `__builtins__`模块) 62 | 63 | * `math`模块:如`math.pi`,`math.e`,`math.sqrt`.... 64 | * 内置函数,如`pow()`,`abs()`,... 65 | ![数学模块与数学函数](../imgs/python_2_5.JPG) 66 | 67 | 9.`random`模块提供的工具可以生成0~1之间的随机浮点数、两个数字之间的任意整数、 68 | 序列中的任意一项。 69 | ![random模块](../imgs/python_2_6.JPG) 70 | 71 | 10.浮点数缺乏精确性,因为存储浮点数的空间有限(这是硬件相关的内存缺陷), 72 | 可以用`Decimal`对象解决精度问题。 73 | `Decimal`对象来自于`decimal`模块,它类似于浮点数,但是有固定的位数和小数点, 74 | 因此是固定精度的小数。相对于浮点数,它带来了微小的精度损失。 75 | 76 | * 设置全局的精度和舍入模式:`decimal.getcontext().prec=5`,将全局精度设置为小数点后5位 77 | * 可用整数、浮点数、字符串初始化`Decimal`对象 78 | ![Decimal对象](../imgs/python_2_7.JPG) 79 | 80 | 11.Python中的分数类型是`Fraction`对象,来自于`fractions`模块。 81 | 82 | * `Fraction`对象以一个分子,一个分母初始化:`Fraction(1,4)`; 83 | 也可以从浮点数或者浮点数的字符串初始化:`Fraction('0.25')`,`Fraction(0.25)` 84 | * `Fraction`对象能保证精确性,且能自动简化结果 85 | * 从浮点数产生分数有两个方法: 86 | * 调用`Fraction`的构造函数:`Fraction(0.25)` 87 | * 用`Fraction.from_float()`函数:`Fraction.from_float(0.25)` 88 | 89 | 从浮点产生分数时,可以通过`.limit_denominator()`限制分母的最大值。 90 | ![Fraction对象](../imgs/python_2_8.JPG) 91 | 92 | 12.浮点数有个`as_integer_ratio()`方法,能生成`(分子,分母)`的元祖。 93 | ![浮点的元祖表示](../imgs/python_2_9.JPG) 94 | 95 | 13.整数、浮点数、`Fraction`、`Decimal` 可以混合运算 96 | > `Fraction`与`Decimal`不可以混合运算 97 | 98 | ~~~mermaid 99 | graph LR 100 | id1(整数)-->|提升|id2(浮点数); 101 | id2(浮点数)-->|提升|id3(Fraction); 102 | id2(浮点数)-->|提升|id4(Decimal); 103 | ~~~ 104 | 105 | 14.Python中的布尔类型为`bool`,它只有两个值`True`和`False`。 106 | `True`和`False`是预定义的内置变量名,其在表达式中的行为与整数1和0是一样的。实际上他们就是内置的`int`类型的子类。 107 | ![布尔类型的整数行为](../imgs/python_2_10.JPG) 108 | 109 | 15.`真`和`假`是Python中每个对象的固有属性:每个对象不是`真`就是`假`。 110 | 该属性可以用于`任何需要`bool`值的地方。 111 | 112 | * 一个数如果不是0,则为真;如整数 0,浮点数 0.0 都是 `假` 113 | * 其他对象如果非空,则为真;如空字符串 `""`为`假`,空字典`{}`为`假`, 114 | 空列表`[]`为`假`,`None`对象为`假` 115 | 116 | 117 | 118 | -------------------------------------------------------------------------------- /chapter/30_python_exception_type.md: -------------------------------------------------------------------------------- 1 | 6 | # 异常对象 7 | 1.Python3中,内置异常与用户自定义异常都是类的实例对象 8 | 9 | 2.在`try...except`语句进行`except ExceptionType`子句匹配时,采用的是`isinstance(exception_obj,ExceptionType)`这种匹配规则。因此如果`ExceptionType`是`exception_obj`所属类的超类,则匹配也成功。 10 | 11 | 3.Python中的内置异常类继承树: 12 | 13 | ~~~mermaid 14 | graph BT 15 | id1(OverflowError) -->|继承|id2(ArithmeticError) 16 | id2 -->|继承|id3(Exception) 17 | id4(IndexError) -->|继承|id3 18 | id3-->|继承|id5(BaseException) 19 | ~~~ 20 | 21 | * 用户自定义异常类不要直接从`BaseException`继承。`BaseException`提供了默认的打印和状态保持行为 22 | * 在构造时传给异常类的所有参数都将作为一个元组存储在`.args`属性中 23 | * 在构造时传入的字符串作为`.__str(self)__`方法返回。如果传入的不是字符串, 24 | 则将先调用`str()`将该参数转换为字符串 25 | * `Exception`是所有内置异常类的超类。用户自定义的异常类都继承自它 26 | * 系统退出事件`SystemExit`、`KeyboardInterrupt`、`GeneratorExit`不能继承自它 27 | 28 | ![BaseException](../imgs/python_30_1.JPG) 29 | 30 | 4.自定义异常类:通常继承自`Exception`类 31 | 32 | * 若想自定义打印显示,则必须重写`.__str__(self)`方法 33 | * 如果想定制初始化方法,必须重写`.__init__(self,args)`方法。此时超类的`.args`属性同样也会起作用 34 | 35 | ![用户的自定义异常类](../imgs/python_30_2.JPG) 36 | 37 | 5.Python在运行时会将`try`语句放入堆栈中。抛出异常时,Python跳转至最近的`try`块中,找到匹配该异常的异常处理器(即`except子句`),执行异常处理的`except`子句。一旦异常被捕获并且处理,则其生命周期结束 38 | 39 | * 异常的传递:向上返回到先前进入但是尚未离开的`try` 40 | 41 | 6.Python中所有的错误都是异常。但是并非所有的异常都是错误 42 | 43 | * 内置的`input`函数每次调用时,遇到文件末尾时引发内置的`EOFError` 44 | * 调用`sys.exit()`会触发`SystemExit`异常 45 | * 在键盘上按下`Ctrl-C`键时,会触发`KeyboardInterrupt`异常 46 | 47 | ![系统级的异常事件](../imgs/python_30_3.JPG) 48 | 49 | 7.用户自定义的异常可以用于触发信号条件。这是利用异常来传递信息的方法 50 | 51 | 8.`try...finally`通常用于释放系统资源。虽然垃圾收集时资源会自动释放,但是垃圾收集的时机不可控,由算法自动调度 52 | 53 | 9.可以在顶层代码中使用`try`以及空的`except`来进行调试,从捕获程序有什么意外情况发生 54 | 55 | 10.`sys.exc_info()`函数返回最近引发的异常信息,它返回一个三元素的元组:`(type,value,traceback)` 56 | 57 | * `type`:异常类型 58 | * `value`:异常实例对象 59 | * `traceback`:一个`traceback`对象,代表异常发生时所调用的堆栈 60 | 61 | ![sys.exc_info函数](../imgs/python_30_4.JPG) 62 | 63 | 11.为了拦截具体的异常,`except `应该具体化,避免拦截无关事件 64 | 65 | * 空的`except`子句拦截任何异常,包括内存错误、系统推出、键盘中断等等 66 | * 但是太具体化不利于扩展 67 | 68 | -------------------------------------------------------------------------------- /chapter/31_python_unicode_bytes.md: -------------------------------------------------------------------------------- 1 | 6 | # Unicode与字节串 7 | 1.字符编码: 8 | 9 | * `ASCII`编码:每个字符存储在一个字节中。字符码值从 0~127 10 | * `Latin-1`编码:每个字符存储在一个字节中。字符码值从 0~255。其中码值128~255分配给特殊字符;码值0~127部分与 11 | `ASCII`编码相同。 12 | * `UTF-8`编码:每个字符存储的字节数量可变,不再是固定编码 13 | > `ASCII`编码是`UTF-8`编码的子集,也是`Latin-1`编码的子集 14 | 15 | 2.`Unicode`文本通常叫做“宽字符”字符串,因为每个字符可能表示为多个字节 16 | 17 | 3.查看字符的`Unicode`码值: `ord()`函数; 18 | 查看`Unicode`码值对应的字符:`chr()`函数 19 | >这里的`Unicode`码值都是整数,可以是十进制、二进制、八进制、十六进制整数等 20 | 21 | ![字符与Unicode码值的转换](../imgs/python_31_1.JPG) 22 | 23 | 4.Python大约支持上百中不同的编码。可以导入`encodings`模块,并运行`help(encodings)`显示很多编码名称。有一些编码是Python中实现的,一些是C中实现的。 24 | >有些编码对应多个不同的名称 25 | 26 | ![encodings模块](../imgs/python_31_2.JPG) 27 | 28 | 5.Python3中有三种字符串相关类型: 29 | 30 | * `str`类型表示`Unicode`文本(8位和更宽的位数),为不可变的字符序列,称为字符串 31 | * `bytes`表示二进制数据,称为字节串。`bytes`对象其实是小整数的一个序列,每个整数的范围在0~255之间。 32 | * 索引一个`bytes`实例返回一个整数 33 | * 分片一个`bytes`实例返回一个新的`bytes`实例 34 | * `list(bytes_obj)`返回一个整数列表而不是字符列表 35 | > `bytes`类型几乎支持所有的`str`操作,但是不支持字符串格式化操作(没有字节串格式化操作) 36 | 37 | * `bytearray`是一种可变的`bytes`类型,称为可变字节串。`bytearray`是`bytes`的一个变体, 38 | 它是可变的且支持原地修改。它支持`str`与`bytes`的常见操作,以及与列表相同的一些原地修改操作。 39 | 40 | ![str、bytes、bytearray](../imgs/python_31_3.JPG) 41 | 42 | 6.`sys.getdefaultencoding()`函数返回平台默认的编码方式。`sys.getfilesystemencoding()`返回系统文件的默认编码方式。 43 | 44 | ![系统默认编码](../imgs/python_31_4.JPG) 45 | 46 | 7.Python3中,当一个文件以文本模式打开时,读取其数据会自动将其内容解码,并返回一个字符串;当一个文件以文本模式写打开时,写入一个字符串会在将该字符串写入文件之前自动编码它。 47 | 48 | * 编码和解码的类型是系统的平台默认编码类型,或者你手动设定的编码类型 49 | * 根据编码类型,Python自动处理文件起始处的字节标记序列(通常用于标记文件编码类型) 50 | * Python自动对行尾换行符转换。在windows下,换行符`\n`在写入文件时转换为windows下的换行符`\r\n`。在读取文件时windows下的换行符`\r\n`转换为标准换行符`\n` 51 | 52 | 当一个文件以二进制模式打开时,读取其数据直接返回其原生内容而并不以任何方式解码,也不做任何方式修改(即不转换换行符),直接作为`bytes`实例返回;写入会接受一个`bytes`实例或者一个`bytearray`实例,并且不加修改地写入到文件(即不转换换行符) 53 | 54 | ![文件和编码](../imgs/python_31_5.JPG) 55 | 56 | 8.在Python3中,`'xxx'`、 `"xxx"` 、`'''xxx'''`均创建一个字符串常量,而添加一个`b`或者`B`中创建一个`bytes`常量`b'xxx'`、 `B"xxx"` 、`b'''xxx'''` 57 | 58 | * 尽管`bytes`打印出来是字符串(若无对应的字符则输出内存表示),但它本质上是一个小整数序列 59 | * Python3中所有字符串都是Unicode字符(是ASCII字符的超集) 60 | 61 | ![bytes常量](../imgs/python_31_6.JPG) 62 | 63 | 9.Python3中,虽然字符串与`bytes`的内存表示相同,但是二者不能混用,因为二者无法自动转换。对于期待一个`str`实例作为参数的函数,它不能接受一个`bytes`实例;反之亦然 64 | 65 | * 字符串的`.encode(encoding)`方法和`bytes`的`bytes(a_string,encoding)`函数将一个字符串实例转换为它原生`bytes`形式 66 | * 字符串的`str(a_bytes,encoding)`函数和`bytes`的`.decode(encoding)`方法将一个`bytes`实例解码成字符串形式。 67 | 68 | >字符串的`.encode(encoding)`方法的`encoding`参数可以为空,此时表示使用平台默认编码 69 | 70 | >`str(a_bytes)`函数返回的是`bytes`实例的打印字符串,而不是执行编码转换过程 71 | 72 | ![bytes和字符串的编解码](../imgs/python_31_7.JPG) 73 | 74 | 10.Python的字符串常量支持: 75 | 76 | * `'\xNN'`:单字节字符(8位),等价于`\u00NN` 77 | * `'\uNNNN'`:双字节字符,16位 78 | * `'\UNNNNNNNN'`:4字节字符,32位 79 | >这里的`N`均为十六进制整数的一个整数位[0~F] 80 | 81 | ![单字节字符与多字节字符](../imgs/python_31_8.JPG) 82 | 83 | 11.编解码ASCII字符非常简单,无需显示指定编解码类型(当然你可以随意指定一个编解码类型,因为ASCII编码是任何编码类型的子集) 84 | 85 | ![ASCII字符编码](../imgs/python_31_9.JPG) 86 | 87 | 编解码非ASCII字符则要注意,对该字符的编码类型必须与解码类型一致,否则乱码或者抛出`UnicodeDecodeError`。 88 | 89 | ![ASCII字符编码](../imgs/python_31_10.JPG) 90 | 91 | 12.生成Unicode字符串你可以通过Unicode转义序列来创建,如`'A\u4e2d\u56fd'`;也可以通过`chr()`函数来创建,如 92 | `'A'+chr(0x4e2d)+chr(0x56fd)`,最终结果都是`'A中国'` 93 | 94 | 13.字符串常量与`bytes`常量区别: 95 | 96 | * 对字符串常量,`'\xNN'`与`'\u00NN'`是等价的;对`bytes`常量,`b'\xNN'`与`b'\u00NN'`是不等的 97 | >`b'\xE8'`长度为2字节,`b'\u00E8'`长度为6字节 98 | * 字符串常量可以包含任意字符;`bytes`常量要求字符要么是ASCII字符,要么是转义字符 99 | * `len(string_literal)`得到字符串常量的字符个数;`len(bytes_literal)`得到`bytes`常量的字节数 100 | 101 | ![字符串常量与bytes常量](../imgs/python_31_11.JPG) 102 | 103 | 14.指定Python源文件字符集编码声明:在脚本的第一行写入: 104 | 105 | ``` 106 | # -*- coding: latin-1 -*- 107 | ``` 108 | 109 | 15.`bytes`实例的构造: 110 | 111 | * `b'abc'`:构造`bytes`常量 112 | * `bytes('abc',encoding='ascii')`:通过构造函数传入字符串和编码构造 113 | * `bytes([97,98,99])`:通过传入小整数可迭代对象构造 114 | * `'abc'.encode('ascii')`:从字符串编码获取 115 | 116 | 16.`bytearray`实例的构造: 117 | 118 | * `bytearray('abc',encoding='ascii')`:通过构造函数传入字符串和编码构造 119 | * `bytearray(b'abc')`:通过`bytes`常量构造 120 | 121 | ![bytes构造与bytearray构造](../imgs/python_31_12.JPG) 122 | 123 | 17.打开文件时,可以通过`encoding`关键字参数指定打开文件的编码方式 124 | 125 | 18.Python的`struct`模块可以从字符串创建和提取打包的`bytes` 126 | 127 | 19.`pickle`模块存储`pickle`化的对象用的是`bytes` 128 | 129 | -------------------------------------------------------------------------------- /chapter/32_python_attribute_management.md: -------------------------------------------------------------------------------- 1 | 6 | # 管理属性 7 | 1.管理属性的工具 8 | 9 | * `.__getattr__(self,name)`方法:拦截所有未定义属性的读取(它要么返回一个值,要么抛出`AttributeError`异常;`.__setattr__(self,name,value)`方法:拦截所有属性的读取赋值(包括未定义的、已定义的) 10 | * `.__getattribute__(self,name)`方法:拦截所有属性的读取(包括未定义的、已定义的) 11 | * `property`特性:将特定属性访问定位到`get`方法和`set`方法 12 | * 描述符协议:将特定属性访问定位到具有任意`get`和`set`方法的实例对象 13 | 14 | 2.`property`:每个`property`管理一个单一的、特定的属性。用法为: 15 | 16 | ``` 17 | class A: 18 | def fget(...): 19 | pass 20 | def fset(...): 21 | pass 22 | def fdel(...): 23 | pass 24 | attribute=property(fget,fset,fdel,"doc") #必须在fget,fset,fdel之后定义 25 | a=A() 26 | a.attribute #调用的是property特性 27 | ``` 28 | 29 | * `property()`函数返回的是一个`property`对象 30 | * 子类继承了超类的`property`,就和类的普通属性一样 31 | 32 | ![property](../imgs/python_32_1.JPG) 33 | 34 | 3.描述符:描述符是作为独立的类创建,它的实例是赋值给了类属性 35 | 36 | * 描述符的实例可以由子类继承 37 | * 描述符的实例管理一个单一的特定的属性 38 | * 从技术上讲,`property()`创建的是一个描述符实例(`property`实例) 39 | * 描述符实例针对想要拦截的属性名访问操作,它提供了特定的方法 40 | 41 | 描述符类的接口为(即描述符协议): 42 | 43 | ``` 44 | class Descriptor: 45 | ''' 46 | This is docstring 47 | ''' 48 | def __get__(self,instance,owner): 49 | pass 50 | def __set__(self,instance,value): 51 | pass 52 | def __delete__(self,instance): 53 | pass 54 | class A: 55 | attr=Descriptor() 56 | ... 57 | ``` 58 | 59 | * `instance`参数为: 60 | * `None`:当用于类的属性访问时(如`cls.attr`) 61 | * 类`A`的实例对象:当用于实例的属性访问时(如`instance.attr`) 62 | * `owner`参数为:使用该描述符的类`A` 63 | * 当访问类实例或者类属性时,自动调用该类的描述符实例的方法。如果该类的描述符中某些方法空缺则: 64 | * 若` __set__(self,instance,value)`未定义,则写该属性抛出`AttributeError`,该属性只读 65 | * 若` __get__(self,instance,owner)`未定义,则读该属性返回一个`Descriptor`实例, 66 | 因为从继承树中可知,该属性返回由类的`attr`变量名指定的对象 67 | * 状态信息可以保持在实例对象中,也可以保存在描述符实例中。因为在这3个方法中,`self`,`instance`都可以访问 68 | 69 | ![描述符属性](../imgs/python_32_2.JPG) 70 | 71 | 4.`.__delattr__(self,name)`方法拦截属性的删除 72 | > `delattr(x,'name')`删除了`x.name`属性 73 | 74 | 5.由于`.__getattribute__(self,name)`方法和`.__setattr__(self,name,value)`方法对所有的属性拦截,因此他们的实现特别要小心,注意不要触发无穷递归。 75 | 76 | * `.__getattribute__(self,name)`方法中,若要取属性则可以用超类的`.__getattribute__(self,name)`获取。如果通过`.__dict__`方法获取则会再次触发`.__getattribute__(self,name)`的调用 77 | * `.__setattr__(self,name,value)`方法中,若要设置属性可以用`self.__dict__[name]=value`的方法,或者用超类的`.__setattr__(self,name,value)`方法 78 | 79 | ![__getattribute__与__setattr__陷阱](../imgs/python_32_3.JPG) 80 | 81 | 6.Python3中,所有使用内置操作隐式的获取方法名属性(如`print(x)`用到了`.__str__(self)`),`.__getattr__(self,name)`、`.__setattr__(self,name,value)`、 82 | `.__getattribute__(self,name)`方法都不会拦截,因为Python在类中查找这样的属性,完全忽略了在实例中查找 83 | 84 | ![__getattribute__与__setattr__陷阱](../imgs/python_32_4.JPG) 85 | 86 | 7.属性拦截优先级: 87 | 88 | * 在读取属性方面,`__getattribute__`优先级最高;在写属性方面,`__setattr__`优先级最高;在删除属性方面, 89 | `__del__`优先级最高 90 | 91 | ![__getattribute__优先级最高](../imgs/python_32_5.JPG) 92 | 93 | * 如果没有`__getattribute__`,`__setattr__`与`__del__`,则读写删属性取决于描述符(`property`也是一种特殊的描述符)。其中如果同一个属性指定了多个描述符,则后面的描述符覆盖前面的描述符 94 | >因为本质上`property`是一种`descriptor` 95 | 96 | ![__getattribute__优先级最高](../imgs/python_32_6.JPG) 97 | 98 | * `__getattribute__`与`__getattr__`区别:`__getattribute__`在任何属性读取的时候拦截,而`__getattr__`只有在未定义属性读取的时候拦截(约定俗成地,它要么返回一个值,要么返回`AttributeError`)。其中若二者同时存在则`__getattribute__`优先级较高 99 | 100 | ![__getattribute__与__getattr__区别](../imgs/python_32_7.JPG) 101 | -------------------------------------------------------------------------------- /chapter/33_python_decorator.md: -------------------------------------------------------------------------------- 1 | 6 | # 装饰器 7 | 1.装饰器是用于包装其他可调用对象的一个可调用对象,它是一个可调用对象,其调用参数为另一个可调用对象<,它返回一个可调用对象 8 | 9 | * 一个函数对象是可调用对象。 10 | * 一个类对象是可调用对象,对它调用的结果就是返回类的实例 11 | * 实现了`.__call__()`方法的类,其实例对象是可调用对象,对它调用的结果就是调用`.__call__()`方法 12 | 13 | ![可调用对象](../imgs/python_33_1.JPG) 14 | 15 | 装饰器有两种使用形式: 16 | 17 | * 函数的装饰器:在函数对象定义的时候使用装饰器,用于管理该函数对象 18 | * 类的装饰器:在类定义的时候使用该装饰器,用于管理该类以及类的实例 19 | 20 | >装饰器是装饰器模式的一个实现 21 | 22 | 2.函数的装饰器:用于管理函数。函数的装饰器声明为: 23 | 24 | ``` 25 | @decorator 26 | def func(*pargs,**kwargs): 27 | pass 28 | ``` 29 | 即在正常的函数定义之前冠以`@decorator`说明符(即装饰器声明)。它等价于: 30 | 31 | ``` 32 | def func(*pargs,**kwargs): 33 | pass 34 | func=decorator(func) 35 | ``` 36 | 37 | * 类中的`@staticmethod`、`@classmethod`、`@property`均为装饰器 38 | * 执行了装饰器的`def`之后,函数名指向的不再是原来的函数对象,而是: 39 | * 一个可调用对象, 当`decorator`是个函数时由`decorator(func)`函数返回的 40 | * `decorator`类的实例,当`decorator`是个类时,由`decorator(func)`构造方法返回 41 | 42 | ![函数的装饰器](../imgs/python_33_2.JPG) 43 | 44 | 3.类的装饰器:用于管理类。类的装饰器声明为: 45 | 46 | ``` 47 | @decorator 48 | class A: 49 | pass 50 | ``` 51 | 即在正常的类定义之前冠以`@decorator`说明符(即装饰器声明)。它等价于: 52 | 53 | ``` 54 | class A: 55 | pass 56 | A=decorator(A) 57 | ``` 58 | 59 | * 类的装饰器并不是拦截创建实例的函数调用,而是返回一个不同的可调用对象 60 | * 执行了装饰器的`class`之后,类名指向的不再是原来的类对象,而是: 61 | * 一个可调用对象, 当`decorator`是个函数时由`decorator(func)`函数返回的 62 | * `decorator`类的实例,当`decorator`是个类时,由`decorator(func)`构造方法返回 63 | 64 | ![类的装饰器](../imgs/python_33_3.JPG) 65 | 66 | 3.装饰器只是一个返回可调用对象的可调用对象,它没有什么特殊的地方。 67 | 68 | * 可以用函数实现装饰器: 69 | 70 | ``` 71 | def decorator(func): #定义了一个叫decorator的装饰器 72 | #某些处理 73 | return func #返回可调用对象 74 | ``` 75 | 76 | * 也可以用类来实现装饰器: 77 | 78 | ``` 79 | class decorator: 80 | def __init__(self,func): 81 | self.func=func 82 | def __call__(self,*args,**kwargs): 83 | return self.func 84 | ``` 85 | 86 | * 通常用嵌套类来实现装饰器: 87 | 88 | ``` 89 | def decorator(func): #定义了一个叫decorator的装饰器 90 | def wrapper(*args): 91 | #使用func或其他的一些工作 92 | return wrapper #返回可调用对象 93 | ``` 94 | 95 | ![三种装饰器的实现](../imgs/python_33_4.JPG) 96 | 97 | 4.装饰器的嵌套: 98 | 99 | * 函数的装饰器的嵌套: 100 | 101 | ``` 102 | @decoratorA 103 | @decoratorB 104 | @decoratorC 105 | def func(): 106 | pass 107 | ``` 108 | 等价于 109 | 110 | ``` 111 | def f(): 112 | pass 113 | f=A(B(C(f))) 114 | ``` 115 | * 类的装饰器的嵌套: 116 | 117 | ``` 118 | @decoratorA 119 | @decoratorB 120 | @decoratorC 121 | class M: 122 | pass 123 | ``` 124 | 等价于 125 | 126 | ``` 127 | class M: 128 | pass 129 | M=A(B(C(M))) 130 | ``` 131 | >每个装饰器处理前一个装饰器返回的结果,并返回一个可调用对象 132 | 133 | 5.装饰器可以携带参数。 134 | 135 | * 函数定义的装饰器带参数:它其实是一个嵌套函数。 136 | * 外层函数的参数为装饰器参数,返回一个函数(内层函数) 137 | * 内层函数的参数为`func`,返回一个可调用参数,内层函数才是真正的装饰器 138 | 139 | ``` 140 | def decorator(*args,**kwargs): 141 | print("this is decorator1:",args,kwargs) 142 | def actualDecorator(func): # 这才是真实的装饰器 143 | ... 144 | return func 145 | return actualDecorator 146 | ``` 147 | 148 | ![带参数的函数定义的装饰器](../imgs/python_33_5.JPG) 149 | 150 | * 类定义的装饰器带参数:它其实是一个嵌套类。 151 | * 外层类的初始化函数的参数为装饰器参数,外层类的`__call__`函数的参数为`func`,返回值为一个类的实例(内部类实例) 152 | * 内层类的初始化函数参数为`func`;内层类的`__call__`函数使用`func`,内层类才是真正的装饰器 153 | 154 | ``` 155 | class decorator2: 156 | class ActualDecorator: #这才是真实的装饰器 157 | def __init__(self,func): 158 | ... 159 | self.func=func#记住func 160 | def __call__(self,*args,**kwargs): 161 | ... 162 | return self.func(*args,**kwargs) #使用func 163 | def __init__(self,*args,**kwargs): 164 | ... 165 | def __call__(self,func): 166 | ... 167 | return decorator2.ActualDecorator(func) 168 | ``` 169 | 170 | ![带参数的函数定义的装饰器](../imgs/python_33_6.JPG) 171 | 172 | 总结: 173 | 174 | * 不带参数的装饰器`decorator`装饰一个名字`F`(可能为函数名、也可能为类名)`@decorator`:则执行的是:`F=decorator(F)`,直接使用`F` 175 | * 带参数的装饰器`decorator`装饰一个名字`F`(可能为函数名、也可能为类名)`@decorator(args)`:则执行的是:`F=decorator(args)(F)`,间接使用`F` 176 | 177 | 6.利用装饰器可以实现单例模式: 178 | 179 | ``` 180 | def Singleton(cls): 181 | instance=None 182 | def onCall(*args,**kwargs): 183 | nonlocal instance 184 | if instance == None: 185 | instance=cls(*args,**kwargs) 186 | return instance 187 | return onCall 188 | @Singleton 189 | class A: 190 | pass 191 | ``` 192 | ![利用装饰器实现单例模式](../imgs/python_33_7.JPG) 193 | 194 | 7.利用装饰器可以跟踪对象的调用接口,从而管理对实例的接口访问(如统计调用次数,打印调用日志) 195 | 196 | ``` 197 | def Tracer(cls): 198 | class Wrapper: 199 | def __init__(self,*args,**kwargs): 200 | self.wrapped=cls(*args,**kwargs) 201 | def __getattr__(self,name): 202 | print('Trace:'+name) 203 | return getattr(self.wrapped,name) 204 | return Wrapper 205 | @Tracer 206 | class A: 207 | pass 208 | ``` 209 | 210 | ![利用装饰器跟踪对象接口](../imgs/python_33_8.JPG) 211 | 212 | 8.装饰器也可以直接管理函数和类,而不仅仅只是管理对他们的调用 213 | 214 | * 利用装饰器添加函数和类到注册表: 215 | 216 | ``` 217 | register_dict={} 218 | def register(obj): 219 | register_dict[obj.__name__]=obj 220 | return obj 221 | @register 222 | def func(): 223 | pass 224 | ``` 225 | 226 | ![利用装饰器添加函数到注册表](../imgs/python_33_9.JPG) 227 | 228 | * 利用装饰器为函数和类添加属性 229 | 230 | ``` 231 | def register(obj): 232 | obj.label=0 233 | return obj 234 | @register 235 | def func(): 236 | pass 237 | ``` 238 | 239 | ![利用装饰器为函数添加属性](../imgs/python_33_10.JPG) 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | -------------------------------------------------------------------------------- /chapter/34_python_metaclass.md: -------------------------------------------------------------------------------- 1 | 6 | # 元类 7 | 1.元类是一种特殊的类,它用于创建类。元类机制允许我们在一条`class`语句的末尾自动插入某些逻辑。它在类对象创建时运行,是管理和扩展类的钩子。元类不是管理类的实例,而是管理类本身 8 | 9 | 2.尽管类的装饰器通常用来管理或者扩展类实例,但是他们也可以用于管理和扩展类对象本身,也与元类的功能重叠 10 | 11 | 3.Python3中,所有用户定义的类都是`type`类对象的实例,`type`类是应用最广的元类 12 | 13 | 4.`class`语句的内部机制:在一条`class`语句的末尾,Python会调用`type`类的构造函数来创建一个`class`对象。 14 | 15 | ``` 16 | MyClass=type(classname,superclasses,attributedict) #新建了一个类,类名叫MyClass 17 | # classname:类名,会成为MyClass类的 .__name__属性 18 | # superclasses:类的超类元组,会成为MyClass类的 .__bases__属性 19 | # attributedict:类的命名空间字典,会成为MyClass类的 .__dict__ 属性 20 | # 这个语句也是动态创建类对象的方法 21 | ``` 22 | 23 | * `type`类定义了一个`.__call__(...)`方法。该方法运行`type`类定义的两个其他方法: 24 | * `.__new__(mclass,classname,superclasses,attributedict)`方法,它返回新建的`MyClass`类 25 | > `mclass`:为本元类,这里是`type`类 26 | > `classname`:为被创建的类的类名,这里是`'MyClass'` 27 | > `superclasses`:为被创建的类的超类元组 28 | > `attributedict`:为被创建的类的名字空间字典 29 | * `.__init__(customclass,classname,superclasses,attributedict)`方法, 30 | 它初始化新建的`MyClass`类 31 | > `customclass`:为被创建的类,这里是`MyClass`类 32 | > `classname`:为被创建的类的类名,这里是`'MyClass'` 33 | > `superclasses`:为被创建的类的超类元组 34 | > `attributedict`:为被创建的类的名字空间字典 35 | 36 | 5.所有的类型均由`type`类创建。要通知Python用一个定制的元类来创建类,可以直接声明一个元类来拦截常规的类创建过程。 37 | 38 | 定义元类:(所有元类必须是`type`的子类) 39 | 40 | ``` 41 | class MetaClass(type): 42 | def __new__(mclass,classname,superclasses,attributedict): 43 | return type.__new__(mclass,classname,superclasses,attributedict) 44 | def __init__(customclass,classname,superclasses,attributedict): 45 | return type.__init__(customclass,classname,superclasses,attributedict) 46 | ``` 47 | 使用元类: 48 | 49 | ``` 50 | class MyClass(metaclass=MetaClass): 51 | pass 52 | ``` 53 | 54 | * 继承的超类也列在括号中,但是要在元类之前,也用逗号分隔: 55 | `class MyClass(BaseCls1,BaseCls2,metaclass=MetaClass)` 56 | * 使用元类声明后,在`class`语句底部进行创建`MyClass`类时,改为调用元类`MetaClass`而不是默认的`type`: 57 | `MyClass=Meta('MyClass`,superclasses,attributedict)` 58 | * 元类`MetaClass`要实现元类协议: 59 | * 重载元类的`.__new__(Meta,classname,superclasses,attributedict)`方法,它返回新建的`MyClass`类 60 | * 重载元类的`.__init__(customclass,classname,superclasses,attributedict)`方法, 61 | 它初始化新建的`MyClass`类 62 | >`type`类的`.__call__(...)`方法将创建和初始化`MyClass`类对象的调用委托给元类MetaClass` 63 | 64 | ![元类](../imgs/python_34_1.JPG) 65 | 66 | 6.元类有的时候不一定是个真正的类,它也可能是一个函数。任何可调用对象都可以作为一个元类,只需要按照以下的做法: 67 | 68 | ``` 69 | def MetaFactory(classname,superclasses,attributedict): 70 | ... 71 | return type(classname,superclasses,attributedict) #动态创建类型 72 | class A(metaclass=MetaFactory): 73 | pass 74 | ``` 75 | 76 | * 在`class`语句的末尾会调用`MetaFactory`函数 77 | 78 | ![函数做元类](../imgs/python_34_2.JPG) 79 | 80 | 7.事实上元类只用于创建类对象,元类并不产生元类自己的实例。因此元类的名字查找规则有些不同:`.__call__`,`.__new__`,`.__init__`方法均在类中查找 81 | 82 | 8.元类的继承: 83 | 84 | * 元类声明由子类继承,即子类的构建也是由父类的元类负责 85 | 86 | ![元类的继承](../imgs/python_34_3.JPG) 87 | 88 | * 如果元类是以函数的方式声明,则子类的构建不再继承这个函数式元类 89 | 90 | ![元类的继承](../imgs/python_34_4.JPG) 91 | 92 | * 元类中的属性并不进入自定义类的命名空间,即元类中声明的一些类属性与被创建类的名字空间无关(他们是两个不同的类) 93 | 94 | ![元类和被创建类的属性](../imgs/python_34_5.JPG) 95 | 96 | * 自定义的类,如果没有显示指定元类,也没有指定父类,则默认使用`type`作为元类(即常规的类创建机制) -------------------------------------------------------------------------------- /chapter/35_python_execution.md: -------------------------------------------------------------------------------- 1 | 6 | # Python 执行细节 7 | 8 | 1.Python脚本执行时,Python内部会首先将源代码编译成字节码的形式。字节码是平台无关的。 9 | 10 | - 字节码是平台无关的 11 | - 如果Python进程拥有写入权限,则它会将脚本的字节码以一个`.pyc`为扩展名的文件。当脚本运行后你可以在源代码所在的目录附近看到`.pyc`文件。 12 | - 下一次运行脚本时,如果你在上次保存字节码之后没有修改源代码时,Python会自动加载`.pyc`文件并跳过编译步骤 13 | - 如果Python进程没有写入权限,则字节码会在内存中生成并在脚本执行结束后抛弃。 14 | - `.pyc`文件也是发布Python程序的方法之一。此时并不需要提供`.py`源代码。 15 | 16 | 2.字节码由Python虚拟机(简称`PVM`)来解释执行。实际上`PVM`不是一个独立的程序,它只是迭代运行字节码指令的一个大循环而已,它是Python系统的一部分。 -------------------------------------------------------------------------------- /chapter/3_python_set_type.md: -------------------------------------------------------------------------------- 1 | 6 | # Set类型 7 | 1.Python的`set`类型是集合内元素值唯一、元素值不可变的无序集。 8 | `set`类型并不要求其集合内各个元素都是相同类型。 9 | 10 | * 唯一:`set`类型的集合内元素的值唯一。 11 | * 如果有两个变量指向同一个对象,则在`set`集合内只会出现一次 12 | * 如果有两个对象值相等,则在`set`集合内也只会出现一次 13 | * 不可变:一旦创建完毕,则`set`集合内的各个元素值不能修改。 14 | > 本质上`set`通过元素值的哈希值来判断唯一性 15 | ![set的唯一性](../imgs/python_3_1.JPG) 16 | 17 | 2.`set`本身的性质有: 18 | 19 | * `set`对象是可迭代对象 20 | * `set`对象可以增长或缩短 21 | * `set`对象可能包含各种类型的对象作为元素 22 | * 创建`set`对象的方法为调用`x=set(iter)`,其中`iter`为任何序列或可迭代对象 23 | 24 | 3.`set`对象有以下操作: 25 | 26 | * 成员关系:`e in x`(返回`bool`值) 27 | * 差集: `x-y`(返回新的`set`对象) 28 | * 并集: `x|y`(返回新的`set`对象) 29 | * 交集: `x&y`(返回新的`set`对象) 30 | * 对称差集: `x^y`(返回新的`set`对象) 31 | >对称差集:并集中去掉交集的那部分 32 | * 判定x是否是y的超集: `x>y`(返回`bool`值) 33 | * 判定x是否是y的子集:`x 注意这里与表达式中的`set`交集、并集操作的区别: 43 | 表达式中的`set`交集、并集操作要求两个对象均为集合 44 | 45 | ![set方法](../imgs/python_3_3.JPG) 46 | 47 | 4.`set`对象是可迭代的,因此可用于`len()`函数,`for`循环,以及列表解析中,但是因为是无序的所以不支持索引和分片操作。 48 | ![set可迭代](../imgs/python_3_4.JPG) 49 | 50 | 5.Python3中,`set`常量可以用大括号创建:`{1,2,3,4}`等价于`set([1,2,3,4])`。 51 | 52 | * 空`set`必须由`set([])`创建,`{}`为空字典而不是空`set` 53 | * Python3中,`set`的字符串表示为`{...}`形式,而不是`set([...])`形式 54 | 55 | 6.`set`只能包含不可变的对象,因此列表对象、字典对象、`set`对象均不能作为`set`的元素。 56 | > 元组和字符串可作为`set`的元素 57 | 58 | ![set可迭代](../imgs/python_3_5.JPG) 59 | 60 | 7.`frozenset`是`set`的子类型,它与`set`的唯一区别是: 61 | `frozenset`对象可以作为`set`的元素。 62 | ![frozenset](../imgs/python_3_6.JPG) 63 | 64 | 8.`set`的解析构造(在Python3之后):`{x**2 for x in iter}`,其中`iter`为任何可迭代对象。 65 | ![frozenset](../imgs/python_3_7.JPG) 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /chapter/4_python_string_type.md: -------------------------------------------------------------------------------- 1 | 6 | # 字符串 7 | 1.Python的字符串是一种不可变序列,它包含的字符存在从左到右的位置顺序。 8 | 不可变是指:字符串创建以后无法修改它的内容。 9 | > 序列包括:字符串、列表、元组 10 | 11 | * 字符串由一对双引号或者单引号包围,二者无任何区别 12 | > 如果希望字符串中包含引号,则有两种办法: 13 | > 14 | >* 最外围的引号和字符串中的引号使用不同的格式。如`"who's name is Tex?"` 15 | >* 采用转义字符。如`who\'s name is Tex?'` 16 | * 空字符串是一对引号之间不包含内容:`""` 17 | * Python自动在任意的表达式中合并相邻的字符串常量 18 | * 在字符串中反斜杠代表转义序列,每个转义序列代表一个字符(不一定能够打印出来): 19 | * `\\`转义反斜杠本身 20 | * `\n`为换行符 21 | * `\0`为`\0x00`(不是字符串结尾) 22 | * `\xhh`为十六进制值 23 | * `\o00`为八进制值 24 | * `\uhhhh`为16位Unicode的十六进制值 25 | * `\Uhhhhhhhh`为32位Unicode的十六进制值 26 | * Python中`\0`并不去结束一个字符串,Python会在内存中保持整个字符串的长度和文本 27 | * Python会以十六进制显示非打印的字符 28 | * 若无合法的转义编码识别`\`以及之后的字符,则Python会在最终的字符串中保留`\` 29 | 30 | ![字符串](../imgs/python_4_1.JPG) 31 | 32 | 2.raw 字符串是一种特殊的字符串。当`r`出现在字符串起始引号之前时,为 raw 字符串。这种字符串会关闭转义机制。 33 | raw 字符串不能以`\`结尾。 34 | ![raw字符串](../imgs/python_4_2.JPG) 35 | 36 | 3.字符串块:以三重引号包围的字符串(引号为一对单引号或者一对双引号均可)。 37 | 三重引号之间的文本会被收集到一个单独的多行字符串中,并且将换行以`\n`代替。 38 | 39 | 4.字符串可以有以下操作: 40 | 41 | * 加:两个字符串相加,返回一个新的字符串 42 | * 乘:字符串与整数`N`相乘,返回一个新的字符串,该字符串是原字符串重复`N`次 43 | * `len()`:返回字符串长度 44 | * 迭代:字符串对象是一个可迭代对象 45 | 46 | ![字符串操作](../imgs/python_4_3.JPG) 47 | 48 | 5.字符串支持索引,其中`S[i]`获取的是偏移为`i`处的字符(偏移`i`从0开始, 49 | 小于等于`len(S)-1`)。 50 | >若偏移`i`大于`len(S)-1`则Python会抛出`IndexError`异常, 51 | >提示`string index out of range` 52 | 53 | 你也可以提供一个负的偏移,其中`-1`为最后一个字符,`-n`为`len(S)-n`处的字符。 54 | ![字符串索引](../imgs/python_4_4.JPG) 55 | 56 | 6.字符串支持分片,语法为`S[m:n]`,它返回从`m`开始到`n`(不包含`n`)的一个新字符串。 57 | 58 | * 未给出上界时,该分片默认上界为`len(S)` 59 | * 未给出下界时,该分片默认下界为 0 60 | * 如果是`S[:]`,则返回字符串`S`的一个全新拷贝 61 | 62 | 如果增加步进参数,则语法为`S[m:n:k]`,它返回从`m`开始到`n`(不包含`n`)且每隔`k`个元素选取一次的一个全新字符串,默认`k`为 1。 63 | 64 | * 若`k`为正数则从左到右步进;若`k`为负数,则从右向左步进 65 | * `S[::-1]`返回S的翻转字符串 66 | * 负步进时,默认的上下界极限有所不同。 67 | * 上界极限可以为空或者为`len(S)`或者为`len(S)-1` 68 | * 下界极限必须为空,否则 0 号元素会被排除掉 69 | 70 | ![字符串分片](../imgs/python_4_5.JPG) 71 | 72 | 7.可以通过`str()`函数与`repr()`函数将数字转成字符串。 73 | 74 | * `str()`函数的效果类似`print()`的效果 75 | * `repr()`函数产生的结果可以由解释器解释。`eval(repr(x))`会返回`x`。 76 | 77 | ![str()与repr()](../imgs/python_4_6.JPG) 78 | 79 | 8.可以在单个字符与它对应的 ASCII 码值之间转换: 80 | 81 | * `ord(char)`:返回单个字符的 ASCII 值 82 | * `chr(ascii_int)`: 返回 ASCII 值对应的字符 83 | 84 | ![字符与ASCII值之间转换](../imgs/python_4_7.JPG) 85 | 86 | 9.字符串是不可变序列,因此不能原地修改一个字符串,只能够生成新的字符串并赋值给原变量。 87 | ![字符串不可原地修改](../imgs/python_4_8.JPG) 88 | 89 | 10.字符串格式化表达式是基于C语言的printf格式化表达式。其格式为: 90 | 91 | "%d %s apples" % (3,'bad') 92 | 它返回一个新的字符串。 93 | 94 | * 占位符有多种: 95 | `%s`:字符串; `%r`:也是字符串,但用`repr()`得到的而不是`str()`; 96 | `%c`:字符; `%d`:十进制整数; `%i`:整数; `%e`:浮点指数; 97 | `%f`: 浮点十进制;`%%`:百分号 `%`, `%g`:自动转成浮点`%e`或者`%f` 98 | ![字符串格式化占位符](../imgs/python_4_9.JPG) 99 | 100 | * 转换通用目标结构为:`%[(key_name)][flags][width][.precision]type` 101 | * `key_name`:用于从右边字典中取对应键的值(此时右边的是字典对象,而不是元组) 102 | 如:`"%(n)%d %(x)%s" %{"n":3,"x":"apples"}` 103 | * `flags`:如果为`-`则左对齐;如果为`+`则为正负号;如果为`0`:则补零 104 | * `width`: 指定位宽(包括小数点在内),至少为`width`字符宽度 105 | * `precision`:指定小数点后几位 106 | >`width`和`precision`可以为`*`,表示它们从输入的下一项中取值 107 | (即从元组中取得) 108 | * `type`为类型,如`d`,`r`,`f`,`e`等 109 | ![字符串格式化表达式通用目标结构](../imgs/python_4_10.JPG) 110 | 111 | 11.格式化字符串除了使用字符串格式化表达式之外,还可以通过字符串的`.format()` 112 | 方法达到同样的效果。 113 | 114 | * `.format()`方法支持位置参数、关键字参数、以及二者的混合。 115 | * 位置参数: `"{0},{1},{2}".format('abc','def','ghi')` 116 | * 关键字参数:`"{k1},{k2},{k3}".format(k1='abc',k2='def',k3='ghi')` 117 | * 混合使用:`"{0},{1},{k}".format('abc','def',k='ghi')` 118 | ![.format参数类型](../imgs/python_4_11.JPG) 119 | * 格式化字符串中可以指定对象的属性、字典键、序列的索引: 120 | * 指定字典的键:`"{0[a]}".format({'a':'value'})`, 121 | 注意这里的键`a`并没有引号包围 122 | * 指定对象的属性:`"{0.platform}".format(sys)`,也可以用关键字参数: 123 | `"{obj.platform}".format(obj=sys)` 124 | * 指定序列的索引:`"{0[2]}".format("abcd")` ,这里只能进行正索引值,且不能分片 125 | ![.format参数类型](../imgs/python_4_12.JPG) 126 | * 通用目标结构为: `{fieldname!conversionflag:formatspec}` 127 | * `fieldname`为位置数字 0,1,2,... 或者为关键字,它后面可选地跟随 128 | * `.name`:则指定对象的属性 129 | * `[index]`:指定了索引 130 | * `[key]`:指定了字典的键 131 | * `conversionflag`为转换标记,可以为: 132 | * `r`:在该值上调用一次`repr()`函数 133 | * `s`:在该值上调用一次`str()`函数 134 | * `a`:在该值上调用一次`ascii()`函数 135 | * `formatspec`为格式,其结构为: 136 | `[[fill] align] [sign] [#] [0] [width] [.precision] [type]` 137 | * `fill`一般与`align`为`=`时配合 138 | * `align`为对齐: 139 | * `<`:左对齐 140 | * `>`:右对齐 141 | * `=`:必须指定`fill`(单个字符),此时用这个字符填充 142 | * `^`:居中对齐 143 | * `sign`:为正负号标记 144 | * `#`:作用未知 145 | * `0`:补0 146 | * `width`:位宽 147 | * `.precision`:精度 148 | * `type`:为类型,如`d`,`r`,`f`,`e`等, 149 | 但与格式化字符串表达式相比,多了`b`(二进制格式输出) 150 | ![.format通用目标结构](../imgs/python_4_13.JPG) 151 | * 某些值可以从`.format()`的参数中获取,如`"{0:+0{1}d}".format(128,8)`, 152 | 其指定精度信息从`format()`的参数中取(参数8) 153 | 154 | 12.格式化单个值比较简单,可以有以下方法: 155 | 156 | * `"%s" % 1.23` 157 | * `"%s" % (1.23,)`,这里`(1.23,)`是一个单元素元组, 158 | 而`(1.23)`是一个表达式 159 | * `"{0}".format(1.23)` 160 | 161 | 13.由于Python内部会暂存并重复使用短字符串来进行优化,因此该短字符串在内存中只有一份。 162 | ![字符串暂存优化](../imgs/python_4_14.JPG) 163 | 164 | 14.浮点数格式化时,采用`%s`说明符与`%f`说明符,其结果不同: 165 | ![浮点数格式化](../imgs/python_4_15.JPG) 166 | 167 | 因为按照`%f`格式化输出时,浮点数有精度和位宽的设定(虽然这里没有显式指定,但是它们有默认值)。而`%s`格式化输出时,首先调用了`str()`函数,然后再进行输出。 168 | 169 | 170 | 171 | 172 | -------------------------------------------------------------------------------- /chapter/5_python_list_type.md: -------------------------------------------------------------------------------- 1 | 6 | # 列表 7 | 1.列表可以包含任何种类的对象。它是本身是一种可变对象,支持原地修改。 8 | > 不可变对象有元组、字符串、`frozenset` 9 | 10 | 2.列表的基本操作有: 11 | 12 | * `list1+list2`:列表相加合并生成新的列表, 13 | 并不会修改`list1`和`list2`的值 14 | * `list1*5`: 列表乘以数字生成重复的新列表, 15 | 并不会修改`list1`的值 16 | ![列表的加法和乘法](../imgs/python_5_1.JPG) 17 | * `item in list1`: 返回元素`item`是否在列表`list1`中 18 | * 用于迭代,并不会修改`list1`的值: 19 | 20 | ``` 21 | for item in list1: 22 | pass 23 | ``` 24 | * 列表解析: `[item*4 for item in iter_obj]` 25 | * `list()`函数:`list(iter_obj)`生成新列表 26 | ![列表的迭代、解析、以及生成](../imgs/python_5_2.JPG) 27 | 28 | * 索引和分片: 29 | * `list1[index]`:索引,获取指定偏移处的对象, 30 | 并不会修改`list1`的值 31 | * `list1[index1:index2]`:分片,返回一个新列表, 32 | 其元素值与旧列表对应片段处元素值相等, 33 | 并不会修改`list1`的值 34 | * 当索引位于赋值左侧时,则是索引赋值。这会改变列表指定项的内容。 35 | 修改`list1`的值 36 | * 当分片位于赋值左侧时,则是分片赋值。这会改变列表指定片段的内容。 37 | 修改`list1`的值 38 | >被赋值的片断长度不一定要与赋值的片断长度相等 39 | 40 | ![列表索引、分片以及索引赋值、分片赋值](../imgs/python_5_3.JPG) 41 | 42 | 3.列表的方法调用有: 43 | 44 | * `.append(val)`方法:在列表之后追加元素,原地修改 45 | * `.sort(key=None, reverse=False)`方法:对列表进行排序, 46 | 原地修改 47 | * `key`为一个排序方法 48 | * `reverse`为是否逆序 49 | * `.reverse()`方法:原地翻转列表,原地修改 50 | * `.extend(iter_obj)`方法:在列表末端插入多个元素,原地修改 51 | * `.pop()`方法:删除末尾元素并返回该元素,原地修改 52 | * `.pop(index)`方法:删除指定位置元素并返回该元素, 53 | 原地修改 54 | * `del list1[0]`函数:删除指定位置处元素,原地修改 55 | >与`.pop()`的区别在于`del()`函数并不返回该元素 56 | * `del list1[index1:index2]`函数:删除指定片断处元素, 57 | 原地修改 58 | * `.remove(val)`:通过值删除元素,**若有多个值,则只删除第一个遇到的值** 59 | 原地修改 60 | * `.insert(index,val)`:在指定位置插入元素,原地修改 61 | * `.index(val)`:返回指定元素的位置,**若有多个值,则只返回第一个遇到的值所在位置** 62 | 63 | ![列表方法](../imgs/python_5_4.JPG) 64 | ![重复值列表的删除与查找](../imgs/python_5_5.JPG) 65 | 66 | 4.列表相关的内置函数: 67 | 68 | * `sorted(list1,key=None,reverse=False)`:排序列表并返回新列表, 69 | 非原地修改 70 | * `reversed(list1)`:返回迭代器,该迭代器迭代结果就是列表的逆序 71 | ![列表相关的内置函数](../imgs/python_5_6.JPG) 72 | 73 | 5.列表不允许引用超出列表末尾的索引。 74 | ![列表索引超出末尾](../imgs/python_5_7.JPG) 75 | 76 | 6.列表解析表达式:通过对可迭代对象迭代生成的项运行一个表达式的方式创建一个新列表。如: 77 | `[c*2 for c in "apple"]` 78 | 79 | 7.`list(iter_obj)`可以通过可迭代对象`iter_obj`生成一个新列表 80 | 81 | 8.假设`L=[1,2]`,注意下面的用法的区别: 82 | 83 | * `X=L*2`:列表乘以整数,生成新列表`[1,2,1,2]` 84 | * `Y=[L]*2`:也是列表乘以整数,但是被乘列表是`[[1,2],]`, 85 | 生成的新列表是`[[1,2],[1,2],]` 86 | >列表元素为可变对象时注意,这里改变`L`会影响`Y`,因为`Y`持有`L`的引用 87 | 88 | ![列表索引超出末尾](../imgs/python_5_8.JPG) 89 | 90 | 9.如果列表的元素是可变对象的,则对该可变对象的修改会影响到列表。 91 | ![列表索引超出末尾](../imgs/python_5_9.JPG) 92 | 93 | -------------------------------------------------------------------------------- /chapter/6_python_tuple_type.md: -------------------------------------------------------------------------------- 1 | 6 | # 元组 7 | 1.元组是由圆括号包围的一系列项,它由任意对象元素组成。它本身是不可变的, 8 | 即不可以原地修改。 9 | >`set`与`tuple`相反,`set`是本身可变,但是其元素不可变。 10 | 11 | * 元组的元素可以为可变对象,也可以为不可变对象。 12 | * 元组是有序的序列 13 | * 元组中的元素可以通过偏移访问,支持索引和分片,但是不支持索引赋值和分片赋值 14 | * 通过索引访问返回一个值 15 | * 通过分片访问返回一个新的元组 16 | * `()`为空元祖,`(0,)`为单元素元组(注意逗号`,`),`(0)`为括号表达式 17 | * `tuple(iter_obj)`函数从一个可迭代对象生成元组 18 | * `len(tuple1)`:该函数获取元组长度 19 | * `item in tuple1`:判断元素值`item`是否在元组中 20 | * 元组是一个可迭代对象,它支持迭代: 21 | 22 | ``` 23 | for item in tuple1: 24 | pass 25 | ``` 26 | * `.index(val)`方法:在元组中搜索`val`值所在位置 27 | * `.count(val)`方法:在元组中累计`val`值出现的次数 28 | 29 | ![元组用法](../imgs/python_6_1.JPG) 30 | 31 | 2.在不会引起语法冲突的情况下,Python支持忽略元组中的圆括号。如`x=10,20`等价于`x=(10,20)` 32 | -------------------------------------------------------------------------------- /chapter/7_python_collections.md: -------------------------------------------------------------------------------- 1 | 6 | #通用序列 7 | 1.Python的通用序列包括:字符串、列表、元组。 8 | 9 | * 字符串:不可变对象,元素只能是字符 10 | * 列表:可变对象,元素类型不限 11 | * 元组:不可变对象,元素类型不限 12 | >这里的`可变`指的是能否修改对象,如增加序列元素,修改序列元素等等 13 | 14 | 2.Python通用序列操作有: 15 | 16 | * 索引操作: `seq[index]`,返回序列在该位置处的值,有越界检查 17 | * 分片操作: `seq[index1:index2]`:返回一个新的序列,有越界检查 18 | * 序列加法: `seq1+seq2`:返回一个新序列,包含`seq1`、`seq2`的拷贝 19 | * 序列乘法: `seq*N`:返回一个新序列,包含`seq`的`N`份拷贝 20 | ![通用序列操作](../imgs/python_7_1.JPG) 21 | 22 | 3.`set`不是序列,它是可变对象,但是元素只能是不可变类型。 23 | 字典也不是序列,它是可变对象,其元素的值是不限类型,但是键必须是不可变类型。 24 | 25 | 4.三种解析表达式: 26 | 27 | * 列表解析表达式:`[c*4 for c in 'abcd']`生成一个新列表对象, 28 | 结果为:`['aaaa','bbbb','cccc','dddd']` 29 | * `set`解析表达式:`{c*4 for c in 'abcd'}`生成一个新`set`对象, 30 | 结果为:`{'aaaa','bbbb','cccc','dddd'}`(打印顺序不确定) 31 | * 字典解析表达式:`{c:c*4 for c in 'abcd'}`生成一个新列表对象, 32 | 结果为:`{'a':'aaaa','b':'bbbb','c':'cccc','d':'dddd'}` (打印顺序不确定) 33 | ![三种解析表达式](../imgs/python_7_2.JPG) 34 | >注意:并没有元组解析表达式。`(c*4 for c in 'abcd')`返回的是一个生成器对象。 35 | ![生成器表达式](../imgs/python_7_3.JPG) 36 | 37 | 5.Python 3.0 中,`bytearray`字节串类型是可变的 38 | 39 | 6.序列的拷贝: 40 | 41 | * 分片表达式能复制序列, 只能进行顶层拷贝 42 | * 字典的`.copy()`方法能复制字典, 只能进行顶层拷贝 43 | * 通过`list()`、`tuple()`等方法 44 | * 通过`copy`标准库模块,可以进行深层拷贝 45 | ![序列的拷贝](../imgs/python_7_4.JPG) 46 | 47 | 7.比较操作时,Python能够自动遍历嵌套的对象,从左到右递归比较,要多深有多深。过充中首次发现的差异将决定比较的结果。 48 | ![嵌套序列的比较](../imgs/python_7_5.JPG) 49 | 50 | * 数字通过相对大小比较 51 | * 字符串从左到右每个字符按照字符的字典序比较 52 | * 列表和元组从左到右每部分内容都比较 53 | * 字典通过排序后的`(键,值)`列表比较 54 | >Python 3.0 中的字典只能进行相等、不等比较 55 | 56 | * 数字与其他类型比较在 Python 3.0 中是错误的 57 | ![Python3不支持的比较操作](../imgs/python_7_6.JPG) 58 | 59 | 8.当一个复合对象包含指向自身的引用时(如`L.append(L)`),称为循环对象。当Python在对象中检测到循环时,会打印成`[...]`而不会陷入无限循环。 60 | ![Python3循环对象](../imgs/python_7_7.JPG) 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /chapter/8_python_dictionary.md: -------------------------------------------------------------------------------- 1 | 6 | # 字典 7 | 1.字典是一些 "键 --- 值" 对的无序集和。它通过键来索引。 8 | 9 | * 字典是可变对象,它的元素的值的类型不限,它的元素的键类型是不可变类型 10 | > 意味着键类型不能是列表、`set`、,字典 11 | 12 | * 字典的常量表达式:`{'a':3,'b':4}`,空字典的常量表达式为`{}` 13 | * `dict()`函数可以从关键字参数生成字典:`dict(a=3,b=4)`生成字典`{'a':3,'b':4}` 14 | * 你可以通过`zip()`函数生成关键字参数:`dict(zip(['a','b'],[3,4]))` 15 | 生成字典`{'a':3,'b':4}` 16 | * 你也可以用字典的`.fromkeys()`类方法生成字典: 17 | * `dict.fromkeys(['a','b'])` 生成字典`{'a':None,'b':None}` 18 | * `dict.fromkeys(['a','b'],3)` 生成字典`{'a':3,'b':3}` 19 | 20 | ![字典的生成](../imgs/python_8_1.JPG) 21 | 22 | * 字典索引:`d[key]`。字典索引返回对应的值 23 | * 键测试:`key in d`。测试指定键是否存在字典中 24 | * 字典的迭代: 25 | * `d.keys()`:返回一个dict_keys对象,它是一个可迭代对象,迭代时返回键序列 26 | * `d.values()`:返回一个dict_values对象,它是一个可迭代对象,迭代时返回值序列 27 | * `d.items()`:返回一个dict_items对象,它是一个可迭代对象, 28 | 迭代时返回元组`(键,值)`的序列 29 | >字典迭代在Python3中返回可迭代对象,在Python2.7中均返回列表。 30 | 因此在Python3中如果想得到列表,必须将返回值传入`list()`函数中得到列表 31 | * 字典的拷贝:`d.copy()`。只是字典的浅拷贝 32 | * 获取键的值:通过`d.get(key,default_value)`。返回键对应的值, 若键不存在则返回 33 | `default_value` 34 | >对于`d[key]`返回对应的值,如果`key`不存在则抛出`KeyError`异常 35 | 36 | ![字典的索引、键测试、迭代和拷贝](../imgs/python_8_2.JPG) 37 | * 字典的操作: 38 | * `d1.update(d2)`:合并两个字典,原地修改`d1`字典 39 | * `d.pop(key)`: 从字典中删除`key`并返回该元素的值 40 | * `del d[key]`:从字典中删除`key`但是不返回该元素的值 41 | * `d[key]=value`:原地的添加/修改字典。当向不存在的键赋值时,相当于添加新元素 42 | * 获取字典中元素数量:`len(d)`。它也等于键列表/值列表的长度 43 | ![字典操作和数量](../imgs/python_8_3.JPG) 44 | * 字典本身也是一个可迭代对象,它的迭代方法为: 45 | 46 | ``` 47 | for key in d: 48 | pass 49 | ``` 50 | 它在列表的键上迭代,也等价于 51 | 52 | ``` 53 | for key in d.keys() 54 | pass 55 | ``` 56 | ![字典迭代](../imgs/python_8_4.JPG) 57 | 58 | 2.Python3中字典的变化: 59 | 60 | * `d.keys()`、`d.values()`、`d.items()`返回的是可迭代对象,他们称为视图对象, 61 | 而不是列表。修改字典,则这些视图对象会相应变化 62 | * 支持字典解析表达式来生成字典,如 `{k:v for k,v in zip(['a','b','c'],[1,2,3])}`, 63 | `{k:v for k,v in [('a',1),('b',2),('c',3)]}` 64 | * 取消了Python2中的`has_key(key)`方法,而用`key in d`表达式取代 65 | * Python3中,只有相等不等测试有效,字典的大小比较无效 66 | ![Python3字典变化](../imgs/python_8_5.JPG) 67 | 68 | 3.Python3中字典是一个迭代器对象,其迭代结果是返回键序列 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /chapter/9_python_file.md: -------------------------------------------------------------------------------- 1 | 6 | # 文件 7 | 1.内置的`open()`函数会创建一个Python文件对象,可以作为计算机上的一个文件的引用。 8 | 9 | 2.打开文件:`outfile=open(r'C:\filename','w')`: 10 | 11 | * 第一个参数`r'C:\filename'`为文件名字符串 12 | * 第二个参数`'w'`为打开模式字符串。有以下三种: 13 | * `'r'`:读打开(默认行为) 14 | * `'w'`:写打开 15 | * `'a'`:追加写打开 16 | 17 | >* 第二个参数也可以添加`'b'`表示二进制处理。二进制处理中,换行符未转换, 18 | > 同时Python3中 Unicode编码被关闭 19 | >* 第二个参数也可以添加`'+'`表示读写同时作用 20 | * 还有第三个参数可选,用于控制输出缓冲.`'0'`表示无缓冲(只能在二进制模式中使用无缓冲)。 21 | ![打开文件](../imgs/python_9_1.JPG) 22 | 23 | 3.文件对象的方法: 24 | 25 | * 文件读入: 26 | * `.read()`:读取接下来的整个文件到单个字符串 27 | * `.read(n)`:读取接下来的`n`个字节到一个字符串 28 | * `.readline()`:读取下一行到一个字符串(包括行末的换行符) 29 | * `.readlines()`:按行读取接下来的整个文件到字符串列表,每个字符串一行 30 | ![读取文件](../imgs/python_9_2.JPG) 31 | * 写入文件: 32 | * `.write(str)`:写入字符串到文件(并不会自动添加换行符以及其他任何字符,`str`是啥就写啥), 33 | 返回写入的字符数 34 | * `.writelines(strlist)`:将字符串列表内所有字符串依次写入文件(并不会自动添加换行符以及其他任何字符) 35 | ![写入文件](../imgs/python_9_3.JPG) 36 | * 关闭文件:`.close()`方法。 37 | * 刷新输出缓冲区:`.flush()`方法。通常关闭文件会将输出缓冲区内容写入到文件中;但用`.flush()`方法不必关闭文件。 38 | * 定位文件:`.seek(N)`方法,将文件偏移修改到字节`N`处以便进行下一次操作 39 | * 文件迭代:文件对象也是一个可迭代对象,每一次迭代返回一行,对于大型文件一次性读取非常耗内存和性能: 40 | 41 | ``` 42 | for line in open('data'):#每次循环迭代时,自动读取并返回一行 43 | #use line 44 | pass 45 | ``` 46 | * 读写文本文件时,默认的编码是`utf-8`,你也可以使用指定的编码:`open(r'filename',encoding='latin-1')` 47 | * 读二进制文件用`openopen(r'filename','rb')` 48 | ![迭代文件以及编码](../imgs/python_9_4.JPG) 49 | 50 | 4.从文本文件中读取文字行的最佳方式是用文件迭代器,不要直接读取文件 51 | 52 | 5.当文件对象被自动收回时,Python会自动关闭该文件,这意味着不一定要手动调用`.close()`方法 53 | 54 | 6.默认的写操作总是缓冲的。当文件关闭或者`.flush()`方法调用时,缓冲的输出数据会写入硬盘。 55 | 56 | 7.文件的空行是含有换行符的字符串,而不是空字符串。因此如果读入操作返回空字符串,则表示已经到文件末尾了。 57 | 58 | 8.Python3中,文本文件将内容表示为常规的`str`字符串,自动执行Unicode编码和解码,并且默认执行行末转换。而二进制文件将内容表示为一个特殊的`bytes`字节串类型,且运行程序不修改地访问文件内容。 59 | >不能以文本格式打开一个二进制数据文件,会乱码 60 | 61 | ![二进制读写文件](../imgs/python_9_5.JPG) 62 | 63 | 9.可以在文件中存储并解析Python对象。由于文件数据在脚本中是字符串,文件对象的`.write()`方法不会自动地将Python对象转成字符串,因此需要手工转换。 64 | 65 | * 可以用格式化字符串方法或者`str()`方法将Python对象转成字符串 66 | * 将字符串转换成Python对象可以用`eval()`方法 67 | * 或者直接使用`pickle`模块自动存储和解析Python对象: 68 | * 存储:`pickle.dump(obj,file)`,其中`obj`是要存储的Python对象,`file`文件对象 69 | (用二进制写打开) 70 | * 加载:`obj=pickle.load(file)`,其中`file`文件对象(用二进制读打开) 71 | ![pickle存储解析Python对象](../imgs/python_9_6.JPG) 72 | 73 | 10.`struct`模块能够打包/解包二进制数据 74 | 75 | * 打包:`data=struct.pack('>i4sh',7,b'abcd',8)`。其中第一个参数为格式说明字符串,后面的参数为待打包的数据。格式说明字符串为: 76 | * `>`为说明符 77 | * `i`说明第一个待打包的数据为整数 78 | * `4s`说明第二个待打包的数据为4个字节的字节串 79 | * `h`说明第三个待打包的数据为16进制整数 80 | * 解包:`val=struct.unpack('>i4sh`,data)`。其中第一个参数为格式说明符,第二个参数为已经打包的二进制数据,返回一个元组。 81 | ![struct打包/解包二进制数据](../imgs/python_9_7.JPG) 82 | 83 | 11.`sys`模块中有几个预先打开的文件对象: 84 | 85 | * `sys.stdout`对象:标准输出对象 86 | * `sys.stdin`对象:标准输入对象 87 | * `sys.stderr`对象:标准错误输出对象 88 | ![stdin/stdout/stderr对象](../imgs/python_9_8.JPG) 89 | 90 | 12.`os`模块中的文件描述符对象支持文件锁定之类的低级工具 91 | ![os.open](../imgs/python_9_9.JPG) 92 | 93 | 13.`socket`、`pipe`、`FIFO`文件对象可以用于网络通信与进行同步 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /imgs/picture_all.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/picture_all.JPG -------------------------------------------------------------------------------- /imgs/picture_notes.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/picture_notes.JPG -------------------------------------------------------------------------------- /imgs/python_11_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_11_1.JPG -------------------------------------------------------------------------------- /imgs/python_11_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_11_2.JPG -------------------------------------------------------------------------------- /imgs/python_11_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_11_3.JPG -------------------------------------------------------------------------------- /imgs/python_11_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_11_4.JPG -------------------------------------------------------------------------------- /imgs/python_12_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_12_1.JPG -------------------------------------------------------------------------------- /imgs/python_12_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_12_2.JPG -------------------------------------------------------------------------------- /imgs/python_12_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_12_3.JPG -------------------------------------------------------------------------------- /imgs/python_13_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_13_1.JPG -------------------------------------------------------------------------------- /imgs/python_14_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_14_1.JPG -------------------------------------------------------------------------------- /imgs/python_14_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_14_2.JPG -------------------------------------------------------------------------------- /imgs/python_14_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_14_3.JPG -------------------------------------------------------------------------------- /imgs/python_14_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_14_4.JPG -------------------------------------------------------------------------------- /imgs/python_14_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_14_5.JPG -------------------------------------------------------------------------------- /imgs/python_14_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_14_6.JPG -------------------------------------------------------------------------------- /imgs/python_15_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_1.JPG -------------------------------------------------------------------------------- /imgs/python_15_10.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_10.JPG -------------------------------------------------------------------------------- /imgs/python_15_11.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_11.JPG -------------------------------------------------------------------------------- /imgs/python_15_12.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_12.JPG -------------------------------------------------------------------------------- /imgs/python_15_13.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_13.JPG -------------------------------------------------------------------------------- /imgs/python_15_14.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_14.JPG -------------------------------------------------------------------------------- /imgs/python_15_15.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_15.JPG -------------------------------------------------------------------------------- /imgs/python_15_16.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_16.JPG -------------------------------------------------------------------------------- /imgs/python_15_17.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_17.JPG -------------------------------------------------------------------------------- /imgs/python_15_18.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_18.JPG -------------------------------------------------------------------------------- /imgs/python_15_19.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_19.JPG -------------------------------------------------------------------------------- /imgs/python_15_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_2.JPG -------------------------------------------------------------------------------- /imgs/python_15_20.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_20.JPG -------------------------------------------------------------------------------- /imgs/python_15_20_pre.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_20_pre.JPG -------------------------------------------------------------------------------- /imgs/python_15_21.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_21.JPG -------------------------------------------------------------------------------- /imgs/python_15_22.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_22.JPG -------------------------------------------------------------------------------- /imgs/python_15_23.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_23.JPG -------------------------------------------------------------------------------- /imgs/python_15_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_3.JPG -------------------------------------------------------------------------------- /imgs/python_15_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_4.JPG -------------------------------------------------------------------------------- /imgs/python_15_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_5.JPG -------------------------------------------------------------------------------- /imgs/python_15_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_6.JPG -------------------------------------------------------------------------------- /imgs/python_15_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_7.JPG -------------------------------------------------------------------------------- /imgs/python_15_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_8.JPG -------------------------------------------------------------------------------- /imgs/python_15_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_15_9.JPG -------------------------------------------------------------------------------- /imgs/python_16_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_16_1.JPG -------------------------------------------------------------------------------- /imgs/python_16_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_16_2.JPG -------------------------------------------------------------------------------- /imgs/python_17_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_17_1.JPG -------------------------------------------------------------------------------- /imgs/python_17_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_17_2.JPG -------------------------------------------------------------------------------- /imgs/python_17_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_17_3.JPG -------------------------------------------------------------------------------- /imgs/python_18_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_18_1.JPG -------------------------------------------------------------------------------- /imgs/python_18_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_18_2.JPG -------------------------------------------------------------------------------- /imgs/python_18_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_18_3.JPG -------------------------------------------------------------------------------- /imgs/python_18_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_18_4.JPG -------------------------------------------------------------------------------- /imgs/python_18_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_18_5.JPG -------------------------------------------------------------------------------- /imgs/python_18_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_18_6.JPG -------------------------------------------------------------------------------- /imgs/python_18_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_18_7.JPG -------------------------------------------------------------------------------- /imgs/python_18_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_18_8.JPG -------------------------------------------------------------------------------- /imgs/python_18_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_18_9.JPG -------------------------------------------------------------------------------- /imgs/python_19_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_19_1.JPG -------------------------------------------------------------------------------- /imgs/python_19_10.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_19_10.JPG -------------------------------------------------------------------------------- /imgs/python_19_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_19_2.JPG -------------------------------------------------------------------------------- /imgs/python_19_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_19_3.JPG -------------------------------------------------------------------------------- /imgs/python_19_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_19_4.JPG -------------------------------------------------------------------------------- /imgs/python_19_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_19_5.JPG -------------------------------------------------------------------------------- /imgs/python_19_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_19_6.JPG -------------------------------------------------------------------------------- /imgs/python_19_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_19_7.JPG -------------------------------------------------------------------------------- /imgs/python_19_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_19_8.JPG -------------------------------------------------------------------------------- /imgs/python_19_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_19_9.JPG -------------------------------------------------------------------------------- /imgs/python_1_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_1_1.JPG -------------------------------------------------------------------------------- /imgs/python_1_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_1_2.JPG -------------------------------------------------------------------------------- /imgs/python_1_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_1_3.JPG -------------------------------------------------------------------------------- /imgs/python_1_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_1_4.JPG -------------------------------------------------------------------------------- /imgs/python_1_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_1_5.JPG -------------------------------------------------------------------------------- /imgs/python_20_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_20_1.JPG -------------------------------------------------------------------------------- /imgs/python_20_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_20_2.JPG -------------------------------------------------------------------------------- /imgs/python_20_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_20_3.JPG -------------------------------------------------------------------------------- /imgs/python_20_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_20_4.JPG -------------------------------------------------------------------------------- /imgs/python_21_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_21_1.JPG -------------------------------------------------------------------------------- /imgs/python_21_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_21_2.JPG -------------------------------------------------------------------------------- /imgs/python_21_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_21_3.JPG -------------------------------------------------------------------------------- /imgs/python_21_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_21_4.JPG -------------------------------------------------------------------------------- /imgs/python_21_4_cross_import.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_21_4_cross_import.JPG -------------------------------------------------------------------------------- /imgs/python_21_4_dict.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_21_4_dict.JPG -------------------------------------------------------------------------------- /imgs/python_21_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_21_5.JPG -------------------------------------------------------------------------------- /imgs/python_21_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_21_6.JPG -------------------------------------------------------------------------------- /imgs/python_21_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_21_7.JPG -------------------------------------------------------------------------------- /imgs/python_21_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_21_8.JPG -------------------------------------------------------------------------------- /imgs/python_21_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_21_9.JPG -------------------------------------------------------------------------------- /imgs/python_22_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_22_1.JPG -------------------------------------------------------------------------------- /imgs/python_22_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_22_2.JPG -------------------------------------------------------------------------------- /imgs/python_22_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_22_3.JPG -------------------------------------------------------------------------------- /imgs/python_22_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_22_4.JPG -------------------------------------------------------------------------------- /imgs/python_22_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_22_5.JPG -------------------------------------------------------------------------------- /imgs/python_23_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_23_1.JPG -------------------------------------------------------------------------------- /imgs/python_23_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_23_2.JPG -------------------------------------------------------------------------------- /imgs/python_23_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_23_3.JPG -------------------------------------------------------------------------------- /imgs/python_23_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_23_4.JPG -------------------------------------------------------------------------------- /imgs/python_24_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_24_1.JPG -------------------------------------------------------------------------------- /imgs/python_24_10.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_24_10.JPG -------------------------------------------------------------------------------- /imgs/python_24_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_24_2.JPG -------------------------------------------------------------------------------- /imgs/python_24_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_24_3.JPG -------------------------------------------------------------------------------- /imgs/python_24_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_24_4.JPG -------------------------------------------------------------------------------- /imgs/python_24_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_24_5.JPG -------------------------------------------------------------------------------- /imgs/python_24_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_24_6.JPG -------------------------------------------------------------------------------- /imgs/python_24_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_24_7.JPG -------------------------------------------------------------------------------- /imgs/python_24_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_24_8.JPG -------------------------------------------------------------------------------- /imgs/python_24_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_24_9.JPG -------------------------------------------------------------------------------- /imgs/python_25_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_25_1.JPG -------------------------------------------------------------------------------- /imgs/python_26_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_1.JPG -------------------------------------------------------------------------------- /imgs/python_26_10.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_10.JPG -------------------------------------------------------------------------------- /imgs/python_26_11.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_11.JPG -------------------------------------------------------------------------------- /imgs/python_26_12.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_12.JPG -------------------------------------------------------------------------------- /imgs/python_26_13.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_13.JPG -------------------------------------------------------------------------------- /imgs/python_26_14.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_14.JPG -------------------------------------------------------------------------------- /imgs/python_26_15.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_15.JPG -------------------------------------------------------------------------------- /imgs/python_26_16.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_16.JPG -------------------------------------------------------------------------------- /imgs/python_26_17.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_17.JPG -------------------------------------------------------------------------------- /imgs/python_26_18.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_18.JPG -------------------------------------------------------------------------------- /imgs/python_26_19.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_19.JPG -------------------------------------------------------------------------------- /imgs/python_26_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_2.JPG -------------------------------------------------------------------------------- /imgs/python_26_20.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_20.JPG -------------------------------------------------------------------------------- /imgs/python_26_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_3.JPG -------------------------------------------------------------------------------- /imgs/python_26_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_4.JPG -------------------------------------------------------------------------------- /imgs/python_26_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_5.JPG -------------------------------------------------------------------------------- /imgs/python_26_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_6.JPG -------------------------------------------------------------------------------- /imgs/python_26_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_7.JPG -------------------------------------------------------------------------------- /imgs/python_26_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_8.JPG -------------------------------------------------------------------------------- /imgs/python_26_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_26_9.JPG -------------------------------------------------------------------------------- /imgs/python_27_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_27_1.JPG -------------------------------------------------------------------------------- /imgs/python_27_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_27_2.JPG -------------------------------------------------------------------------------- /imgs/python_27_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_27_3.JPG -------------------------------------------------------------------------------- /imgs/python_27_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_27_4.JPG -------------------------------------------------------------------------------- /imgs/python_27_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_27_5.JPG -------------------------------------------------------------------------------- /imgs/python_27_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_27_6.JPG -------------------------------------------------------------------------------- /imgs/python_27_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_27_7.JPG -------------------------------------------------------------------------------- /imgs/python_28_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_1.JPG -------------------------------------------------------------------------------- /imgs/python_28_10.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_10.JPG -------------------------------------------------------------------------------- /imgs/python_28_11.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_11.JPG -------------------------------------------------------------------------------- /imgs/python_28_12.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_12.JPG -------------------------------------------------------------------------------- /imgs/python_28_13.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_13.JPG -------------------------------------------------------------------------------- /imgs/python_28_14.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_14.JPG -------------------------------------------------------------------------------- /imgs/python_28_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_2.JPG -------------------------------------------------------------------------------- /imgs/python_28_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_3.JPG -------------------------------------------------------------------------------- /imgs/python_28_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_4.JPG -------------------------------------------------------------------------------- /imgs/python_28_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_5.JPG -------------------------------------------------------------------------------- /imgs/python_28_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_6.JPG -------------------------------------------------------------------------------- /imgs/python_28_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_7.JPG -------------------------------------------------------------------------------- /imgs/python_28_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_8.JPG -------------------------------------------------------------------------------- /imgs/python_28_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_28_9.JPG -------------------------------------------------------------------------------- /imgs/python_29_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_29_1.JPG -------------------------------------------------------------------------------- /imgs/python_29_10.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_29_10.JPG -------------------------------------------------------------------------------- /imgs/python_29_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_29_2.JPG -------------------------------------------------------------------------------- /imgs/python_29_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_29_3.JPG -------------------------------------------------------------------------------- /imgs/python_29_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_29_4.JPG -------------------------------------------------------------------------------- /imgs/python_29_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_29_5.JPG -------------------------------------------------------------------------------- /imgs/python_29_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_29_6.JPG -------------------------------------------------------------------------------- /imgs/python_29_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_29_7.JPG -------------------------------------------------------------------------------- /imgs/python_29_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_29_8.JPG -------------------------------------------------------------------------------- /imgs/python_29_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_29_9.JPG -------------------------------------------------------------------------------- /imgs/python_2_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_2_1.JPG -------------------------------------------------------------------------------- /imgs/python_2_10.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_2_10.JPG -------------------------------------------------------------------------------- /imgs/python_2_11.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_2_11.JPG -------------------------------------------------------------------------------- /imgs/python_2_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_2_2.JPG -------------------------------------------------------------------------------- /imgs/python_2_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_2_3.JPG -------------------------------------------------------------------------------- /imgs/python_2_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_2_4.JPG -------------------------------------------------------------------------------- /imgs/python_2_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_2_5.JPG -------------------------------------------------------------------------------- /imgs/python_2_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_2_6.JPG -------------------------------------------------------------------------------- /imgs/python_2_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_2_7.JPG -------------------------------------------------------------------------------- /imgs/python_2_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_2_8.JPG -------------------------------------------------------------------------------- /imgs/python_2_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_2_9.JPG -------------------------------------------------------------------------------- /imgs/python_30_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_30_1.JPG -------------------------------------------------------------------------------- /imgs/python_30_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_30_2.JPG -------------------------------------------------------------------------------- /imgs/python_30_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_30_3.JPG -------------------------------------------------------------------------------- /imgs/python_30_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_30_4.JPG -------------------------------------------------------------------------------- /imgs/python_31_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_31_1.JPG -------------------------------------------------------------------------------- /imgs/python_31_10.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_31_10.JPG -------------------------------------------------------------------------------- /imgs/python_31_11.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_31_11.JPG -------------------------------------------------------------------------------- /imgs/python_31_12.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_31_12.JPG -------------------------------------------------------------------------------- /imgs/python_31_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_31_2.JPG -------------------------------------------------------------------------------- /imgs/python_31_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_31_3.JPG -------------------------------------------------------------------------------- /imgs/python_31_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_31_4.JPG -------------------------------------------------------------------------------- /imgs/python_31_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_31_5.JPG -------------------------------------------------------------------------------- /imgs/python_31_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_31_6.JPG -------------------------------------------------------------------------------- /imgs/python_31_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_31_7.JPG -------------------------------------------------------------------------------- /imgs/python_31_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_31_8.JPG -------------------------------------------------------------------------------- /imgs/python_31_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_31_9.JPG -------------------------------------------------------------------------------- /imgs/python_32_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_32_1.JPG -------------------------------------------------------------------------------- /imgs/python_32_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_32_2.JPG -------------------------------------------------------------------------------- /imgs/python_32_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_32_3.JPG -------------------------------------------------------------------------------- /imgs/python_32_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_32_4.JPG -------------------------------------------------------------------------------- /imgs/python_32_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_32_5.JPG -------------------------------------------------------------------------------- /imgs/python_32_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_32_6.JPG -------------------------------------------------------------------------------- /imgs/python_32_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_32_7.JPG -------------------------------------------------------------------------------- /imgs/python_33_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_33_1.JPG -------------------------------------------------------------------------------- /imgs/python_33_10.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_33_10.JPG -------------------------------------------------------------------------------- /imgs/python_33_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_33_2.JPG -------------------------------------------------------------------------------- /imgs/python_33_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_33_3.JPG -------------------------------------------------------------------------------- /imgs/python_33_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_33_4.JPG -------------------------------------------------------------------------------- /imgs/python_33_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_33_5.JPG -------------------------------------------------------------------------------- /imgs/python_33_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_33_6.JPG -------------------------------------------------------------------------------- /imgs/python_33_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_33_7.JPG -------------------------------------------------------------------------------- /imgs/python_33_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_33_8.JPG -------------------------------------------------------------------------------- /imgs/python_33_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_33_9.JPG -------------------------------------------------------------------------------- /imgs/python_34_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_34_1.JPG -------------------------------------------------------------------------------- /imgs/python_34_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_34_2.JPG -------------------------------------------------------------------------------- /imgs/python_34_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_34_3.JPG -------------------------------------------------------------------------------- /imgs/python_34_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_34_4.JPG -------------------------------------------------------------------------------- /imgs/python_34_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_34_5.JPG -------------------------------------------------------------------------------- /imgs/python_3_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_3_1.JPG -------------------------------------------------------------------------------- /imgs/python_3_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_3_2.JPG -------------------------------------------------------------------------------- /imgs/python_3_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_3_3.JPG -------------------------------------------------------------------------------- /imgs/python_3_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_3_4.JPG -------------------------------------------------------------------------------- /imgs/python_3_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_3_5.JPG -------------------------------------------------------------------------------- /imgs/python_3_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_3_6.JPG -------------------------------------------------------------------------------- /imgs/python_3_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_3_7.JPG -------------------------------------------------------------------------------- /imgs/python_4_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_1.JPG -------------------------------------------------------------------------------- /imgs/python_4_10.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_10.JPG -------------------------------------------------------------------------------- /imgs/python_4_11.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_11.JPG -------------------------------------------------------------------------------- /imgs/python_4_12.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_12.JPG -------------------------------------------------------------------------------- /imgs/python_4_13.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_13.JPG -------------------------------------------------------------------------------- /imgs/python_4_14.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_14.JPG -------------------------------------------------------------------------------- /imgs/python_4_15.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_15.JPG -------------------------------------------------------------------------------- /imgs/python_4_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_2.JPG -------------------------------------------------------------------------------- /imgs/python_4_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_3.JPG -------------------------------------------------------------------------------- /imgs/python_4_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_4.JPG -------------------------------------------------------------------------------- /imgs/python_4_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_5.JPG -------------------------------------------------------------------------------- /imgs/python_4_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_6.JPG -------------------------------------------------------------------------------- /imgs/python_4_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_7.JPG -------------------------------------------------------------------------------- /imgs/python_4_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_8.JPG -------------------------------------------------------------------------------- /imgs/python_4_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_4_9.JPG -------------------------------------------------------------------------------- /imgs/python_5_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_5_1.JPG -------------------------------------------------------------------------------- /imgs/python_5_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_5_2.JPG -------------------------------------------------------------------------------- /imgs/python_5_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_5_3.JPG -------------------------------------------------------------------------------- /imgs/python_5_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_5_4.JPG -------------------------------------------------------------------------------- /imgs/python_5_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_5_5.JPG -------------------------------------------------------------------------------- /imgs/python_5_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_5_6.JPG -------------------------------------------------------------------------------- /imgs/python_5_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_5_7.JPG -------------------------------------------------------------------------------- /imgs/python_5_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_5_8.JPG -------------------------------------------------------------------------------- /imgs/python_5_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_5_9.JPG -------------------------------------------------------------------------------- /imgs/python_6_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_6_1.JPG -------------------------------------------------------------------------------- /imgs/python_7_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_7_1.JPG -------------------------------------------------------------------------------- /imgs/python_7_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_7_2.JPG -------------------------------------------------------------------------------- /imgs/python_7_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_7_3.JPG -------------------------------------------------------------------------------- /imgs/python_7_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_7_4.JPG -------------------------------------------------------------------------------- /imgs/python_7_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_7_5.JPG -------------------------------------------------------------------------------- /imgs/python_7_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_7_6.JPG -------------------------------------------------------------------------------- /imgs/python_7_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_7_7.JPG -------------------------------------------------------------------------------- /imgs/python_8_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_8_1.JPG -------------------------------------------------------------------------------- /imgs/python_8_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_8_2.JPG -------------------------------------------------------------------------------- /imgs/python_8_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_8_3.JPG -------------------------------------------------------------------------------- /imgs/python_8_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_8_4.JPG -------------------------------------------------------------------------------- /imgs/python_8_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_8_5.JPG -------------------------------------------------------------------------------- /imgs/python_9_1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_9_1.JPG -------------------------------------------------------------------------------- /imgs/python_9_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_9_2.JPG -------------------------------------------------------------------------------- /imgs/python_9_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_9_3.JPG -------------------------------------------------------------------------------- /imgs/python_9_4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_9_4.JPG -------------------------------------------------------------------------------- /imgs/python_9_5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_9_5.JPG -------------------------------------------------------------------------------- /imgs/python_9_6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_9_6.JPG -------------------------------------------------------------------------------- /imgs/python_9_7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_9_7.JPG -------------------------------------------------------------------------------- /imgs/python_9_8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_9_8.JPG -------------------------------------------------------------------------------- /imgs/python_9_9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaxz1986/python_learning_notes/199e60da307a1c27794eb8d825beb964a5d97397/imgs/python_9_9.JPG --------------------------------------------------------------------------------