调用不带括号的函数python的目的 [英] Purpose of calling function without brackets python

查看:32
本文介绍了调用不带括号的函数python的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下事项:

class objectTest():def __init__(self, a):self.value = adef get_value(self):返回 self.value类执行():def __init__(self):a = objectTest(1)b = objectTest(1)打印(a == b)打印(a.get_value()== b.get_value)打印(a.get_value()== b.get_value())打印(a.get_value == b.get_value)如果 __name__ == '__main__':执行 = 执行()

此代码返回

<预><代码>>>>错误的错误的真的错误的

鉴于 get_value 是一个函数,我希望执行停止并返回错误,但事实并非如此.有人可以解释为什么 python 解释器允许这种语法而不是引发属性错误,这在我的情况下会节省我宝贵的时间.

解决方案

如前所述,函数和方法是一流的对象.你调用他们通过在最后抛出一些括号(方括号).但看起来你想要更多的动机来解释为什么 python 甚至让我们这样做.为什么我们要关心函数是否是一流的?

有时您不想调用它们,而是希望传递对可调用对象本身的引用.

from multiprocessing import Processt = 进程(目标 = my_long_running_function)

如果你在上面加上括号,它会在你的主线程中运行你的my_long_running_function;几乎没有你想要的!你想给 Process 一个你的可调用对象的引用,它会在一个新进程中自行运行.

有时您只想指定可调用对象并让其他东西...

def do_something(s):返回 s[::-1].upper()地图(做某事,['嘿','怎么了','哟'])出 [3]: ['YEH', 'PU TAHW', 'OY']

(在本例中为map)填写其参数.

也许您只想将一堆可调用对象放入某个集合中,然后以动态方式获取您想要的那个.

from operator import *str_ops = {'<':lt,'>':gt,'==':eq} # etcop = str_ops.get(my_operator)如果操作:结果 = op(lhs,rhs)

以上是将运算符的字符串表示映射到其实际操作的一种方法.

Consider the following:

class objectTest():

    def __init__(self, a):
        self.value = a

    def get_value(self):
        return self.value

class execute():

    def __init__(self):
        a = objectTest(1)
        b = objectTest(1)
        
        print(a == b)
        print(a.get_value() == b.get_value)
        print(a.get_value() == b.get_value())
        print(a.get_value == b.get_value)

if __name__ == '__main__':
    execute = execute()

This code return

>>>
False
False
True 
False

Given that get_value is a function, I would expect the execution to stop and return an error, but it doesn't. Can somebody explain why the python interpreter allow this kind of syntax instead of raising an attribute error, which in my case would have saved me precious time.

解决方案

As mentioned, functions and methods are first-class objects. You call them by throwing some parentheses (brackets) on the end. But it looks like you want some more motivation for why python even lets us do that. Why should we care if functions are first-class or not?

Sometimes you don't want to call them, you want to pass a reference to the callable itself.

from multiprocessing import Process
t = Process(target=my_long_running_function)

If you put brackets after the above, it runs your my_long_running_function in your main thread; hardly what you wanted! You wanted to give Process a reference to your callable that it will run itself in a new process.

Sometimes you just want to specify the callable and let something else...

def do_something(s):
    return s[::-1].upper()

map(do_something,['hey','what up','yo'])
Out[3]: ['YEH', 'PU TAHW', 'OY']

(map in this case) fill in its arguments.

Maybe you just want to drop a bunch of callables into some collection, and fetch the one you want in a dynamic manner.

from operator import *

str_ops = {'<':lt,'>':gt,'==':eq} # etc
op = str_ops.get(my_operator)
if op:
    result = op(lhs,rhs)

The above is one way to map string representations of operators onto their actual action.

这篇关于调用不带括号的函数python的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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