Quantcast
Channel: My Blog » Python
Browsing all 5 articles
Browse latest View live

Image may be NSFW.
Clik here to view.

Python decorator(1)

没有太多时间,把一些实验和结果post一下,相信看过这些例子后会对python的修饰符有足够实用的了解。(所有程序在Python 2.7中验证) 第一组 1: @dec2 2: @dec1 3: def func(arg1, arg2, ...): 4: pass 等同于 1: def func(arg1, arg2, ...): 2: pass 3: func = dec2(dec1(func))...

View Article



Image may be NSFW.
Clik here to view.

Python decorator(2)

大多数情况下需要根据参数来定制decorator,此时的语法和前面就有了不同。注意下面例子中,decorator必须有()做调用。 1: @decomaker(argA,argB,...) 2: def func(arg1,arg2,...): 3: pass 这种形式等同于 1: def func(arg1,arg2,...): 2: pass 3: func =...

View Article

Image may be NSFW.
Clik here to view.

Python decorator(3)

前面的两个例子都很简单,这里再举一个更常见、更复杂的。在日常使用中经常需要获得原函数的各项参数甚至返回值做检查。如何做呢?下面演示一个检查输入参数类型的例子。 1: def input_type(t): 2: 3: def wrapper(func): 4: 5: def wrapped(msg): 6: if type(msg) is t: 7: return func(msg) 8: else:...

View Article

Image may be NSFW.
Clik here to view.

Python的默认参数

Python代码中默认参数不仅可以使用常量,还可以使用函数调用。前者和其他常用静态语言没发现有什么不同,后者还是有些地方需要注意的。 Python的默认参数会被提前evaluate   1: def print_a_return_1(): 2: print 'a' 3: return 1 4: 5: def func(x = print_a_return_1()): 6: print x 7:...

View Article

Image may be NSFW.
Clik here to view.

写Micolog2有感

Python参数传递的灵活性给程序开发带来很大的简便 静态语言中如果class B用到class...

View Article

Browsing all 5 articles
Browse latest View live




Latest Images