码迷,mamicode.com
首页 > 编程语言 > 详细

python装饰器

时间:2016-07-22 10:04:43      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

# -*- coding: utf-8 -*-
python奇怪的作用域:

分全局变量globals()和局部变量locals()

python函数中参数的默认值是可选的

装饰器(Decorators):一个decorator只是一个带有一个函数作为参数并返回一个替换函数的闭包。
装饰器的语法糖@

#########################

a_string = This is a global variable;

def foo():
    print locals();
foo(); # {}

print globals();
‘‘‘
{‘foo‘: <function foo at 0x023CF870>,
 ‘__builtins__‘: <module ‘__builtin__‘ (built-in)>,
  ‘__file__‘: ‘test.py‘,
   ‘a_string‘: ‘This is a global variable‘,
    ‘__name__‘: ‘__main__‘,
     ‘__package__‘: None,
      ‘__doc__‘: None}
‘‘‘

################################

a_string = This is a global variable;

def foo():
    a_string = test;
    print locals(); # {‘a_string‘: ‘test‘}
foo(); 

print globals();
‘‘‘
{‘foo‘: <function foo at 0x023CF870>,
 ‘__builtins__‘: <module ‘__builtin__‘ (built-in)>,
  ‘__file__‘: ‘test.py‘,
   ‘a_string‘: ‘This is a global variable‘,
    ‘__name__‘: ‘__main__‘,
     ‘__package__‘: None,
      ‘__doc__‘: None}
‘‘‘

################################

# 函数传的参数成了局部变量

def foo(x):
    print locals();
foo(1); # {‘x‘: 1} 

################################

# python函数中参数的默认值是可选的

def foo(x, y = 0):
    print (x - y);

foo(3); # 3
foo(2, 1); # 1
foo(y = 1, x = 6); # 5

##############################

# 内嵌函数
def outer():
    def inner():
        print (inner);
    inner();

outer(); # inner

##########################

def add(x, y):
    return x + y;

def sub(x, y):
    return x - y;

def foo(func, x, y):
    return func(x, y);

print (foo(add, 5, 3)); # 8
print (foo(sub, 5, 3)); # 2

################################

# 返回内部函数
def outer():
    a = some word;
    def inner():
        print (a);
    return inner;
    
foo = outer();    
foo(); # some word

##############################

# 闭包
def outer(x):
    def inner():
        print (x);
    return inner;

outer1 = outer(1);
outer2 = outer(2);

outer1(); # 1
outer2(); # 2

############################
# 装饰器(Decorators)
# decorator 是把一个函数当作参数传入并返回替换参数的闭包

def outer(some_func):
    def inner():
        print some_func() + 1;
    return inner;

def foo():
    return 2

decorated = outer(foo);
decorated(); # 3


##############################

# 装饰器的语法糖@
def dog(fn):
    print (html %s % fn.__name__);
    fn();
    print (css %s % fn.__name__);

# 装饰器语法糖的目的就是再原有函数基础上添加一个小功能
@dog
def foo():
    print(foo);

‘‘‘
html foo
foo
css foo
‘‘‘

###############################

def one(*args):
    print (args);

one(); # ()
one(1, 2, 3); # (1, 2, 3)

def two(x, y, *args):
    print (x, y, args);

two(a, b, c, d); # (‘a‘, ‘b‘, (‘c‘, ‘d‘))

##############################

‘‘‘
在调用一个函数时带有一个以*为前导的变量作为参数表示这个变量内容需要被解析然后用作位置参数。
‘‘‘

def add(x, y):
    print(x + y);

lis = [1, 2];
add(lis[0], lis[1]); # 3
add(*lis); # 3

#############################

#当我们引入**时,事情变得更加复杂点,与*表示iterables和位置参数一样,**表示dictionaries & key/value对。

def foo(**kvalues):
    print (kvalues);

foo(x = 1, y = 2); # 返回一个dictionary,{‘y‘: 2, ‘x‘: 1}

############################

# **的逆运用
dic = {x: 1, y: 2};

def foo(x, y):
    print (x + y);

foo(**dic); # 3

 

python装饰器

标签:

原文地址:http://www.cnblogs.com/lqcdsns/p/5693906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!