📄 chap1.txt
字号:
file-like objects that do not attach to actual files should
not implement this method, but implementing it to always
return '0' is probably a better approach.
FILE.mode
Attribute containing the mode of the file, normally
identical to the 'mode' argument passed to the object's
initializer.
FILE.name
The name of the file. For file-like objects without a
filesystem name, some string identifying the object should
be put into this attribute.
FILE.read([size=sys.maxint])
Return a string containing up to 'size' bytes of content
from the file. Stop the read if an EOF is encountered or
upon other condition that makes sense for the object type.
Move the file position forward immediately past the read in
bytes. A negative 'size' argument is treated as the
default value.
FILE.readline([size=sys.maxint])
Return a string containing one line from the file,
including the trailing newline, if any. A maximum of
'size' bytes are read. The file position is moved forward
past the read. A negative 'size' argument is treated as the
default value.
FILE.readlines([size=sys.maxint])
Return a list of lines from the file, each line including
its trailing newline. If the argument 'size' is given,
limit the read to -approximately- 'size' bytes worth of
lines. The file position is moved forward past the read in
bytes. A negative 'size' argument is treated as the
default value.
FILE.seek(offset [,whence=0])
Move the file position by 'offset' bytes (positive or
negative). The argument 'whence' specifies where the
initial file position is prior to the move: 0 for BOF; 1
for current position; 2 for EOF.
FILE.tell()
Return the current file position.
FILE.truncate([size=0])
Truncate the file contents (it become 'size' length).
FILE.write(s)
Write the string 's' to the file, starting at the current
file position. The file position is moved forward past the
written bytes.
FILE.writelines(lines)
Write the lines in the sequence 'lines' to the file. No
newlines are added during the write. The file position is
moved forward past the written bytes.
FILE.xreadlines()
Memory-efficient iterator over lines in a file. In Python
2.2+, you might implement this as a generator that returns
one line per each 'yield'.
SEE ALSO, [xreadlines]
=================================================================
BUILTIN -- int : New-style base class for integer objects
=================================================================
BUILTIN -- long : New-style base class for long integers
=================================================================
In Python, there are two standard datatypes for representing
integers. Objects of type 'IntType' have a fixed range that
depends on the underlying platform--usually between plus and
minus 2**31. Objects of type 'LongType' are unbounded in size.
In Python 2.2+, operations on integers that exceed the range of
an 'int' object results in automatic promotion to 'long'
objects. However, no operation on a 'long' will demote the
result back to an 'int' object (even if the result is of small
magnitude)--with the exception of the `int()` function, of
course.
From a user point of view ints and longs provide exactly the same
interface. The difference between them is only in underlying
implementation, with ints typically being significantly faster to
operate on (since they use raw CPU instructions fairly directly).
Most of the magic methods integers have are shared by floating
point numbers as well and are discussed below. For example,
consult the discussion of `float.__mul__()` for information on
the corresponding `int.__mul__()` method. The special capability
that integers have over floating point numbers is their ability
to perform bitwise operations.
Under Python 2.2+, you may create a custom datatype that
inherits from 'int' or 'long'; under earlier versions, you
would need to manually define all the magic methods you wished
to utilize (generally a lot of work, and probably not worth
it).
Each binary bit operation has a left-associative and a
right-associative version. If you define both versions and
perform an operation on two custom objects, the
left-associative version is chosen. However, if you perform an
operation with a basic 'int' and a custom object, the
custom right-associative method will be chosen over the basic
operation. For example:
>>> class I(int):
... def __xor__(self, other):
... return "XOR"
... def __rxor__(self, other):
... return "RXOR"
...
>>> 0xFF ^ 0xFF
0
>>> 0xFF ^ I(0xFF)
'RXOR'
>>> I(0xFF) ^ 0xFF
'XOR'
>>> I(0xFF) ^ I(0xFF)
'XOR'
METHODS:
int.__and__(self, other)
int.__rand__(self, other)
Return a bitwise-and between 'self' and 'other'.
Determines how a datatype responds to the '&' operator.
int.__hex__(self)
Return a hex string representing 'self'. Determines how a
datatype responds to the built-in `hex()` function.
int.__invert__(self)
Return a bitwise inversion of 'self'. Determines how a
datatype responds to the '~' operator.
int.__lshift__(self, other)
int.__rlshift__(self, other)
Return the result of bit-shifting 'self' to the left by
'other' bits. The right-associative version shifts 'other'
by 'self' bits. Determines how a datatype responds to the
'<<' operator.
int.__oct__(self)
Return an octal string representing 'self'. Determines how
a datatype responds to the built-in `oct()` function.
int.__or__(self, other)
int.__ror__(self, other)
Return a bitwise-or between 'self' and 'other'.
Determines how a datatype responds to the '|' operator.
int.__rshift__(self, other)
int.__rrshift__(self, other)
Return the result of bit-shifting 'self' to the right by
'other' bits. The right-associative version shifts 'other'
by 'self' bits. Determines how a datatype responds to the
'>>' operator.
int.__xor__(self, other)
int.__rxor__(self, other)
Return a bitwise-xor between 'self' and 'other'.
Determines how a datatype responds to the '^' operator.
SEE ALSO, [float], `int`, `long`, `sys.maxint`, [operator]
=================================================================
BUILTIN -- float : New-style base class for floating point numbers
=================================================================
Python floating point numbers are mostly implemented using the
underlying C floating point library of your platform; that is, to
a greater or lesser degree based on the IEEE 754 standard. A
complex number is just a Python object that wraps a pair of
floats with a few extra operations on these pairs.
DIGRESSION:
Although the details are far outside the scope of this book, a
general warning is in order. Floating point math is harder
than you think! If you think you -understand- just how complex
IEEE 754 math is, you are not yet aware of all of the
subtleties. By way of indication, Python luminary and
erstwhile professor of numeric computing Alex Martelli
commented in 2001 (on '<comp.lang.python>'):
Anybody who thinks he knows what he's doing when floating
point is involved IS either naive, or Tim Peters (well, it
COULD be W. Kahan I guess, but I don't think he writes here).
Fellow Python guru Tim Peters observed:
I find it's possible to be both (wink). But *nothing* about
fp comes easily to anyone, and even Kahan works his butt off
to come up with the amazing things that he does.
Peters illustrated further by way of Donald Knuth (_The Art of
Computer Programming_, Third Edition, Addison-Wesley, 1997; ISBN:
0201896842, vol. 2, p. 229):
Many serious mathematicians have attempted to analyze a
sequence of floating point operations rigorously, but found
the task so formidable that they have tried to be content
with plausibility arguments instead.
The trick about floating point numbers is that although they
are extremely useful for representing real-life (fractional)
quantities, operations on them do not obey the arithmetic rules
we learned in middle school: associativity, transitivity,
commutativity; moreover, many very ordinary-seeming numbers can
be represented only approximately with floating point numbers.
For example:
>>> 1./3
0.33333333333333331
>>> .3
0.29999999999999999
>>> 7 == 7./25 * 25
0
>>> 7 == 7./24 * 24
1
CAPABILITIES:
In the hierarchy of Python numeric types, floating point
numbers are higher up the scale than integers, and complex
numbers higher than floats. That is, operations on mixed types
get promoted upwards. However, the magic methods that make a
datatype "float-like" are strictly a subset of those associated
with integers. All of the magic methods listed below for
floats apply equally to ints and longs (or integer-like custom
datatypes). Complex numbers support a few addition methods.
Under Python 2.2+, you may create a custom datatype that
inherits from 'float' or 'complex'; under earlier versions, you
would need to manually define all the magic methods you wished
to utilize (generally a lot of work, and probably not worth
it).
Each binary operation has a left-associative and a
right-associative version. If you define both versions and
perform an operation on two custom objects, the left-associative
version is chosen. However, if you perform an operation with a
basic datatype and a custom object, the custom right-associative
method will be chosen over the basic operation. See the example
under [int].
METHODS:
float.__abs__(self)
Return the absolute value of 'self'. Determines how a
datatype responds to the built-in function `abs()`.
float.__add__(self, other)
float.__radd__(self, other)
Return the sum of 'self' and 'other'. Determines how a
datatype responds to the '+' operator.
float.__cmp__(self, other)
Return a value indicating the order of 'self' and 'other'.
Determines how a datatype responds to the numeric comparison
operators '<', '>', '<=', '>=', '==', '<>', and '!='. Also
determines the behavior of the built-in `cmp()` function.
Should return -1 for 'self<other', 0 for 'self==other', and
1 for 'self>other'. If other comparison methods are
defined, they take precedence over '.__cmp__()':
'.__ge__()', '.__gt__()', '.__le__()', and '.__lt__()'.
float.__div__(self, other)
float.__rdiv__(self, other)
Return the ratio of 'self' and 'other'. Determines how a
datatype responds to the '/' operator. In Python 2.3+,
this method will instead determine how a datatype responds
to the floor division operator '//'.
float.__divmod__(self, other)
float.__rdivmod__(self, other)
Return the pair '(div, remainder)'. Determines how a
datatype responds to the built-in `divmod()` function.
float.__floordiv__(self, other)
float.__rfloordiv__(self, other)
Return the number of whole times 'self' goes into 'other'.
Determines how a datatype responds to the Python 2.2+
floor division operator '//'.
float.__mod__(self, other)
float.__rmod__(self, other)
Return the modulo division of 'self' into 'other'.
Determines how a datatype responds to the '%' operator.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -