python学习(10)————函数与模块

来源:互联网 发布:异常断电数据库 编辑:程序博客网 时间:2024/05/18 23:27

函数的命名

函数是一个程序的必备元素,它可以简化主体函数,让程序看的更加具体、形象。
函数具有三个特征:

  • 首先,它们给一段代码命名,并让它可重复使用;
  • 其次,它获取参数的方式就像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 + bdef subtract(a, b):    print "SUBTRACTING %d - %d" % (a, b)    return a - bdef multiply(a, b):    print "MULTIPLYING %d * %d" % (a, b)    return a * bdef divide(a, b):    print "DIVIDING %d / %d" % (a, b)    return a / bprint "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 wordsdef 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 worddef print_last_word(words):    """Prints the last word after popping it off."""    word = words.pop(-1)    print worddef 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_3sentence = "All good things come to those who wait."words = python10_3.break_words(sentence)wordssorted_words = python10_3.sort_words(words)sorted_wordspython10_3.print_first_word(words)python10_3.print_last_word(words)wordspython10_3.print_first_word(sorted_words)python10_3.print_last_word(sorted_words)sorted_wordssorted_words = python10_3.sort_sentence(sentence)sorted_wordspython10_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的时候会运行。

0 0