file,open和Inf,NaN

file的doc

>>> print file.__doc__
    file(name[, mode[, buffering]]) -> file object
    Open a file.  The mode can be 'r', 'w' or 'a' for     reading (default),
    writing or appending.  The file will be created if it doesn't exist
    when opened for writing or appending; it will be truncated when
    opened for writing.  Add a 'b' to the mode for binary files.
    Add a '+' to the mode to allow simultaneous reading and writing.
    If the buffering argument is given, 0 means unbuffered, 1 means line buffered, and larger numbers specify the buffer size.  The preferred way to open a file is with the builtin open() function.
    Add a 'U' to mode to open the file for input with  universal newline support.  Any line ending in the input file will be seen as a '\n' in Python.  Also, a file so opened gains the attribute 'newlines';
    the value for this attribute is one of None (no newline read yet), '\r', '\n', '\r\n' or a tuple containing all the newline types seen.
    'U' cannot be combined with 'w' or '+' mode.

open的doc

>>> print open.__doc__
    open(name[, mode[, buffering]]) -> file object
    Open a file using the file() type, returns a file object.  This is the preferred way to open a file.  See file.__doc__ for further information.

总结

  • file和open都可以对文件进行操作,但是在对文件打开操作,优先使用file

Inf和NaN

Inf表示无穷大,NaN表示not a number,在运算溢出等时候可能会产生此类结果。

inf可以用 ==来比较,NaN不可以用这个来比较,两个NaN是不相等的。

>>> float("Inf") == float("Inf")
    True
    >>> float("Inf") == 1
    False
    >>> float("NaN") == float("NaN")
    False
  • 如何判定NaN和Inf:用isinf(),isnan()
 import numpy as np
    >>> a = np.arange(3,dtype
    >>> a[0] = np.nan
    >>> a[1] = np.inf
    >>> a[2] = -np.inf
    >>> a[1]
    inf
    >>> a[0]
    nan
    >>> a[2]
    -inf
    >>> np.isnan(a[0])
    True
    >>> np.isinf(a[1])
    True
    >>> np.isinf(a[2])
    True
  • 用math判断
import math
    >>> math.isinf(float("inf"))
    True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值