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()   
    

      

  • 相关阅读:
    hadoop运行mahout问题解决方法
    在cdh5.1.3中在mapreduce使用hbase
    scala-eclipse
    创建cloudera-cdh5本地yum源
    HttpClient 操作总结
    Java NIO 操作总结
    linux 操作总结
    白话机器学习
    Java(jdk1.7) 陷阱
    Weibo Crawler in Action
  • 原文地址:https://www.cnblogs.com/binguo2008/p/7226209.html
Copyright © 2011-2022 走看看