什么是“针对接口而不是对象的代码”的Python版本?

编程入门 行业动态 更新时间:2024-10-07 06:43:04
本文介绍了什么是“针对接口而不是对象的代码”的Python版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

来自这里。

语句针对接口的代码,而不是对象是否具有任何意义Python?

Does the statement "Code against an interface, not an object" have any significance in Python?

我正在寻找像 Original Question 但是使用Python片段和思想。

I'm looking for answers like the ones in the Original Question but with Python snippets and thoughts.

推荐答案

对接口而不是对象的代码在Python中没有字面意义,因为该语言没有接口功能。 粗略 Python等价物是使用鸭子打字。如果你想看一个对象是否是一个鸭子,换句话说,你应该检查它是否有一个 quack()方法,或者更好的是尝试 quack()并提供适当的错误处理,而不是测试它是否是 Duck 的实例。

"Code against an interface, not an object" doesn't make literal sense in Python because the language doesn't have an interface feature. The rough Python equivalent is "use duck typing." If you want to see if an object is a duck, in other words, you should check to see whether it has a quack() method, or better yet try to quack() and provide appropriate error handling, not test to see if it is an instance of Duck.

Python中常见的鸭子类型是文件(嗯,实际上是文件类对象),映射( dict -like objects),callables (类似函数的对象),序列( list -like objects)和iterables(你可以迭代的东西,可以是容器或生成器)。

Common duck types in Python are files (well, really, file-like objects), mappings (dict-like objects), callables (function-like objects), sequences (list-like objects), and iterables (things you can iterate over, which can be containers or generators).

作为一个例子,想要文件的Python特性通常很乐意接受一个实现它需要的 file 方法的对象;它不需要从文件类派生。例如,要使用对象作为标准输出,它需要的主要是 write()方法(也许 flush() 和 close(),无需实际执行任何操作)。类似地,可调用的是具有 __ call __()方法的任何对象;它不需要从函数类型派生(事实上,你不能从函数类型派生)。

As an example, Python features that want a file will generally be happy to accept an object that implements the methods of file it needs; it needn't be derived from the file class. To use an object as standard out, for example, the main thing it is going to need is a write() method (and maybe flush() and close(), which needn't actually do anything). Similarly, a callable is any object that has a __call__() method; it needn't be derived from the function type (in fact, you can't derive from the function type).

你应该采取类似的方法。检查您要对对象执行的操作所需的方法和属性。更好的是,记录您的期望并假设任何调用您的代码的人都不是完全的doofus。 (如果他们给你一个你不能使用的对象,他们肯定会从他们得到的错误中快速地找出它。)仅在必要时测试特定类型。 有时是必要的,这就是为什么Python给你 type(), isinstance()和 issubclass(),但要小心它们。

You should take a similar approach. Check for the methods and attributes you need for what you're going to do with an object. Better yet, document what you expect and assume that whoever is calling your code is not a total doofus. (If they give you an object you can't use, they will certainly figure that out quickly enough from the errors they get.) Test for specific types only when necessary. It is necessary at times, which is why Python gives you type(), isinstance(), and issubclass(), but be careful with them.

Python的鸭子输入相当于代码对于一个接口,而不是一个对象在某种意义上,建议你不要让你的代码过于依赖于对象的类型,而是要看它是否具有你需要的接口。不同之处在于,在Python中,接口只是指提供特定行为的对象的非正式属性和方法捆绑,而不是专门命名为接口的语言构造。

Python's duck typing is equivalent to "code against an interface, not an object" in the sense that you're advised not to make your code too reliant on an object's type, but rather to see whether it has the interface you need. The difference is that in Python, "interface" just means an informal bundle of attributes and methods of an object that provide a certain behavior, rather than a language construct specifically named interface.

您可以使用 abc 模块在某种程度上形式化Python接口,这可以让您声明给定类是使用您期望的任何标准的给定抽象基类(接口)的子类,例如它具有属性颜色, tail_length , quack , quack 可以调用。但这仍然比具有接口功能的静态语言严格得多。

You can formalize Python "interfaces" to some extent using the abc module, which allows you to declare that a given class is a subclass of a given "abstract base class" (interface) using any criteria you desire, such as "it has attributes color, tail_length, and quack, and quack is callable." But this is still much less strict than static languages having an interface feature.

更多推荐

什么是“针对接口而不是对象的代码”的Python版本?

本文发布于:2023-11-29 15:41:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646826.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   接口   对象   版本   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!