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

      

  • 相关阅读:
    Building a flexiable renderer
    Indirect Illumination in mental ray
    我的心情
    Cellular Automata
    Subsurface Scattering in mental ray
    Shader Types in mental ray
    BSP Traversal
    我的渲染器终于达到了MR的速度
    How to handle displacement and motion blur
    说明
  • 原文地址:https://www.cnblogs.com/binguo2008/p/7226209.html
Copyright © 2011-2022 走看看