python快速上手_第三章 函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mike_Cui_LS/article/details/83114636
def hello():
    print('hello')
    print('world')


hello()
hello()
hello()

def 关键字来定义一个函数, 接下来是 函数名hello() 接下来缩进的部分是 函数体。

函数是在调用的时候执行,而不是在定义时执行。

def语句和参数

def hello(name):
    print('hello '+name)

hello('Alice')
hello('Mike')

这里定义了一个hello的函数,有一个名为 name的变元。‘变元’ 是一个变量。 对应于java中的形参。 而我们传递的 Alice、Mike 为实参。

返回值和 return语句

return语句包含:

  • return关键字
  • 函数应该返回的值或表达式
import random

def getAnswer(answerNumber):
    if answerNumber == 1:
        return 'It is 1'
    elif answerNumber == 2:
        return 'It is 2'
    elif answerNumber == 3:
        return 'It is 3'
    elif answerNumber == 4:
        return 'It is 4'
    elif answerNumber == 5:
        return 'It is 5'
    elif answerNumber == 6:
        return 'It is 6'
    elif answerNumber == 7:
        return 'It is 7'
    elif answerNumber == 8:
        return 'It is 8'
    elif answerNumber == 9:
        return 'It is 9'

r = random.randint(1,9)
fortune = getAnswer(r)
print(fortune)

None值

None 在python中表示 没有值。None是 NoneType数据类型的唯一值。首字母必须大写。

等同于其他语言的 null、nil 或者 undefined.

没有返回值的 函数,默认就是返回 None。比如 print()函数。

对于没有return语句的函数定义。Python都会在末尾的时候加上 return None. 类似于while或者for循环隐式的以 continue语句结尾。

关键字参数和 print()

大多数的参数是由它们在函数调用中的位置来确定的。而关键字参数则是由加在它们签名的关键字来识别的。

关键字参数通常用于可选变元。 print()函数有可选的变元 end 和 sep. 分别指定在参数末尾打印什么,以及在参数之间打印什么。默认结尾打印的是 换行。

print('hello',end='')
print('world')

helloworld
print('hello','a','b','c',end='',sep=',')
print('world')

hello,a,b,cworld

局部和全局作用域

  • 全局作用域中的代码不能使用任何局部变量。
  • 但是,局部作用域可以访问全局变量。
  • 一个函数的局部作用域中的代码,不能使用其他局部作用域中的变量。
  • 如果在不同的作用域中,你可以用相同的名字命名不同的变量。

局部变量不能在全局作用域内使用

def spam():
    eggs = 3120

spam()
print(eggs)

上面的代码中,在全局作用域访问了只在局部作用域的 eggs。

Traceback (most recent call last):
  File "/Users/cuiyonghong/Documents/fiveTimes.py", line 7, in <module>
    print(eggs)
NameError: name 'eggs' is not defined

报错提醒, eggs没有定义。

局部作用域不能是哟经其他局部作用域内的变量

def spam():
    eggs = 99
    bacon()
    print(eggs)

def bacon():
    ham = 101
    eggs = 0

spam()

99

bacon()函数返回时, bacon的局部作用域就销毁了。

一个函数中的局部变量完全与其他函数中的局部变量是分开的。

全局变量可以在局部作用域中读取

def spam():
    print(eggs)

eggs = 42
spam()
print(eggs)

在 spam()函数中没有变元名为 eggs, 也没有代码为 eggs 赋值。所以Python认为它是全局变量 eggs的引用。 所以会打印出 42、

名称相同的局部变量和全局变量

def spam():
    eggs = 'spam local' 
    print(eggs) # spam local  eggs变量存在于 spam被调用时的局部作用域

def bacon():
    eggs = 'bacon local'
    print(eggs) # bacon local  eggs变量存在于 bacon被调用时的局部作用域
    spam()
    print(eggs) # bacon local eggs变量存在于 bacon被调用时的局部作用域

eggs = 'global'
bacon()
print(eggs) #global  eggs变量存在于 全局作用域

global 语句

如果要在一个函数内修改全局变量,就要使用 global语句。如果在函数的顶部有 global eggs 这样的代码, 就是告诉 Python, “eggs 指向的是全局变量。不要用这个名字创建一个局部变量”


def spam():
    global eggs
    eggs = 'spam'

eggs = 'global'
spam()
print(eggs)

结果

spam

4条法则,来区分一个变量是处于局部作用域还是全局作用域:

1.如果变量在全局作用域中使用(即所有的函数之外),它就总是全局变量。
2.如果一个函数中,有针对该变量的 global语句, 它就是全局变量。
3.否则,如果该变量用于函数中的赋值语句,它就是局部变量。
4.但是,如果该变量没有用再赋值语句中,它就是全局变量。

如果要在一个函数中修改全局变量中存储的值,就必须要对该变量使用 global语句。

def spam():
    print(eggs)
    eggs = 'spam'

eggs = 'global'
spam()
print(eggs)


Traceback (most recent call last):
  File "/Users/cuiyonghong/Documents/fiveTimes.py", line 8, in <module>
    spam()
  File "/Users/cuiyonghong/Documents/fiveTimes.py", line 4, in spam
    print(eggs)
UnboundLocalError: local variable 'eggs' referenced before assignment

发生这个错误的原因是, Python 看到局部函数中有对 eggs 赋值的语句,因此认为 eggs是一个局部变量, 但是 在使用前,并没有对 eggs的申明。所以会报错。

异常处理

相较与java 中的异常处理。python中的为 try: … except XXxError: …

def spam(divideBy):
    return 42/divideBy

print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))


Traceback (most recent call last):
  File "/Users/cuiyonghong/Documents/fiveTimes.py", line 8, in <module>
    print(spam(0))
  File "/Users/cuiyonghong/Documents/fiveTimes.py", line 4, in spam
    return 42/divideBy
ZeroDivisionError: division by zero

程序遇到错误,就会导致崩溃,并停止运行。 相反,我更希望程序能检测错误,处理错误,然后继续运行。

def spam(divideBy):

    try:
        return 42/divideBy
    except ZeroDivisionError:
        print('Error: Invalid a argument')
        
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))

使用 try:…except 来捕捉错误。并继续向后执行。

猜你喜欢

转载自blog.csdn.net/mike_Cui_LS/article/details/83114636