首页 > 编程语言 >Python - Protocols

Python - Protocols

时间:2024-08-15 22:05:06浏览次数:15  
标签:Python duck class Flyer typing method Protocols

Introduced in Python 3.8 via the typing module, Protocols offer a more flexible approach than ABCs, known as structural duck typing, where an object is considered valid if it has certain attributes or methods, regardless of its actual inheritance.

Unlike traditional duck typing, where type compatibility is determined at runtime, structural duck typing allows for type checking at compile time. This means that you can catch type errors before your code even runs (while in your IDE, for example), making your programs more robust and easier to debug.

The key advantage of using Protocols is that they focus on what an object can do, rather than what it is. In other words, if an object walks like a duck and quacks like a duck, it’s a duck, regardless of its actual inheritance hierarchy. This is particularly useful in a dynamically typed language such as Python, where an object’s behavior is more important than its actual type.

For example, you can define a Drawable protocol that requires a draw() method. Any class that implements this method would implicitly satisfy the protocol without having to explicitly inherit from it.

Here’s a quick example to illustrate the concept. Let’s say you need a Protocol named Flyer that requires a fly() method. You can define it as follows:

from typing import Protocol


class Flyer(Protocol):
    def fly(self) -> None:
        ...

And that’s it! Now, any class that has a fly() method would be considered Flyer, whether it explicitly inherits from the Flyer class or not. This is a powerful feature that allows you to write more generic and reusable code and adheres to the principle of composition over inheritance.

标签:Python,duck,class,Flyer,typing,method,Protocols
From: https://www.cnblogs.com/zhangzhihui/p/18361881

相关文章

  • python-flask计算机毕业设计中国服饰文献资料管理平台(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着中国传统文化的复兴与全球文化交流的日益频繁,中国服饰作为承载深厚历史文化底蕴的重要载体,其研究价值与日俱增。然而,当前中国服饰文献......
  • Python 通过UDP传输超过64k的信息
    在UDP中,单个数据包的最大尺寸通常受到网络层的限制,这通常被称为最大传输单元(MTU)。在以太网环境中,标准的MTU大小通常为1500字节。尽管有些网络环境可能支持更大的数据包,但是UDP数据包的理论最大限制是65535字节(64KB),这是由于UDP头部的16位长度字段决定的。然而,如果你需要发送超过这......
  • Python - Foundational Design Principles
    EncapsulateWhatVariesOneofthemostcommonchallengesinsoftwaredevelopmentisdealingwithchange.Requirements evolve,technologiesadvance,anduserneedsalsochange.Therefore,itiscrucialtowritecodethat canadaptwithoutcausingaripple......
  • 使用 Flask、Celery 和 Python 实现每月定时任务
    为了创建一个使用Flask、Celery和Python实现的每月定时任务,我们需要按照以下步骤进行:1.安装必要的库我们需要安装Flask、Celery和Redis(作为消息代理)。我们可以使用pip来安装它们:bash复制代码​pipinstallflaskceleryredis2.设置Flask和Celery首先,我们需......
  • Python数据分析项目实战01_票房榜单分析和pyecharts大屏可视化
    一:最终大屏效果展示由于生成了html网页只能在本地上显示,这个大屏是动态的,只能显示其中的图片。如果要分享给他人使用,就需要将html源码转为网页。生成的html源码入口:‬​‍​‌‌​⁠​​​‌‍​​​​​‬​​​​​‬​​​​‌​​​‌‍​​​​​⁠‌‬‌​​‌Python数......
  • Levenshtein,一个超实用的Python库
    Levenshtein距离,又称编辑距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。在Python中,Levenshtein库提供了计算Levenshtein距离和相关度量的高效算法。Levenshtein的功能特性高效性:Levenshtein算法在计算字符串距离时具有较高效率。灵活性:支持自定义替换、......
  • awesome-django,一个超酷的Python库
    awesome-django是一个开源的Django扩展库,汇集了众多实用的第三方Django插件和工具,旨在帮助开发者快速构建高质量、功能丰富的Django应用程序。通过awesome-django,开发者可以轻松集成常用的功能,提升开发效率。如何安装awesome-django首先,确保你已经安装了最新版本......
  • cryptography,是一个强大的 Python 库
    cryptography是一个强大的Python库,提供了加密、解密、签名、验证等一系列安全功能,帮助开发者轻松实现数据安全。该库适用于各种加密需求,从简单的数据加密到复杂的网络安全通信。cryptography的功能特性安全性:提供多种加密算法,确保数据安全。灵活性:支持多种加密协议和标......
  • 盘点一个Python图像读取的小问题
    大家好,我是皮皮。一、前言前几天在Python最强王者交流群【Wayne.Wu】问了一个Python图像读取的问题,这里拿出来给大家分享下。一看文字这么多,感觉还是挺复杂的,都有点让人头大的感觉。二、实现过程经过提示,粉丝自己明白了,先进行本地保存,之后再读取,就可以搞定了。顺利地解决......
  • 云计算实训30——mysql主从复制同步、mysql5.7版本安装配置、python操作mysql数据库、
    一、mysql主从复制及同步1、mysql主从自动开机同步2、配置mysql5.7版本mysql-5.7.44-linux-glibc2.12-x86_64.tar启动服务、登录对数据库进行基本操作3、使用python操纵mysql数据库4、编辑python脚本自动化操纵mysql数据库二、mycat读写分离......