zoukankan      html  css  js  c++  java
  • python-内置函数及捕获异常

     

    eval:把字符串转换成有效表达式

    repr:把有效表达式转换成字符串

     

    round(...)

    round(number[, ndigits]) -> number

     

        Round a number to a given precision in decimal digits (default 0 digits).

        This returns an int when called with one argument, otherwise the

    same type as the number. ndigits may be negative.

    pow(x, y, z=None, /)

    Equivalent to x**y (with two arguments) or x**y % z (with three arguments)

     

    Some types, such as ints, are able to use a more efficient algorithm when

    invoked using the three argument form.

     

    class map(object)

     |  map(func, *iterables) --> map object

     |

     |  Make an iterator that computes the function using arguments from

     |  each of the iterables.  Stops when the shortest iterable is exhausted.

     

    >>> a

    [1, 2, 3]

    >>> b

    ['a', 'b', 'c']

    >>> dict(zip(a,b))

    {1: 'a', 2: 'b', 3: 'c'}

     

    >>> mylist.extend([1,2,3])

    >>> mylist

    [2, 1, 3, 2, 1, 4, 3, 2, 1, 'a', 'a', 1, 2, 3]

    >>> mylist.append([1,2,3])

    >>> mylist

    [2, 1, 3, 2, 1, 4, 3, 2, 1, 'a', 'a', 1, 2, 3, [1, 2, 3]]

     

    -------------------------------------------以上是内置函数------------------------------------------

     

    异常,错误

    程序没法处理的任务,会抛出异常

     

     

    错误一般是,逻辑错误,语法错误,无法生成结果等

     

     

    >>> a

    Traceback (most recent call last):

      File "<stdin>", line 1, in <module>

    NameError: name 'a' is not defined

     

     

    >>> 1 / 0

    Traceback (most recent call last):

      File "<stdin>", line 1, in <module>

    ZeroDivisionError: division by zero

     

    >>> if a = b

      File "<stdin>", line 1

        if a = b

             ^

    SyntaxError: invalid syntax

     

    >>> b[4]

    Traceback (most recent call last):

      File "<stdin>", line 1, in <module>

    IndexError: list index out of range

     

    python,大部分异常都是基于Exception 这个类

     

    KeyboardInterrupt  Exception  exit

    基类 

     

    Exception  

     

    python可以通过 

    try语句检测异常:

    要检测异常的语句

         except 语句来处理异常:

     

    >>> try:

    ...     print(a)

    ... except NameError:

    ...     print('ABC')

    ...

    ABC

     

    >>> try:

    ...     print(a)

    ... except Exception as error:

    ...     print(error)

    ...

    name 'a' is not defined

     

    try:

       要检测异常的语句

    except 检测的异常(如果我们使用Execption)则是捕捉所有异常:#但是不建议大家这么做,因为有些异常是我们需要去真实看到的,

       执行捕获异常之后的事情

    else:

    用来在没有任何异常情况下执行这里的内容

    finally:

       不管异常是否发生,这里面写的语句都会执行

    我们一般用来关闭socket,关闭文件,回收线程,进程创建释放,数据库连接,mysql句柄,做一些对象内存释放的工作,

     

     

    >>> try:

    ...     print(a)

    ... except Exception:

    ...     print('abc')

    ... else:

    ...     print('--------')

    ... finally:

    ...     print('********')

    ...

    abc

    ********

     

    raise 可以抛出异常

     

    断言

    assert当判断的语句为false 那么会抛出一个AssertionError 异常

     

    断言一般用来判断一些bool语句,在断言成功时不采取任何措施,否则触发AssertionError(断言错误)的异常,

     

    try:

    assert 1 == 0

    except AssertionError:

    print('not equal')

     

    with语句,with语句其实和tryfinally语句是一样的,只不过,他只对支持上下文管理协议context

    management protocol,比如我们的mysql链接数据库,会在完成业务之后他有关闭,打开文件也会有关闭文件,

    close()

     

    我们通过with语句打开一个文件对象,如果没有出错,文件对象就是file

    之后我们做的一系列操作不论是否会发生错误,抛出异常,都会执行内存清理的语句,刷新缓冲区,关闭文件等

     

    -----------------------------------------------异常------------------------------------------------

     

  • 相关阅读:
    事务四大特征:原子性,一致性,隔离性和持久性(ACID)
    解决“要登录到这台远程计算机,你必须被授予”
    SqlServer_查看SQLServer版本信息
    sed 查找文件的某一行内容
    linux echo命令的-n、-e两个参数
    在.Net中进行跨线程的控件操作(上篇:Control.Invoke)
    .NET性能优化方面的总结
    SQLSERVER2008 显示列信息,包含扩展属性
    C#4.0新特性:可选参数,命名参数,Dynamic
    浅谈.net中的params关键字
  • 原文地址:https://www.cnblogs.com/askDing/p/8525715.html
Copyright © 2011-2022 走看看