了解Python中的Callable

Functions and classes are the most common things we use in our daily development. We invoke them, pass them around and yet never wonder what makes them so amazing. Well the short answer is callable protocol; for the long answer keep reading!

函数和类是我们日常开发中最常用的东西。 我们调用它们,将它们传递出去,却永远不奇怪是什么使它们如此惊人。 答案很简单,就是可调用协议。 对于长答案,请继续阅读!

定义📢 (Definition 📢)

Objects defining the __call__ method is known as callable. Or basically callable is anything you can call using the parenthesis () and pass arguments to it. Yes I am basically talking about a function.

定义__call__方法的对象称为可调用对象。 或基本上可调用的是您可以使用括号()并将其传递参数的任何东西。 是的,我基本上是在谈论功能。

__call__方法🤙 (__call__ method 🤙)

__call__ is one of the most interesting dunder method in Python. It is what most of the built-in functions make use of. If we peek into the type of some of these built-in functions then we often see the result as a class.

__call__是Python中最有趣的dunder方法之一。 这是大多数内置函数所利用的。 如果我们窥视其中一些内置函数的type ,那么我们通常会将结果视为一个class

>>> range
<class 'range'>>>> zip
<class 'zip'>>>> int
<class 'int'>

You get the gist. But how is a class acting as a function? I mean you can just zip(iterable1, iterable2)and you get your result without further invoking any other methods of the class.

你明白了。 但是,类如何充当函数? 我的意思是,您只需zip(iterable1, iterable2)可以得到结果,而无需进一步调用该类的任何其他方法。

Just imagine using zip like if it were a normal class.

试想像使用 zip 一样, 就像使用 普通类一样。

processor = ['Intel', 'Ryzen', 'Apple Silicon']
year = [2018, 2019, 2020]zipped = zip(processor, year)
zipped.start()

This definitely isn’t intuitive to work with. So how does it work?

这绝对是不直观的。 那么它是怎样工作的?

使用__call__方法🔨 (Using __call__ method 🔨)

Well behind the scenes it’s because of the __call__ method. This method is used in classes if we want the instance of the class to be callable.

幕后的原因是__call__方法。 如果我们希望类的实例是可调用的,则在类中使用此方法。

What do I mean by that? Let’s take an example of a class that prints the square of a number.

那是什么意思 让我们以一个打印数字平方的类为例。

class Square:

def __call__(self, num):
print(num * num)>>> sq = Square() # create an instance
>>> sq(5) # invoke
25

This is similar to executing:

这类似于执行:

>>> sq = Square()
>>> sq.__call__(5)
25

But with the __call__ dunder method we don’t have to do that. We can directly invoke the instance like a normal function.

但是,使用__call__ dunder方法,我们不必这样做。 我们可以像正常函数一样直接调用实例。

You may have noticed, we don’t need to use the __init__ constructor to pass the value. Since the class instance acts like a function, then it can take arguments like a function without a need of a constructor.

您可能已经注意到,我们不需要使用__init__构造函数来传递值。 由于类实例的行为就像一个函数,因此它可以像函数一样接受参数,而无需构造函数。

测试可调用的👨 (Testing callable 👨‍🔬)

A class or a function is callable by default; but the instance is callable with __call__ dunder method only. We can check this by using the callable() function.

默认情况下,可以调用类或函数; 但该实例仅可使用__call__ dunder方法__call__ 。 我们可以使用callable()函数进行检查。

Take for example a class without a __call__ method.

以没有__call__方法的类为例。

class Random:
pass>>> callable(Random)
True>>> r = Random()
>>> callable(r)
False

On the contrary, let’s take our Square class from earlier example.

相反,让我们从前面的示例中获取Square类。

>>> callable(Square)
True>>> callable(sq)
True

结论🚀 (Conclusion 🚀)

Next time when you’re creating a class for something and it needs to return a value in an instant; you can make use of the __call__ method.

下次当您为某个东西创建一个类时,它需要立即返回一个值; 您可以使用__call__方法。

I hope this blog has been a help to understand an underlying concept involved in functions and classes. If you have any queries or suggestions, let’s discuss them in the comment section.

我希望该博客对理解函数和类所涉及的基本概念有所帮助。 如果您有任何疑问或建议,请在评论部分进行讨论。

翻译自: https://medium.com/@yankee.exe/understanding-callable-in-python-3dd6e4fd38e

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值