python 的funtools模块

作者: 梅_梅 | 来源:发表于2016-05-25 18:07 被阅读437次

functools.partial

from operator import add
import functools
print add(1,2) #3
add1 = functools.partial(add,1)
print add1(10) #11

functools.update_wrapper

#-*- coding: gbk -*-

def thisIsliving(fun):
  def living(*args, **kw):
    return fun(*args, **kw) + '活着就是吃嘛。'
  return living

@thisIsliving
def whatIsLiving():
  "什么是活着"
  return '对啊,怎样才算活着呢?'

print whatIsLiving()
print whatIsLiving.__doc__

print

from functools import update_wrapper
def thisIsliving(fun):
  def living(*args, **kw):
    return fun(*args, **kw) + '活着就是吃嘛。'
  return update_wrapper(living, fun)

@thisIsliving
def whatIsLiving():
  "什么是活着"
  return '对啊,怎样才算活着呢?'

print whatIsLiving()
print whatIsLiving.__doc__

#output
对啊,怎样才算活着呢?活着就是吃嘛。
None

对啊,怎样才算活着呢?活着就是吃嘛。
什么是活着

functools.wraps

#-*- coding: gbk -*-

from functools import wraps

def thisIsliving(fun):
  @wraps(fun)
  def living(*args, **kw):
    return fun(*args, **kw) + '活着就是吃嘛。'
  return living

@thisIsliving
def whatIsLiving():
  "什么是活着"
  return '对啊,怎样才算活着呢?'

print whatIsLiving()
print whatIsLiving.__doc__
#output
对啊,怎样才算活着呢?活着就是吃嘛。什么是活着

相关文章

网友评论

    本文标题:python 的funtools模块

    本文链接:https://www.haomeiwen.com/subject/jvutdttx.html