`

Python Function parameters

阅读更多

A note on the Function Argument.

 

 

 

# about the parameter of the python argument
# there are 4 types of argument that a Python Fucntino that can accept
# they are
#   1. fixed argument (or called non-default argument) 
#   2. the positional arguments
#   3. the keyword arguments
#   4. the default arguments ( a special case of keyword argument)
#
# the syntax to define python function is as follow
#    func_name(a, b, ..., [*args | default=default_values]], **kwargs)
# 
#  so the general rule of defining python argument is as follow
#      def func_name(a, b, ..., *args, **kwargs)
# however, it is quite confusing when the default value for a argument comes into the play
# to declare a default argument, you normally do this 
#      def func_name(a, b, ..., *args, default=default_value)
# because the default value for an argument syntactically looks like the keyword argument
# which means
#    1. you cannot have default argument before the positional argument
#        def func_name(a, b, ..., default=default_value, *args) # raise TypeError
#    2. it is not allowed if to have default argument in the middle of fixed argument
#        def func_name(a, ..., default=default_value, b, ..., *args) # raise TypeError
#    3. you cannot have both positional argument and default argument together...
#        def func_name(a, b, *args, default="default_value") # raise TypeError
#        def func_name(a, b, default=default_value", *args, **kwargs) # raise TypeError
#    4. You can have an argument with default value after the positional argument and before the 
#       keyword argument (**kwargs) (you can think default valued argument is a special form of
#       keyword argument.  
#         def func_name(a, b, ..., default=default_value, **kwargs)
#    5. As said in 4, if you try to put the default argument after the keyword arguments, it fails
#         def func_name(a, b, ..., **kwargs, default=default_value);



def could_compile1(a, b, default="default_value", **kwargs):
    print a, b
    print default
    for key in kwargs:
        print("keyword argument {0} = {1}".format(key, kwargs[key]))

def would_fail(a, default="default_value", b, *args):
    pass


def would_fail2(a, b, default="default_value", *args):
    pass


#def would_fail3(a, b,*args, default="default_value", **kwargs):
#    pass

#def would_fail4(a, b, default="default_value", *args, **kwargs):
#    pass

#def would_fail5(a, b, **kwargs, default="default_value"):
#    pass


def main():
    could_compile1(1, 2, default="overriden default value", optional1="optional 1 value", optional2="optional 2 value")
    # would_compile2(1, "override_default_value", 2, 3, 4) # SyntaxError: non-default argument follows default argument
    # would_fail2(1, 2, "override_default_value", 3, 4) # SyntaxError: non-default argument follows default argument
    
    pass
 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    a_byte_of_python

    Using Function Parameters Local Variables Using Local Variables Using the global statement Default Argument Values Using Default Argument Values Keyword Arguments Using Keyword Arguments The ...

    python定义函数语法.docx

    Python中定义函数的语法如下: ``` def function_name(parameters): """docstring""" statement(s) ``` 其中,关键字"def"表示定义函数的关键字,后面跟着函数名"function_name",函数名可以自定义,但是需要符合...

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In ...

    Python函数的定义和调用方法.docx

    一、函数的定义方法 Python中定义一个函数需要使用关键字def,函数的基本形式如下: ``` def function_name(parameters): "函数文档说明" function body return [expression] ``` Python函数的定义和调用方法全文共...

    Beginning Python (2005).pdf

    Passing Parameters from Python to C 360 Returning Values from C to Python 363 The LAME Project 364 The LAME Extension Module 368 Using Python Objects from C Code 380 Summary 383 Exercises 383 ...

    python-条件定义函数.docx

    条件定义函数的基本语法如下: ``` def function_name(parameters): if condition1: statement1 elif condition2: statement2 else: statement3 ``` 其中,function_name是函数的名称,parameters是函数的参数列表...

    Python Cookbook英文版

    Python Cookbook英文版 Table of Contents Foreword Preface 1. Python Shortcuts 1.1 Swapping Values Without Using a Temporary Variable 1.2 Constructing a Dictionary Without Excessive Quoting 1.3...

    python语言程序设计自定义函数.docx

    自定义函数的基本语法如下: ``` def function_name(parameters): # 函数体 return [expression] ``` 其中,function_name是函数的名称,parameters是函数的参数列表,函数体是一系列执行特定任务的语句,而return...

    Invent Your Own Computer Games with Python (2008).pdf

    Parameters x Local Variables and Global Variables with the Same Name x Where to Put Function Defintions x The Colon : x Step by Step, One More Time x Designing the Program x A Web Page for Program ...

    Python Cookbook, 2nd Edition

    Python Cookbook, 2nd Edition By David Ascher, Alex Martelli, Anna Ravenscroft Publisher : O'Reilly Pub Date : March 2005 ISBN : 0-596-00797-3 Pages : 844 Copyright Preface The ...

    Python-Nova.novaextension:适用于Nova编辑器的Python语言服务器(PyLS)扩展

    还支持所有Python Language Server插件→ mypy , isort和black工作特征 自动补全(包括代码段填充-启用“ Include Function and Class Parameters ,已在Nova 2.0中修复) 跟踪导入(在Nova 2.0中修复) 全功能...

    python programming

    1.7 Inside a Python Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.8 Chaos and Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    Python函数教程配详细案例.md

    函数是Python中最基本的组成部分之一,是一段可以重复使用的代码块,可以接受输入参数并返回输出结果。 def function_name(parameters): """docstring""" statement(s) return [expression]

    Python Power - The Comprehensive Guide (2008).pdf

    About Python ............................................................................................1 What Is Python? ................................................................................

    python中def是做什么的

    def function_name(parameters): expressions 实例 def function(): print('This is a function') a = 1+2 print(a) 上面我们定义了一个名字为 function 的函数,函数没有不接受参数,所以括号内部为空,紧接着...

    python 动态调用函数实例解析

    3. apply(functoin_name,parameters) 这个function_name不是字符串,而是函数对象本身;parameters是参数,类似(a,b,…)这样的格式 4. 当函数不确定参数的数目时候,采用 一个 * 或两个** 他们的用法是有讲究的。 ...

    pytron:Python中的信任区域牛顿优化

    Python 中的 Trust-Region Newton 方法 !危险! 这是 alpha 质量的软件,但边缘仍然很粗糙。 具体来说,仍然缺乏错误管理(这意味着如果优化中出现问题,您将不会看到错误消息,而只会看到垃圾信息)。 这些事情...

    Python实现比较两个列表(list)范围

    Create a function, this function receives two lists as parameters, each list indicates a scope of numbers, the function judges whether list2 is included in list1.  Function signature:  differ_scope...

    ramda.py:Ramda.js的Python克隆

    Python克隆 ( 改良叉 pip install ramda >> > from ramda import * >> > inc ( 1 ) 2 >> > map ( inc , [ 1 , 2 , 3 ]) [ 2 , 3 , 4 ] >> > incEach = map ( inc ) >> > incEach ([ 1 , 2 , 3 ]) [ 2 , 3 , 4 ] ...

    easytorch:基于Python的numpy实现的简易深度学习框架,包括自动求导、优化器、layer等的实现

    easytorch使用Python的numpy实现的简易深度学习框架,API与pytorch基本相同,实现了自动求导、基础优化器、layer等。1 文档目录2 Quick Startfrom easytorch.layer import Linear, Tanh, Sequentialfrom easytorch....

Global site tag (gtag.js) - Google Analytics