python function - scope, exception handling

1, def statements and parameters

Keywords python defined function is def, the following format:

def function name ([argument], [argument], ....)           # value stored in the argument, after the function returns the argument will be destroyed.

2, return value and return statement

renturn function should return a value or expression           # If you use an expression, the return value is the result of the expression evaluation 

3, None value

None has a value referred to in Python, which represents no value. None is NoneType is a unique value data type, None must capitalize the first letter N. If you want something stored in the variable will not be confused with a real value, this value may not be useful.                              Return Value #Print () function is None 

 

4, the local and global scope

Local variables: Variables and variable assignment in the function being called .   # Local variables can not use the global scope, local scope can not use other local variable scope

Global variables: variable assignment outside of any function.  # Global variables can be read in the local scope 

Popular for local variables like you own something you can only use global variables are public, everyone is available, but you can not change the use and destruction of public things that really need to change if you need to submit application, python submit an application in a global statement.

Output:

5, Exception Handling

 Python program encounters an error or "abnormal", which means the collapse of the entire program. If you want the program to be able to detect errors, process them, and then continue to run, then you must use the try and except statements to deal with the following format:

try  :

  Statement after the error block to be processed

the except:        # you can use without any exception types except, but this is not a good way, through this program we can not identify the specific exception information. Because it captures all the exceptions.

  Statements executed after an exception occurs

except  <异常类别>,<参数>  :       #一个异常可以带上参数,可作为输出的异常信息参数。

  出现异常后执行的语句

else  :      #异常处理可搭配else使用,如果没发生异常则执行else语句块。

  else语句块

 

try-finally 语句

try-finally 语句无论是否发生异常都将执行最后的代码。

try  :

  <语句>

finally:

  <语句>      #退出try时总会执行

 

 

触发异常

 

我们也可以使用raise语句自己触发异常

 

raise语法格式如下:

raise [Exception [, args [, traceback]]]             # 触发异常后,后面的代码就不会再执行

 

语句中 Exception 是异常的类型(例如,NameError)参数标准异常中任一种,args 是自已提供的异常参数。

 

最后一个参数是可选的(在实践中很少使用),如果存在,是跟踪异常对象。

 

 

用户自定义异常

通过创建一个新的异常类,程序可以命名它们自己的异常。异常应该是典型的继承自Exception类,通过直接或间接的方式。

class 自定义名字(异常基类)

  def 函数名(参数):

    语句块

 

python标准异常

异常名称               描述

BaseException        所有异常的基类

SystemExit         解释器请求退出

KeyboardInterrupt      用户中断执行(通常是输入^C)

Exception              常规错误的基类

StopIteration        迭代器没有更多的值

GeneratorExit        生成器(generator)发生异常来通知退出

StandardError        所有的内建标准异常的基类

ArithmeticError         所有数值计算错误的基类

FloatingPointError     浮点计算错误

OverflowError        数值运算超出最大限制

ZeroDivisionError      除(或取模)零 (所有数据类型)

AssertionError           断言语句失败

AttributeError       对象没有这个属性

EOFError          没有内建输入,到达EOF 标记

EnvironmentError       操作系统错误的基类

IOError            输入/输出操作失败

OSError            操作系统错误

WindowsError           系统调用失败

ImportError          导入模块/对象失败

LookupError          无效数据查询的基类

IndexError           序列中没有此索引(index)

KeyError           映射中没有这个键

MemoryError         内存溢出错误(对于Python 解释器不是致命的)

NameError           未声明/初始化对象 (没有属性)

UnboundLocalError        访问未初始化的本地变量

ReferenceError          弱引用(Weak reference)试图访问已经垃圾回收了的对象

RuntimeError         一般的运行时错误

NotImplementedError      尚未实现的方法

SyntaxError          Python 语法错误

IndentationError        缩进错误

TabError           Tab 和空格混用

SystemError          一般的解释器系统错误

TypeError            对类型无效的操作

ValueError           传入无效的参数

UnicodeError        Unicode 相关的错误

UnicodeDecodeError         Unicode 解码时的错误

UnicodeEncodeError         Unicode 编码时错误

UnicodeTranslateError    Unicode 转换时错误

Warning 警告的基类

DeprecationWarning        关于被弃用的特征的警告

FutureWarning           关于构造将来语义会有改变的警告

OverflowWarning          旧的关于自动提升为长整型(long)的警告

PendingDeprecationWarning    关于特性将会被废弃的警告

RuntimeWarning         可疑的运行时行为(runtime behavior)的警告

SyntaxWarning            可疑的语法的警告

UserWarning          用户代码生成的警告

 

Guess you like

Origin www.cnblogs.com/98peiqi/p/11121495.html