python function

Because the arguments to power in the final invocation of it are named, their order is irrelevant; the arguments are associated with the parameters of the same name in the definition is called key-word passing

Keyword passing, in combination with the default argument capability of Python functions, can be highly useful when you're defining functions with large numbers of possible arguments, most of which have common defaults.

Python functions can also be defined to handle variable numbers of arguments.

Prefixing the final parameter name of the function with a * causes all excess nonkeyword arguments in a call of a function to be collected together and assigned as a tuple to the given parameter.

If the final parameter in the parameter list is prefixed with **, it will collect all excess keyword-passed arguments into a dictionary.

Arguments are passed in by object reference. The parameter becomes a new reference to the object. For immutable objects( such as tuples, strings,and numbers), what is done with a parameter has no effect outside the function.

global

nonlocal, causes an identifier to refer to a previously bound variable in the closest enclosing scope.

lambda expressions are anonymous little functions that you can quickly define inline.

lambda parameter1, parameter2, . . .: expression

A generator function is a special kind of function that you can use to define your own iterators. When you define a generator function, you return each iteration's value using yield keyword.

Depending on how it's used, a generator that doesn't have some condition to halt it could cause an endless loop when called.

A decorator is syntactic suger for this process and lets you wrap one function inside another with a one-line addition. (like js closure,java aop)

def decorate(func):
    print(" in decorate function, decorating", func.__name__)
    def wrapper_func(*args):
        print("executing", func.__name__)
        return func(*args)
    return wrapper_func


@decorate
def myfunc(parameter):
    print(parameter)

 

转载于:https://www.cnblogs.com/grep/archive/2012/08/13/2636000.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值