Learning Python Quotes

Rate this book
Clear rating
Learning Python Learning Python by Mark Lutz
3,191 ratings, 4.01 average rating, 177 reviews
Open Preview
Learning Python Quotes Showing 31-60 of 114
“Every time you use an expression of the form object.attr where object is an instance or class object, Python searches the namespace tree from bottom to top, beginning with object, looking for the first attr it can find.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“On a related note, you can also code multiple __init__ methods within the same class, but only the last definition will be used; see Chapter 31 for more details on multiple method definitions.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Assignments to instance attributes create or change the names in the instance, rather than in the shared class. More generally, inheritance searches occur only on attribute references, not on assignment:”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Assignments to instance attributes create or change the names in the instance, rather than in the shared class.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“In Chapter 32, we’ll also meet Python static methods (akin to those in C++), which are just self-less functions that usually process class attributes.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“In general, though, any type of name assignment at the top level of a class statement creates a same-named attribute of the resulting class object.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“All the statements inside the class statement run when the class statement itself runs (not when the class is later called to make an instance).”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Like a def, a class statement is an object builder, and an implicit assignment — when run, it generates a class object and stores a reference to it in the name used in the header.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Inheritance is best at coding extensions based on direct customization (like our Manager specialization of Person). Composition is well suited to scenarios where multiple objects are aggregated into a whole and directed by a controller layer class. Inheritance passes calls up to reuse, and composition passes down to delegate.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“ZODB, for example, is similar to Python’s shelve but addresses many of its limitations, better supporting larger databases, concurrent updates, transaction processing, and automatic write-through on in-memory changes (shelves can cache objects and flush to disk at close time with their writeback option, but this has limitations: see other resources).”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Because shelves are Python objects containing Python objects, we can process them with normal Python syntax and development modes. Here, the interactive prompt effectively becomes a database client:”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“We’ll use from to load in our script, just because it’s a bit less to type.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“By coding functions and classes in module files, we’ve ensured that they naturally support reuse. And by coding our software as classes, we’ve ensured that it naturally supports extension.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“A better and less commonly used solution would be to use two underscores at the front of the method name only: __gatherAttrs for us. Python automatically expands such names to include the enclosing class’s name, which makes them truly unique when looked up by the inheritance search. This is a feature usually called pseudoprivate class attributes, which we’ll expand on in Chapter 31 and deploy in an expanded version of this class there.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“To minimize the chances of name collisions like this, Python programmers often prefix methods not meant for external use with a single underscore: _gatherAttrs in our case.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“but slots — and other “virtual” attributes — won’t be reported as instance data.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Still, object embedding, and design patterns based upon it, can be a very good fit when embedded objects require more limited interaction with the container than direct customization implies.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“The more important point here is that this Manager alternative is representative of a general coding pattern usually known as delegation — a composite-based structure that manages a wrapped object and propagates method calls to it.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“composites. We’ll explore this pattern in more detail in Chapter 31, which is really more about design than about Python. As a quick example, though, we could use this composition idea to code our Manager extension by embedding a Person, instead of inheriting from it.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Python’s super is like a box of chocolates — you never know what you’re going to get!”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Think about it: because we copied the original version, if we ever have to change the way raises are given (and we probably will), we’ll have to change the code in two places, not one. Although this is a small and artificial example, it’s also representative of a universal issue — anytime you’re tempted to program by copying code this way, you probably want to look for a better approach.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“The problem here is a very general one: anytime you copy code with cut and paste, you essentially double your maintenance effort in the future.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Because printing runs __str__ and the interactive prompt echoes results with __repr__, this can provide both target audiences with an appropriate display.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Sometimes classes provide both a __str__ for user-friendly displays and a __repr__ with extra details for developers to view.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Design note: as we’ll learn in Chapter 30, the __repr__ method is often used to provide an as-code low-level display of an object when present, and __str__ is reserved for more user-friendly informational displays like ours here.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Technically, __str__ is preferred by print and str, and __repr__ is used as a fallback for these roles and in all other contexts.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“operator overloading — coding methods in a class that intercept and process built-in operations when run on the class’s instances.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Although other techniques (such as enclosing scope reference closures) can save details, too, instance attributes make this very explicit and easy to understand.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Despite its syntax details, Python’s class system really is largely just a matter of searching for an attribute in a tree of objects, along with a special first argument for functions.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming
“Operator overloading is coded in a Python class with specially named methods; they all begin and end with double underscores to make them unique. These are not built-in or reserved names; Python just runs them automatically when an instance appears in the corresponding operation.”
Mark Lutz, Learning Python: Powerful Object-Oriented Programming