日期:2017-12-30
作者:秋的懵懂
# coding = utf-8
# ***********************************************************
# @file python_08_function.py
# @brief 函數(shù)
# @author 魏文應(yīng)
# @date 2017-12-27
# ***********************************************************
# ---------------------------------------------------------
# 定義函數(shù)
print('\n\n')
print('_______________________________________________')
print("定義函數(shù):")
# 定義函數(shù)
def greet_user():
"""顯示問(wèn)候語(yǔ)"""
print('hello!')
# 使用函數(shù)
greet_user()
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 給函數(shù)傳一個(gè)參數(shù)
print('\n\n')
print('_______________________________________________')
print("給函數(shù)傳一個(gè)參數(shù):")
# 定義函數(shù)超凳,定義時(shí)稱(chēng)為形參
def greet_user(username):
"""顯示問(wèn)候語(yǔ)"""
print('hello! ' + username.title() + '.')
# 使用函數(shù)勘究,使用時(shí)稱(chēng)為實(shí)參
greet_user('魏文應(yīng)')
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 實(shí)參
print('\n\n')
print('_______________________________________________')
print("實(shí)參:")
def describe_pet(animal_type, pet_name):
"""顯示動(dòng)物信息"""
print('My ' + animal_type + "'s name is " + pet_name + '.')
# 根據(jù)位置,把實(shí)參關(guān)聯(lián)到形參上
describe_pet('hamster', 'harry')
# 關(guān)鍵字實(shí)參
describe_pet(animal_type='hamster', pet_name='harry')
# 指定默認(rèn)值,如果前面參數(shù)沒(méi)有默認(rèn)值疟呐,
# 有默認(rèn)值的參數(shù)需要放在后面
def describe_pet(pet_name, animal_type = 'dog'):
"""顯示動(dòng)物信息"""
print('My ' + animal_type + "'s name is " + pet_name + '.')
describe_pet(pet_name='Willie')
describe_pet('Willie')
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 函數(shù)返回值
print('\n\n')
print('_______________________________________________')
print("函數(shù)返回值:")
def get_formatted_name(fisrt_name, last_name):
"""返回完整的名字"""
full_name = fisrt_name + ' ' + last_name
return full_name.title()
musician = get_formatted_name('jimi', 'handrix')
print(musician)
# 讓實(shí)參變成可選的
def get_formatted_name(fisrt_name, last_name,
middle_name = ''):
"""返回完整的名字"""
if middle_name:
full_name = fisrt_name + ' ' + middle_name + \
' ' + last_name
else:
full_name = fisrt_name + ' ' + last_name
return full_name.title()
musician = get_formatted_name('jimi', 'handrix')
print(musician)
musician = get_formatted_name('jimi', 'handrix', 'lee')
print(musician)
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 返回值為字典類(lèi)型
print('\n\n')
print('_______________________________________________')
print("返回值為字典類(lèi)型:")
def build_person(first_name, last_name, age=''):
"""返回一字典译柏,包含人的信息"""
person = {'first': first_name, 'last': last_name}
# 可參數(shù)age
if age:
person['age'] = age
return person
musician = build_person('jimi', 'handrix')
print(musician)
musician = build_person('jimi', 'handrix', 27)
print(musician)
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 函數(shù)參數(shù)為列表
print('\n\n')
print('_______________________________________________')
print("函數(shù)參數(shù)為列表:")
def print_models(unprinted_designs, complete_models):
"""打印信息"""
while unprinted_designs:
current_design = unprinted_designs.pop()
print('Printing model:' + current_design)
complete_models.append(current_design)
def show_complete_models(complete_models):
"""打印信息"""
print('\nThe following models have been printed:')
for complete_model in complete_models:
print(complete_model)
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
# 這里unprinted_designs傳了列表副本挑豌,列表不會(huì)被修改
print_models(unprinted_designs[:], completed_models)
show_complete_models(completed_models)
show_complete_models(unprinted_designs)
# 直接傳列表參數(shù)名酗失,列表會(huì)被修改
print_models(unprinted_designs, completed_models)
show_complete_models(completed_models)
show_complete_models(unprinted_designs)
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 傳遞任意數(shù)量參數(shù)
print('\n\n')
print('_______________________________________________')
print("傳遞任意數(shù)量參數(shù):")
# *toppings是一個(gè)空元組
def make_pizza(*toppings):
print(toppings)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 傳遞任意數(shù)量參數(shù)(字典)
print('\n\n')
print('_______________________________________________')
print("傳遞任意數(shù)量參數(shù)(字典):")
# **user_info 創(chuàng)建一個(gè)空字典
def build_profile(first, last, **user_info):
"""創(chuàng)建一個(gè)字典义钉,保存任意信息"""
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key, value in user_info.items():
profile[key] = value
return profile
user_profile = build_profile('albert', 'einstein',
location='princeton',
field='physics')
print(user_profile)
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 將函數(shù)存儲(chǔ)在模塊中
print('\n\n')
print('_______________________________________________')
print("將函數(shù)存儲(chǔ)在模塊中:")
# import導(dǎo)入的文件不能是以數(shù)字開(kāi)頭命名的
# 如果文件名過(guò)長(zhǎng),可用pizza 替代
# python_08_function_pizza级零,如下
# 指定別名
import python_08_function_pizza as pizza
pizza.make_pizza(12, 'pepperoni')
pizza.make_pizza(16, 'mushrooms',
'green peppers',
'extra cheese')
# 只導(dǎo)入其中一個(gè)函數(shù)
# from module_name import function_0, function_1, function_2
# * 號(hào)可以導(dǎo)入所有函數(shù)
# from python_08_function_pizza import *
from python_08_function_pizza import make_pizza
make_pizza(13, 'pepperoni')
print('_______________________________________________')
# ---------------------------------------------------------
子模塊:
# coding = utf-8
# ***********************************************************
# @file python_08_function_pizza.py
# @brief 函數(shù)模塊
# @author 魏文應(yīng)
# @date 2017-12-28
# ***********************************************************
def make_pizza(size, *toppings):
"""概述要制作的蛋糕"""
print('\nMake a ' + str(size) +
'-inch pizza with the following toppings:')
for topping in toppings:
print('-' + topping)