Learning Python: Powerful Object-Oriented Programming
Rate it:
Kindle Notes & Highlights
16%
Flag icon
All collection data types in Python can nest inside each other arbitrarily:
17%
Flag icon
in dictionaries, there’s just one value per key, but there may be many keys per value.
17%
Flag icon
In practice, dictionaries tend to be best for data with labeled components, as well as structures that can benefit from quick, direct lookups by name, instead of slower linear searches.
17%
Flag icon
they also may be better for sparse collections and collections that grow at arbitrary positions.
18%
Flag icon
tuples are best thought of as object reference arrays; tuples store access points to other objects (references),
18%
Flag icon
List comprehensions are really sequence operations — they always build new lists, but they may be used to iterate over any sequence objects, including tuples, strings, and other lists.
18%
Flag icon
Given this, they may be better called iteration tools.
18%
Flag icon
rule about tuple immutability applies only to the top level of the tuple itself, not to its contents.
18%
Flag icon
the immutability of tuples provides some integrity
18%
Flag icon
File iterators are best for reading lines
18%
Flag icon
You can avoid buffering with extra open arguments, but it may impede performance.
18%
Flag icon
close is often optional: auto-close on collection
18%
Flag icon
, in Python an object’s memory space is automatically reclaimed as soon as the object is no longer referenced anywhere in the program.
18%
Flag icon
Notice that the third readline call returns an empty string; this is how Python file methods tell you that you’ve reached the end of the file
18%
Flag icon
When you read a binary data file you get back a bytes object — a sequence of small integers that represent absolute byte values
18%
Flag icon
(which may or may not correspond to characters),
18%
Flag icon
text files
Peter
another italic combo
19%
Flag icon
we can run them through eval, a built-in function that treats a string as a piece of executable program code (technically, a string containing a Python expression):
19%
Flag icon
The pickle module is a more advanced tool that allows us to store almost any Python object in a file directly, with no to- or from-string conversion requirement on our part. It’s like a super-general data formatting and parsing utility.
19%
Flag icon
shelve is a tool that uses pickle to store Python objects in an access-by-key filesystem, which is beyond our scope here (though you will get to see an example of shelve in action in Chapter 28, and other pickle examples in Chapter 31 and Chapter 37).
19%
Flag icon
you can use the binary file-processing modes 'wb' and 'rb' to process a simpler binary file, such as an image or audio file, as a whole without having to unpack its contents; in such cases your code might pass it unparsed to other files or tools.
19%
Flag icon
classificationsObject type Category Mutable? Numbers (all) Numeric No Strings (all) Sequence No Lists Sequence Yes Dictionaries Mapping Yes Tuples Sequence No Files Extension N/A Sets Set Yes Frozenset Set No bytearray Sequence Yes
19%
Flag icon
Lists, dictionaries, and tuples can hold any kind of object. Sets can contain any type of immutable object.
19%
Flag icon
Lists, dictionaries, and tuples can be arbitrarily nested. Lists, dictionaries, and sets can dynamically grow and shrink.
19%
Flag icon
empty-limit slices and the dictionary copy method only make top-level copies; that is, they do not copy nested data structures, if any are present. If you need a complete, fully independent copy of a deeply nested data structure (like the various record structures we’ve coded in recent chapters), use the standard copy module,
19%
Flag icon
Python recognizes any empty data structure as false and any nonempty data structure as true.
20%
Flag icon
More generally, the notions of true and false are intrinsic properties of every object in Python — each object is either true or false, as follows:
20%
Flag icon
Numbers are false if zero, and true otherwise. Other objects are false if emp...
This highlight has been truncated due to consecutive passage length restrictions.
20%
Flag icon
it’s common to see Python programmers code tests like if X:, which, assuming X is a string, is the same as if X != '':.
20%
Flag icon
The largest point to notice here is that everything in a Python system is an object type and may be processed by your Python programs. For instance, you can pass a class to a function, assign it to a variable, stuff it in a list or dictionary, and so on.
20%
Flag icon
for reasons introduced in Chapter 4, manual type testing is usually not the right thing to do in Python, since it limits your code’s flexibility.
20%
Flag icon
Because types can be subclassed in Python today, the isinstance technique is generally recommended.
20%
Flag icon
remember that repetition, concatenation, and slicing copy only the top level of their operand objects,
20%
Flag icon
if a collection object contains a reference to itself, it’s called a cyclic object.
20%
Flag icon
don’t use cyclic references unless you really need to, and make sure you anticipate them in programs that must care.
20%
Flag icon
(Recall that single-item tuples require a trailing comma.)
20%
Flag icon
Programs are composed of modules. Modules contain statements. Statements contain expressions. Expressions create and process objects.
20%
Flag icon
The one new syntax component in Python is the colon character (:). All Python compound statements — statements that have other statements nested inside them — follow the same general pattern of a header line terminated in a colon, followed by a nested block of code
21%
Flag icon
Readability matters, and indentation is a major component of readability.
21%
Flag icon
The end of a line terminates the statement on that line (without semicolons). Nested statements are blocked and associated by their physical indentation (without braces).
21%
Flag icon
Sequence-unpacking assignments also give rise to another common coding idiom in Python — assigning an integer series to a set of variables: >>> red, green, blue = range(3) >>> red, blue (0, 2)
22%
Flag icon
tuple assignment at work is for splitting a sequence into its front and the rest in loops like this:
22%
Flag icon
This is similar in spirit to slicing, but not exactly the same — a sequence unpacking assignment always returns a list for multiple matched items, whereas slicing returns a sequence of the same type as the object sliced:
22%
Flag icon
Here, changing b only changes b because numbers do not support in-place changes. As long as the object assigned is immutable, it’s irrelevant if more than one name references it.
22%
Flag icon
concatenation is less prone to the side effects of shared object references but will generally run slower than the in-place equivalent.
22%
Flag icon
Concatenation makes a new object
22%
Flag icon
But += really means extend
22%
Flag icon
objects have a type (e.g., integer, list) and may be mutable or not. Names (a.k.a. variables), on the other hand, are always just references to objects; they have no notion of mutability and have no associated type information, apart from the type of the object they happen to reference at a given point in time.
23%
Flag icon
Technically, nested appearances display with repr and top-level objects with str
23%
Flag icon
If you’ve used languages like C or Pascal, you might be interested to know that there is no switch or case statement in Python that selects an action based on a variable’s value. Instead, you usually code multiway branching as a series of if/elif tests, as in the prior example, and occasionally by indexing dictionaries or searching lists.
« Prev 1 3