python函数数量_Python学习心得(三)函数之任意数量实参、任意数量关键字实参、导入模块中的函数...

#!/usr/bin/python

# -*- coding:utf-8 -*-

'''

1.传递任意数量的实参

Python允许函数传入任意数量的实参,例如:

*messages形参名中的*表示让Python创建一个空的名称为messages的元组,接收传入的所有值

'''

def get_person_message(*messages):

concat = ''

for message in messages:

concat += ' ' + message

print "\nShow Person's Message:" + concat

get_person_message('CSDN','http://blog.csdn.net/binguo168')

get_person_message('http://www.cnblogs.com/binguo2008/')

get_person_message('binguo','27','male')

get_person_message('binguo168','27','male','basketball','football')

'''

2.使用位置实参和任意数量的实参

如果函数需要接收不同类型的位置实参,需要将接收任意数量的实参放在最后。

'''

def set_computer_configure(cpu,memory,*others):

concat = ''

for info in others:

concat += ' '+ info

print 'cpu:'+cpu+' ' + memory + 'memory:' + ' ' +'others:' +concat

set_computer_configure('Core i7','32g' ,'monitor:LG 27UD68','Display Card:GeForce GTX 1060')

set_computer_configure('Intel Xeon E5-2679','128g' ,'monitor:LG 27UD68')

'''

3.使用任意数量的关键字实参(接收任意数量的键值对)

通过**arguname来实现接收任意数量的实参,其中两个**表示让Python创建一个空的字典,传入的所有键值对都放在这个字典中

'''

def build_user_profile(username,**othersinfo):

dict_profile = {}

dict_profile['username'] = username

for userkey,uservalue in othersinfo.items():

dict_profile[userkey] = uservalue

return dict_profile

test_user_profile1 = build_user_profile(username = 'binguo',age = 27,gender = 'male')

print test_user_profile1

test_user_profile2 = build_user_profile(username = 'binguo168',hobby = 'basketball',blogs = 'CSDN',blog_url = 'http://blog.csdn.net/binguo168')

print test_user_profile2

'''

4.将函数存储在模块中

'''

#导入整个模块

import importModel

systemInfo = importModel.get_system_info(1)

print systemInfo

#导入指定的函数

from importModel import get_system_info,get_system_owner

get_system_info(1)

get_system_owner()

#使用as对模块设定别名

import importModel as testModel

testModel.get_system_info(1)

#导入模块中所有的函数

from importModel import *

get_system_info(1)

get_system_owner()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值