Python笔记

inheritance search occurs only on attribute reference, not assignment: assigning to an object’s attribute always changes that object, and no other. (Unless the attribute assignment operation has been redefined by a class with the __setattr__ operator overloading method to do something unique.)

Don’t code C in Python. A note to C/C++ programmers: you don’t need to type parentheses around tests in if and while headers (e.g., if (X==1):); you can if you like (any expression can be enclosed in parentheses), but they are fully superfluous in this context. Also, do not terminate all your statements with a semicolon; it’s technically legal to do this in Python as well, but is totally useless, unless you’re placing more than one statement on a single line (the end of a line normally terminates a statement). And remember, don’t embed assignment statements in while loop tests, and don’t use { } around blocks (indent your nested code blocks consistently instead).

Don’t expect results from functions that change objects in-place . In-place change operations like the list.append( ) and list.sort( ) methods of Chapter 6 do not return a value (other than None); call them without assigning the result. It’s not uncommon for beginners to say something like mylist=mylist.append(X) to try to get the result of an append; instead, this assigns mylist to None, rather than the modified list (in fact, you’ll lose a reference to the list altogether).

–Learning Python, 2ed Edition

Page 1 of 11