Reading structured binary data with the struct module
Generally speaking, when working with your own files, you probably don't want to read or write binary data in Python. For very simple storage needs, it's usually best to use textual input and output as described earlier. For more sophisticated applications, Python provides the ability to easily read or write arbitrary Python objects pickling, described later in this chapter . This ability is much less error prone than directly writing and reading your own binary data and is highly recommended....
Inheritance
self.x x self.y y class Square Shape def _init_ self, r 1, x 0, y 0 There are generally two requirements in using an inherited class in Python, both of which you can see in the bolded code in the Circle and Square classes. The first requirement is defining the inheritance hierarchy, which you do by giving the classes inherited from, in parentheses, immediately after the name of the class being defined with the class keyword. In the previous code, Circle and Square both inherit from Shape. The...
Positional parameters
The simplest way to pass parameters to a function in Python is by position. In the first line of the function, you specify definition variable names for each parameter when the function is called, the parameters used in the calling code are matched to the function's parameter variables based on their order. The following function computes x to the power of y gt gt gt def power x, y r 1 This method requires that the number of parameters used by the calling code exactly match the number of...
Browsing Python documentation on your computer
Many distributions of Python include the full documentation by default. In some Linux distributions, the documentation is a separate package that you need to install separately. In most cases, however, full documentation is already on your computer and easily accessible. Accessing help in the interactive shell or at a command line In chapter 2, you saw how to use the help command in the interactive interpreter to access online help for any Python module or object gt gt gt help int Help on int...
Instance variables
Instance variables are the most basic feature of OOP. Take a look at the Circle class again radius is an instance variable of Circle instances. That is, each instance of the Circle class has its own copy of radius, and the value stored in that copy may be different from the values stored in the radius variable in other instances. In Python, you can create instance variables as necessary by assigning to a field of a class instance If the variable doesn't already exist, it's created...
Statements blocks and indentation
Because the control flow constructs we encountered in this chapter are the first to make use of blocks and indentation, this is a good time to revisit the subject. Python uses the indentation of the statements to determine the delimitation of the different blocks or bodies of the control-flow constructs. A block consists of one or more statements, which are usually separated by newlines. Examples of Python statements are the assignment statement, function calls, the print function, the...
Class variables
A class variable is a variable associated with a class, not an instance of a class, and is accessed by all instances of the class, in order to keep track of some class-level information, such as how many instances of the class have been created at any point in time. Python provides class variables, although using them requires slightly more effort than in most other languages. Also, you need to watch out for an interaction between class and instance variables. A class variable is created by an...
Variable numbers of arguments
Python functions can also be defined to handle variable numbers of arguments. You can do this two different ways. One way handles the relatively familiar case where you wish to collect an unknown number of arguments at the end of the argument list into a list. The other method can collect an arbitrary number of keyword-passed arguments, which have no correspondingly named parameter in the function parameter list, into a dictionary. These two mechanisms are discussed next. DEALING WITH AN...

