为什么在Python中需要括号? [英] Why do you need parenthesis in Python?

查看:372
本文介绍了为什么在Python中需要括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题只是试图理解某些东西;我知道该怎么办,只想知道两者之间的区别:

This question is just an attempt at understanding something; I know what to do, just want to know what is the difference between:

datetime.datetime.now().date

datetime.datetime.now().date()

推荐答案

括号表示您实际上是想调用该方法,而不是提及供以后使用.后者似乎没有意义,但是在较大的表达式中,它允许 delaying 函数调用:

Parentheses denote that you actually want to call the method, rather than mention it for later use. On a line of its own, the latter doesn't appear to make sense, but in larger expressions, it allows delaying the function call:

# access the "date" method, but don't call it yet
fn = datetime.datetime.now().date
... do other things ...
# *now* call date()
print(fn())

存储在fn中的可调用对象是绑定方法,它引用一个具体对象.在Python中,方法调用和函数调用之间没有根本区别:通过名称访问方法会创建一个绑定的方法对象,然后将该对象称为任何其他函数.就Python而言,省略括号仅意味着您(尚未)对调用它感兴趣.

The callable object stored in fn is a bound method, and it refers to a concrete object. In Python there is no fundamental difference between a method call and a function call: accessing a method by name creates a bound method object, which is then called as any other function. As far as Python is concerned, omitting the parentheses simply means that you are not (yet) interested in calling it.

当您具有接受可调用函数时,此功能很有用.例如,想象一下调用事件处理器,该处理器允许您提供自定义可调用对象来处理事件.通常,事件是通过GUI调度的,但是您想要(临时)希望将事件收集在刚刚创建的列表中.这将是绑定方法的用例:

This is useful when you have a function that accepts a callable. For example, imagine calling into an event processor that allows you to provide a custom callable for processing events. Normally the events are dispatched through GUI, but you want to (temporarily) want to collect the events in a list you've just created. This would be a use case for a bound method:

# library function that we cannot change
def do_processing(dispatchfn):
    for event in something_that_generates_events():
        ... internal bookkeeping
        # dispatch the event
        dispatchfn(event)

# our code to log the events
log = []
do_processing(log.append)
# ... inspect the events list, e.g.:
for event in log:
    print event

在这种情况下,使用绑定方法的替代方法是使用lambda:

In this case, an alternative to using a bound method would be to use a lambda:

do_processing(lambda event: log.append(event))

但是lambda的语义和性能略有不同.实际上,使用绑定方法可以在非常紧密的循环中改善性能. Python中一种流行的优化方法是转换紧密循环:

But a lambda has subtly different semantics and performance. Using a bound method can actually improve performance in very tight loops. A popular optimization in Python is to transform a tight loop:

todo = deque()
while todo:
    next_item = todo.popleft()
    ...

进入:

todo = deque()
todo_popleft = todo.popleft
while todo:
    next_item = todo_popleft()
    ...

如果while循环真的 很少,那么此转换实际上可以提高性能,因为它消除了一次dict查找和绑定方法创建的成本.这不是应该常规执行的操作,而是公认的未调用绑定方法的用法.

If the while loop does really little, this transformation can actually improve performance because it eliminates the cost of one dict lookup and of creation of the bound method. This is not something that should be done routinely, but it is a recognized usage of uncalled bound methods.

这篇关于为什么在Python中需要括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆