下面小编就为大家带来一篇老生常谈python之鸭子类和多态。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
一、 什么是多态
f35d6e602fd7d0f0edfa6f7d103c1b57一种类型具有多种类型的能力
2cc198a1d5eb0d3eb508d858c9f5cbdb允许不同的对象对同一消息做出灵活的反应
5bdf4c78156c7953567bb5a0aef2fc53以一种通用的方式对待个使用的对象
23889872c2e8594e0f446a471a78ec4c非动态语言必须通过继承和接口的方式来实现
二、 python中的多态
<1>通过继承实现多态(子类可以作为父类来使用) <2>子类通过重载父类的方法实现多态 class Animal: def move(self): print('animal is moving....') class Dog(Animal): pass def move(obj): obj.move() >>>move(Animal()) >>>animal is moving.... >>>move(Dog()) >>>animal is moving.... class Fish(Animal): def move(self): print('fish is moving....') >>>move(Fish()) >>>fish is moving....
三、 动态语言和鸭子类型
f35d6e602fd7d0f0edfa6f7d103c1b57变量绑定的类型是不确定的
2cc198a1d5eb0d3eb508d858c9f5cbdb函数和方法可以接收任何类型的参数
5bdf4c78156c7953567bb5a0aef2fc53调用方法时不检查提供的参数类型
23889872c2e8594e0f446a471a78ec4c调用是否成功有参数的方法和属性确定,调用不成功则抛出错误
43ad812d3a971134e40facaca816c822不用实现接口
class P: def init(self, x, y): self.x = x self.y = y def add(self, oth): return P(self.x+oth.x, self.y+oth.y) def info(self): print(self.x, self.y) class D(P): def init(self, x, y, z): super.init(x, y) self.z = z def add(self, oth): return D(self.x+oth.x, self.y+oth.y, self.z+oth.z) def info(self): print(self.x, self.y, self.z) class F: def init(self, x, y, z): self.x = x self.y = y self.z = z def add(self, oth): return D(self.x+oth.x, self.y+oth.y, self.z+oth.z) def info(self): print(self.x, self.y, self.z) def add(a, b): return a + b if name == 'main': add(p(1, 2), p(3, 4).info()) add(D(1, 2, 3), D(1, 2, 3).info()) add(F(2, 3, 4), D(2, 3, 4).info())
四、 多态的好处
f35d6e602fd7d0f0edfa6f7d103c1b57可实现开放的扩展和修改的封闭
2cc198a1d5eb0d3eb508d858c9f5cbdb使python程序更加的灵活
以上是分享python中鸭子类和多态实例的详细内容。更多信息请关注PHP中文网其他相关文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

数组的同质性对性能的影响是双重的:1)同质性允许编译器优化内存访问,提高性能;2)但限制了类型多样性,可能导致效率低下。总之,选择合适的数据结构至关重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,内存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。