FrancisLeon / Reinforement-Learning-

0 stars 0 forks source link

introc to python #6

Open FrancisLeon opened 7 years ago

FrancisLeon commented 7 years ago

3.1 Using Data Types

Methods

We can call (or invoke) a method by using a variable name, followed by the dot operator (.), followed by the method name, followed by a list of arguments delimited by commas and enclosed in parentheses. As a simple example, Python's built-in int type has a method named bit_length(), so you can determine the number of bits in the binary representation of an int value as follows:

x = 3 ** 100 
bits = x.bit_length() 
stdio.writeln(bits)

The key difference between a function and a method is that a method is associated with a specified object. You can think of this specified object as an extra argument that gets passed to a function, in addition to the ordinary methods argument. ????

A User-Defined Data Type

File conventions. The code that defines a user-defined data type resides in a .py file. By convention, we define each data type in a distinct .py file, with the same name as the data type (but not capitalized). Thus, the Charge data type is found in a file named charge.py. To compose a client program that uses the Charge data type, we put the following import statement at the top of the client .py file:

from charge import Charge

Note that the format of the import statement that we use with user-defined data types differs from the format we use with functions.

API

Programmers normally think in terms of a contract between the client and the implementation that is a clear specification of what the implementation is to do. You have been able to compose programs that are clients of math and random and other standard Python modules because of an informal contract (an English-language description of what they are supposed to do) along with a precise specification of the signatures of the functions that are available for use. Collectively, this information is known as an application programming interface (API). The same mechanism is effective for user-defined modules. The API allows any client to use the module without having to examine the code that defines the module. When we compose a new module, we always provide an API.

FrancisLeon commented 7 years ago

3.3 Designing Data Types

Immutability Immutable data types. The purpose of many data types is to encapsulate values that do not change. For example, a programmer implementing a Complex client might reasonably expect to compose the code z = z0, thus setting two variables to reference the same Complex object, in the same way as for floats or integers. But if Complex were mutable and the object referenced by z were to change after the assignment z = z0, then the object referenced by z0 would also change (they are aliases, or both references to the same object). Conceptually, changing the value of z would change the value of z0! This unexpected result, known as an aliasing bug, comes as a surprise to many newcomers to object-oriented programming.