zoukankan      html  css  js  c++  java
  • 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 "
    Show 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()   
    

      

  • 相关阅读:
    【BZOJ 3090】 树形DP
    【BZOJ 2323】 2323: [ZJOI2011]细胞 (DP+矩阵乘法+快速幂*)
    【BZOJ 1019】 1019: [SHOI2008]汉诺塔 (DP?)
    【BZOJ 3294】 3294: [Cqoi2011]放棋子 (DP+组合数学+容斥原理)
    【BZOJ 3566】 3566: [SHOI2014]概率充电器 (概率树形DP)
    【BZOJ 2121】 (字符串DP,区间DP)
    【BZOJ 4305】 4305: 数列的GCD (数论)
    【UOJ 179】 #179. 线性规划 (单纯形法)
    【BZOJ 4568】 4568: [Scoi2016]幸运数字 (线性基+树链剖分+线段树)
    【BZOJ 4027】 4027: [HEOI2015]兔子与樱花 (贪心)
  • 原文地址:https://www.cnblogs.com/binguo2008/p/7226209.html
Copyright © 2011-2022 走看看