博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python functools.partial
阅读量:4987 次
发布时间:2019-06-12

本文共 3148 字,大约阅读时间需要 10 分钟。

def declare_consumer(self, consumer_cls, topic, callback):        """Create a Consumer using the class that was passed in and        add it to our list of consumers        """        def _connect_error(exc):            log_info = {'topic': topic, 'err_str': str(exc)}            LOG.error(_("Failed to declare consumer for topic '%(topic)s': "                      "%(err_str)s") % log_info)        def _declare_consumer():            consumer = consumer_cls(self.conf, self.channel, topic, callback,                                    six.next(self.consumer_num))            self.consumers.append(consumer)            return consumer        return self.ensure(_connect_error, _declare_consumer)    def declare_direct_consumer(self, topic, callback):        """Create a 'direct' queue.        In nova's use, this is generally a msg_id queue used for        responses for call/multicall        """        self.declare_consumer(DirectConsumer, topic, callback)    def declare_topic_consumer(self, topic, callback=None, queue_name=None,                               exchange_name=None):        """Create a 'topic' consumer."""        self.declare_consumer(functools.partial(TopicConsumer,                                                name=queue_name,                                                exchange_name=exchange_name,                                                ),                              topic, callback)    def declare_fanout_consumer(self, topic, callback):        """Create a 'fanout' consumer."""        self.declare_consumer(FanoutConsumer, topic, callback)

functools.partial可以提供对象的默认参数而生成一个新的访问形式,方便统一代码调用形式。以上代码中TopicConsumer类和FanoutConsumer类及DirectConsumer类的构造函数存在差异,如果不使用functools则需要在declare_consumer中进行判断

摘一下Python DOC上的说明和例子(https://docs.python.org/2.7/library/functools.html#functools.partial):

functools.partial(func[,*args][, **keywords])

Return a new  object which when called will behave like func called with the positional arguments args and keyword arguments keywords. If more arguments are supplied to the call, they are appended to args. If additional keyword arguments are supplied, they extend and override keywords. Roughly equivalent to:

def partial(func, *args, **keywords): def newfunc(*fargs, **fkeywords): newkeywords = keywords.copy() newkeywords.update(fkeywords) return func(*(args + fargs), **newkeywords) newfunc.func = func newfunc.args = args newfunc.keywords = keywords return newfunc

The  is used for partial function application which “freezes” some portion of a function’s arguments and/or keywords resulting in a new object with a simplified signature. For example,  can be used to create a callable that behaves like the  function where the base argument defaults to two:

>>>
>>> from functools import partial>>> basetwo = partial(int, base=2) >>> basetwo.__doc__ = 'Convert base 2 string to an int.' >>> basetwo('10010') 18

自己也来练习一发:

>>> def say(man, words):...     print 'say', words, 'to', man...>>> say('boss', 'hello')say hello to boss>>> say2boss = functools.partial(say, 'boss')>>> say2boss('good morning!')say good morning! to boss

 

转载于:https://www.cnblogs.com/lailailai/p/3892241.html

你可能感兴趣的文章
javascript的时间描述图怎么写
查看>>
Maximum Gap 164
查看>>
Robot Framework Share 4
查看>>
【LeetCode】155. Min Stack
查看>>
【LeetCode】214. Shortest Palindrome
查看>>
现有资源和jsapi的融合一种方式
查看>>
UICollectionViewController的简单使用及一些注意点(json)
查看>>
Vue.js 源码分析(十三) 基础篇 组件 props属性详解
查看>>
Ubuntu系统升级内核方法
查看>>
Spring Bean单例与线程安全
查看>>
EasyUI datagrid.getSelections 没有返回正确的选择行数
查看>>
分享一个随机重排函数(C#)
查看>>
Asp.Net Core在CentOS部署与注意
查看>>
自反+递归 实现评论的无限引用
查看>>
新闻发布系统
查看>>
NOIP提高组2016 D1T2 【天天爱跑步】
查看>>
数据结构基础(19) --堆与堆排序
查看>>
HTML基础
查看>>
Window通过cmd查看端口占用、相应进程、杀死进程
查看>>
Exp4 恶意代码分析 _20151220
查看>>