函數的命名
函數是一個程序的必備元素,它可以簡化主體函數,讓程序看的更加具體、形象。
函數具有三個特徵:
- 首先,它們給一段代碼命名,並讓它可重複使用;
- 其次,它獲取參數的方式就像python腳本獲取argvs一樣;
- 最後,用1和2可以讓你實現你的小腳本。
這裏,我們給出了一些基本的函數使用案例:
# -*- coding: utf-8 -*-
#以下四種方式介紹了四種傳參方式,有多參數和單參數方式
#第一種方式:用指針的方式傳遞元組
def print_first_way(*args):
arg1,arg2 = args
print "arg1:%r,arg2:%r" % (arg1,arg2)
#第二種方式:傳遞兩個參數
def print_second_way(arg1,arg2):
print "arg1:%r,arg2:%r" % (arg1,arg2)
#第三種方式:傳遞單參數
def print_third_way(arg1):
print "arg1:%r" % arg1
#第四種方式:無參傳遞
def print_fourth():
print "I got nothing."
#把四個函數都運行一遍:
print_first_way("hello","world")
print_second_way("hello","world")
print_third_way("hello")
print_fourth()
得到的結果:
後三個都很容易理解,就是一個一個參數的傳遞進去。第一個函數的參數*args是什麼呢。這裏,args把所有的參數把它以參數列表的形式傳遞給了函數,這樣更加便於管理。
函數返回值
函數的作用就是,傳遞進來參數,然後對參數進行處理,最終得到參數返回。
以下給了一個函數返回值的示例:
# -*- coding: utf-8 -*-
def add(a, b):
print "ADDING %d + %d" % (a, b)
return a + b
def subtract(a, b):
print "SUBTRACTING %d - %d" % (a, b)
return a - b
def multiply(a, b):
print "MULTIPLYING %d * %d" % (a, b)
return a * b
def divide(a, b):
print "DIVIDING %d / %d" % (a, b)
return a / b
print "Let's do some math with just functions!"
age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)
print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
print "Here is a puzzle."
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print "That becomes: ", what, "Can you do it by hand?"
得到的結果:
函數、模塊的應用
這一節,我們建立一個python腳本,只包含函數,沒有執行語句,然後通過另外一個python腳本來調用這些函數:
首先創建ex10_3.py:
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words
def sort_words(words):
"""Sorts the words."""
return sorted(words)
def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0)
print word
def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word
def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)
def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
以上代碼是無法運行的,爲了運行以上代碼,我們創建一個python腳本來運行:
import python10_3
sentence = "All good things come to those who wait."
words = python10_3.break_words(sentence)
words
sorted_words = python10_3.sort_words(words)
sorted_words
python10_3.print_first_word(words)
python10_3.print_last_word(words)
words
python10_3.print_first_word(sorted_words)
python10_3.print_last_word(sorted_words)
sorted_words
sorted_words = python10_3.sort_sentence(sentence)
sorted_words
python10_3.print_first_and_last(sentence)
python10_3.print_first_and_last_sorted(sentence)
也就是說從引入包python10_3.py,然後調用其中的函數,執行得到以下結果:
可以看到,在建立模塊以後,我們可以用import的方式來進行調用,這樣非常適合我們進行工具開發。另外,我們還可以通過help(python10_3)來查看函數的使用方式:
“heheheheheehe”是作者添加的測試句,添加在python10_3.py的最後,發現在import的時候會運行。