如何正确使用static、class、abstract方法二

如何正确使用static、class、abstract方法

####How methods work in Python
A method is a function that is stored as a class attribute. You can declare and access such a function this way:
####方法在Python中是如何工作的
方法就是一个函数,它作为一个类属性而存在,你可以用如下方式来声明、访问一个函数:

>>> class Pizza(object):
...     def __init__(self, size):
...         self.size = size
...     def get_size(self):
...         return self.size
...
>>> Pizza.get_size
<unbound method Pizza.get_size>

What Python tells you here, is that the attribute get_size of the class Pizza is a method that is unbound. What does this mean? We’ll know as soon as we’ll try to call it:
Python在告诉你,属性get_size是类Pizza的一个未绑定方法。这是什么意思呢?很快我们就会知道答案:

>>> Pizza.get_size()
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: unbound method get_size() must be called with Pizza instance as first argument (got nothing instead)

We can’t call it because it’s not bound to any instance of Pizza. And a method wants an instance as its first argument (in Python 2 it must be an instance of that class; in Python 3 it could be anything). Let’s try to do that then:
我们不能这么调用,因为它还没有绑定到Pizza类的任何实例上,它需要一个实例作为第一个参数传递进去(Python2必须是该类的实例,Python中可以是任何东西),尝试一下:

>>> Pizza.get_size(Pizza(42))
42

It worked! We called the method with an instance as its first argument, so everything’s fine. But you will agree with me if I say this is not a very handy way to call methods; we have to refer to the class each time we want to call a method. And if we don’t know what class is our object, this is not going to work for very long.
太棒了,现在用一个实例作为它的的第一个参数来调用,整个世界都清静了,如果我说这种调用方式还不是最方便的,你也会这么认为的;没错,现在每次调用这个方法的时候我们都不得不引用这个类,如果不知道哪个类是我们的对象,长期看来这种方式是行不通的。

So what Python does for us, is that it binds all the methods from the class Pizza to any instance of this class. This means that the attribute get_size of an instance of Pizza is a bound method: a method for which the first argument will be the instance itself.
那么Python为我们做了什么呢,它绑定了所有来自类Pizza的方法以及该类的任何一个实例的方法。也就意味着现在属性get_sizePizza的一个实例对象的绑定方法,这个方法的第一个参数就是该实例本身。

>>> Pizza(42).get_size
<bound method Pizza.get_size of <__main__.Pizza object at 0x7f3138827910>>
>>> Pizza(42).get_size()
42

As expected, we don’t have to provide any argument to get_size, since it’s bound, its self argument is automatically set to our Pizza instance. Here’s a even better proof of that:
和我们预期的一样,现在不再需要提供任何参数给get_size,因为它已经是绑定的,它的self参数会自动地设置给Pizza实例,下面代码是最好的证明:

>>> m = Pizza(42).get_size
>>> m()
42

Indeed, you don’t even have to keep a reference to your Pizza object. Its method is bound to the object, so the method is sufficient to itself.
更有甚者,你都没必要使用持有Pizza对象的引用了,因为该方法已经绑定到了这个对象,所以这个方法对它自己来说是已经足够了。

But what if you wanted to know which object this bound method is bound to? Here’s a little trick:
也许,如果你想知道这个绑定的方法是绑定在哪个对象上,下面这种手段就能得知:

>>> m = Pizza(42).get_size
>>> m.__self__
<__main__.Pizza object at 0x7f3138827910>
>>> # You could guess, look at this:
...
>>> m == m.__self__.get_size
True

Obviously, we still have a reference to our object, and we can find it back if we want.

In Python 3, the functions attached to a class are not considered as unbound method anymore, but as simple functions, that are bound to an object if required. So the principle stays the same, the model is just simplified.

显然,该对象仍然有一个引用存在,只要你愿意你还是可以把它找回来。

在Python3中,依附在类上的函数不再当作是未绑定的方法,而是把它当作一个简单地函数,如果有必要它会绑定到一个对象身上去,原则依然和Python2保持一致,但是模块更简洁:

>>> class Pizza(object):
...     def __init__(self, size):
...         self.size = size
...     def get_size(self):
...         return self.size
...
>>> Pizza.get_size
<function Pizza.get_size at 0x7f307f984dd0>

####Static methods
Static methods are a special case of methods. Sometimes, you’ll write code that belongs to a class, but that doesn’t use the object itself at all. For example:
####静态方法
静态方法是一类特殊的方法,有时你可能需要写一个属于这个类的方法,但是这些代码完全不会使用到实例对象本身,例如:

class Pizza(object):@staticmethoddef mix_ingredients(x, y):return x + ydef cook(self):return self.mix_ingredients(self.cheese, self.vegetables)

In such a case, writing mix_ingredients as a non-static method would work too, but it would provide it a self argument that would not be used. Here, the decorator @staticmethod buys us several things:
这个例子中,如果把mix_ingredients作为非静态方法同样可以运行,但是它要提供self参数,而这个参数在方法中根本不会被使用到。这里的*@staticmethod*装饰器可以给我们带来一些好处:

  • Python doesn’t have to instantiate a bound-method for each Pizza object we instiantiate. Bound methods are objects too, and creating them has a cost. Having a static method avoids that:

  • Python不再需要为Pizza对象实例初始化一个绑定方法,绑定方法同样是对象,但是创建他们需要成本,而静态方法就可以避免这些。

      >>> Pizza().cook is Pizza().cookFalse>>> Pizza().mix_ingredients is Pizza.mix_ingredientsTrue>>> Pizza().mix_ingredients is Pizza().mix_ingredientsTrue
    
  • It eases the readability of the code: seeing @staticmethod, we know that the method does not depend on the state of object itself;

  • 可读性更好的代码,看到*@staticmethod*我们就知道这个方法并不需要依赖对象本身的状态。

  • It allows us to override the mix_ingredients method in a subclass. If we used a function mix_ingredients defined at the top-level of our module, a class inheriting from Pizza wouldn’t be able to change the way we mix ingredients for our pizza without overriding cook itself.

  • 可以在子类中被覆盖,如果是把mix_ingredients作为模块的顶层函数,那么继承自Pizza的子类就没法改变pizza的mix_ingredients了如果不覆盖cook的话。

####Class methods
Having said that, what are class methods? Class methods are methods that are not bound to an object, but to… a class!
####类方法
话虽如此,什么是类方法呢?类方法不是绑定到对象上,而是绑定在类上的方法。

>>> class Pizza(object):
...     radius = 42
...     @classmethod
...     def get_radius(cls):
...         return cls.radius
... 
>>> 
>>> Pizza.get_radius
<bound method type.get_radius of <class '__main__.Pizza'>>
>>> Pizza().get_radius
<bound method type.get_radius of <class '__main__.Pizza'>>
>>> Pizza.get_radius is Pizza().get_radius
True
>>> Pizza.get_radius()
42

Whatever the way you use to access this method, it will be always bound to the class it is attached too, and its first argument will be the class itself (remember that classes are objects too).
无论你用哪种方式访问这个方法,它总是绑定到了这个类身上,它的第一个参数是这个类本身(记住:类也是对象)。

When to use this kind of methods? Well class methods are mostly useful for two types of methods:
什么时候使用这种方法呢?类方法通常在以下两种场景是非常有用的:

  • Factory methods, that are used to create an instance for a class using for example some sort of pre-processing. If we use a @staticmethod instead, we would have to hardcode the Pizza class name in our function, making any class inheriting from Pizza unable to use our factory for its own use.

  • 工厂方法:它用于创建类的实例,例如一些预处理。如果使用*@staticmethod代替,那我们不得不硬编码Pizza类名在函数中,这使得任何继承Pizza*的类都不能使用我们这个工厂方法给它自己用。

      class Pizza(object):def __init__(self, ingredients):self.ingredients = ingredients@classmethoddef from_fridge(cls, fridge):return cls(fridge.get_cheese() + fridge.get_vegetables())
    
  • Static methods calling static methods: if you split a static methods in several static methods, you shouldn’t hard-code the class name but use class methods. Using this way to declare ou method, the Pizza name is never directly referenced and inheritance and method overriding will work flawlessly

  • 调用静态类:如果你把一个静态方法拆分成多个静态方法,除非你使用类方法,否则你还是得硬编码类名。使用这种方式声明方法,Pizza类名明永远都不会在被直接引用,继承和方法覆盖都可以完美的工作。

      class Pizza(object):def __init__(self, radius, height):self.radius = radiusself.height = height@staticmethoddef compute_area(radius):return math.pi * (radius ** 2)@classmethoddef compute_volume(cls, height, radius):return height * cls.compute_area(radius)def get_volume(self):return self.compute_volume(self.height, self.radius)
    

####Abstract methods
An abstract method is a method defined in a base class, but that may not provide any implementation. In Java, it would describe the methods of an interface.
####抽象方法
抽象方法是定义在基类中的一种方法,它没有提供任何实现,类似于Java中接口(Interface)里面的方法。

So the simplest way to write an abstract method in Python is:
在Python中实现抽象方法最简单地方式是:

class Pizza(object):def get_radius(self):raise NotImplementedError

Any class inheriting from Pizza should implement and override the get_radius method, otherwise an exception would be raised.

This particular way of implementing abstract method has a drawback. If you write a class that inherits from Pizza and forget to implement get_radius, the error will only be raised when you’ll try to use that method.
任何继承自Pizza的类必须覆盖实现方法get_radius,否则会抛出异常。

这种抽象方法的实现有它的弊端,如果你写一个类继承Pizza,但是忘记实现get_radius,异常只有在你真正使用的时候才会抛出来。

>>> Pizza()
<__main__.Pizza object at 0x7fb747353d90>
>>> Pizza().get_radius()
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "<stdin>", line 3, in get_radius
NotImplementedError

There’s a way to triggers this way earlier, when the object is being instantiated, using the abc module that’s provided with Python.
还有一种方式可以让错误更早的触发,使用Python提供的abc模块,对象被初始化之后就可以抛出异常:

import abcclass BasePizza(object):__metaclass__  = abc.ABCMeta@abc.abstractmethoddef get_radius(self):"""Method that should do something."""

Using abc and its special class, as soon as you’ll try to instantiate BasePizza or any class inheriting from it, you’ll get a TypeError.
使用abc后,当你尝试初始化BasePizza或者任何子类的时候立马就会得到一个TypeError,而无需等到真正调用get_radius的时候才发现异常。

>>> BasePizza()
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class BasePizza with abstract methods get_radius

####Mixing static, class and abstract methods
When building classes and inheritances, the time will come where you will have to mix all these methods decorators. So here’s some tips about it.

Keep in mind that declaring a method as being abstract, doesn’t freeze the prototype of that method. That means that it must be implemented, but i can be implemented with any argument list.
####混合静态方法、类方法、抽象方法
当你开始构建类和继承结构时,混合使用这些装饰器的时候到了,所以这里列出了一些技巧。

记住,声明一个抽象的方法,不要凝固方法的原型,这就意味着你必须实现它,但是我可以用任何参数列表来实现:

import abcclass BasePizza(object):__metaclass__  = abc.ABCMeta@abc.abstractmethoddef get_ingredients(self):"""Returns the ingredient list."""class Calzone(BasePizza):def get_ingredients(self, with_egg=False):egg = Egg() if with_egg else Nonereturn self.ingredients + egg

This is valid, since Calzone fulfil the interface requirement we defined for BasePizza objects. That means that we could also implement it as being a class or a static method, for example:
这样是允许的,因为Calzone满足BasePizza对象所定义的接口需求。同样我们也可以用一个类方法或静态方法来实现:

import abcclass BasePizza(object):__metaclass__  = abc.ABCMeta@abc.abstractmethoddef get_ingredients(self):"""Returns the ingredient list."""class DietPizza(BasePizza):@staticmethoddef get_ingredients():return None

This is also correct and fulfil the contract we have with our abstract BasePizza class. The fact that the get_ingredients method don’t need to know about the object to return result is an implementation detail, not a criteria to have our contract fulfilled.

Therefore, you can’t force an implementation of your abstract method to be a regular, class or static method, and arguably you shouldn’t. Starting with Python 3 (this won’t work as you would expect in Python 2, see issue5867), it’s now possible to use the @staticmethod and @classmethod decorators on top of @abstractmethod:
这同样是正确的,因为它遵循抽象类BasePizza设定的契约。事实上get_ingredients方法并不需要知道返回结果是什么,结果是实现细节,不是契约条件。

因此,你不能强制抽象方法的实现是一个常规方法、或者是类方法还是静态方法,也没什么可争论的。从Python3开始(在Python2中不能如你期待的运行,见issue5867),在abstractmethod方法上面使用*@staticmethod@classmethod*装饰器成为可能。

import abcclass BasePizza(object):__metaclass__  = abc.ABCMetaingredient = ['cheese']@classmethod@abc.abstractmethoddef get_ingredients(cls):"""Returns the ingredient list."""return cls.ingredients

Don’t misread this: if you think this going to force your subclasses to implement get_ingredients as a class method, you are wrong. This simply implies that your implementation of get_ingredients in the BasePizza class is a class method.

An implementation in an abstract method? Yes! In Python, contrary to methods in Java interfaces, you can have code in your abstract methods and call it via super():
别误会了,如果你认为它会强制子类作为一个类方法来实现get_ingredients那你就错了,它仅仅表示你实现的get_ingredientsBasePizza中是一个类方法。

可以在抽象方法中做代码的实现?没错,Python与Java接口中的方法相反,你可以在抽象方法编写实现代码通过*super()*来调用它。(译注:在Java8中,接口也提供的默认方法,允许在接口中写方法的实现)

import abcclass BasePizza(object):__metaclass__  = abc.ABCMetadefault_ingredients = ['cheese']@classmethod@abc.abstractmethoddef get_ingredients(cls):"""Returns the ingredient list."""return cls.default_ingredientsclass DietPizza(BasePizza):def get_ingredients(self):return ['egg'] + super(DietPizza, self).get_ingredients()

In such a case, every pizza you will build by inheriting from BasePizza will have to override the get_ingredients method, but will be able to use the default mechanism to get the ingredient list by using super().
这个例子中,你构建的每个pizza都通过继承BasePizza的方式,你不得不覆盖get_ingredients方法,但是能够使用默认机制通过super()来获取ingredient列表。

https://julien.danjou.info/blog/2013/guide-python-static-class-abstract-methods

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/52501.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

关于linux上root连接mysql时遇到的一点小问题以及rsync通过ssh的文件同步传输以及免密码传输的实现

一、关于linux上root连接mysql时遇到的一点小问题 今天因为工作需要&#xff0c;需要使用root连接一下很久没有连接过的mysql服务器了&#xff0c;一看找不到root密码了&#xff0c;记得当时我在搭建整个mysql主从的时候&#xff0c;我明明把root密码记录在了txt文件上的&#…

Modbus-TCP——Libmodbus安装和使用(Ubuntu22.04)

1、简介 Modbus是一种通信协议&#xff0c;广泛用于工业自动化和过程控制领域&#xff0c;允许不同设备之间进行数据交换。libmodbus是一个用于 Modbus 协议的开源库&#xff0c;主要用于开发和实现 Modbus 协议的客户端和服务器应用程序。libmodbus 以 C 语言编写&#xff0c…

003 交换机工作原理及VLAN技术

引言 交换机是企业网络的核心设备&#xff0c;而VLAN技术则帮助网络管理员高效管理和隔离流量。本篇博文将深入剖析交换机的工作原理和VLAN的实际配置技巧。 1. 交换机的基本工作原理 交换机通过MAC地址表将数据帧转发到目标端口。当一个数据帧到达交换机时&#xff0c;交换…

【云原生】MySQL的源码编译

1、实验环境 &#xff08;1&#xff09;虚拟机版本&#xff1a;RHEL7.9 &#xff08;2&#xff09;主机 主机名称IP地址mysql-node1172.25.254.10mysql-node2172.25.254.20 2、实验步骤 注意&#xff1a;我们的两台主机都要进行MySQL源码编译&#xff0c;并且操作相同&…

探索 InternLM 模型能力边界

一、任务介绍 在 CompassArena 中选择双模型对话&#xff0c;与InternLM2.5及另外任意其他模型对话&#xff0c;收集 5 个 InternLM2.5 输出结果不如其他模型的对话案例&#xff0c;以及 InternLM2.5 的 5 个 Good Case。 任务地址&#xff1a;Docs Bad Case 1&#xff1a; 模…

Transforms的学习以及地址问题

一、地址问题 在学习Dataset类的实战与Tensboard的学习中&#xff0c;有出现一些地址的问题&#xff1a; 1、相对地址 相对地址的使用&#xff1a; 使用于在从端口中&#xff0c;打开TensorBoard的页面。使用的就是相对地址&#xff1b;例如&#xff1a; tensorboard --log…

新书推荐:《分布式商业生态战略:数字商业新逻辑与企业数字化转型新策略》

近两年&#xff0c;商业经济环境的不确定性越来越明显&#xff0c;市场经济受到疫情、技术、政策等多方因素影响越来越难以预测&#xff0c;黑天鹅事件时有发生。在国内外经济方面&#xff0c;国际的地缘政治对商业经济产生着重大的影响&#xff0c;例如供应链中断&#xff0c;…

rabbitMQ安装与简单demo

安装 mac安装有了brew很方便&#xff0c;windows的可参考 win10 安装rabbitMQ详细步骤 brew install rabbitmq启动 brew services start rabbitmq关闭 brew services stop rabbitmq出了问题之后可以重启一下 brew services restart rabbitmqsome issue 某些库下载超时 比…

使用vagrant、virtualbox、快速创建kali linux

使用vagrant、virtualbox、快速创建kali linux 初始化kali下载vagrant相应镜像vagrant添加相应镜像创建vagrantfile在vagrantfile根目录执行cmd虚拟机登录密码修改sshd配置 用shell远程链接(可选)可视化界面设置成中文创建成功展示图 添加实体网卡使用kali 破解WiFi密码解决 on…

Godot《躲避小兵》实战之为游戏添加音效

现在&#xff0c;我们已经完成了游戏的所有功能。以下是一些剩余的步骤&#xff0c;为游戏加点“料”&#xff0c;改善游戏体验。 随意用你自己的想法扩展游戏玩法。 背景 默认的灰色背景不是很吸引人&#xff0c;那么我们就来改一下颜色。一种方法是使用 ColorRect节点。将…

Ubuntu技巧-Ubuntu远程访问之电信公网IP

&#x1f4a1; 大家好&#xff0c;我是可夫小子&#xff0c;《小白玩转ChatGPT》专栏作者&#xff0c;关注AIGC、互联网和自媒体。 前面文章介绍了家庭服务器接入外网的三种方式的第一种&#xff0c;今天介绍第二种&#xff0c;即通过获得电脑公网IP&#xff0c;然后再设置动态…

QT Quick QML 网络助手——TCP客户端

GitHub 源码: QmlLearningPro &#xff0c;选择子工程 Nettools.pro QML 其它文章请点击这里: QT QUICK QML 学习笔记 ● 运行效果&#xff1a; 左侧为常用的网络调试工具&#xff0c;右侧为本项目 UI 效果&#xff0c;前端使用 QML &#xff0c;后端使用C &#xff…

linux(Ubuntu )搭C++ 最新版GDAL完整教程

在前面的文章中主要是介绍如何在windows系统下利用python安装gdal库&#xff0c;如下&#xff1a; 如何快速安装GDAL 在linux环境下python安装gdal也可以利用现成的whl文件&#xff0c;但是安装c GDAL环境的比较麻烦&#xff0c;目前网络上大多是安装的老版本的教程&#xff…

自适应学习率(Datawhale X 李宏毅苹果书 AI夏令营)

传统的梯度下降方法在优化过程中常常面临学习率设置不当的问题。固定的学习率在训练初期可能过大&#xff0c;导致模型训练不稳定&#xff0c;而在后期可能过小&#xff0c;导致训练速度缓慢。为了克服这些问题&#xff0c;自适应学习率方法应运而生。这些方法通过动态调整学习…

Spring常用的注解有哪些?作用是什么?怎么用?

一、怎么用&#xff1f; 首先需要在bean.xml文件中配置注解组件扫描&#xff0c;只有配置了组件扫描&#xff0c;spring才会去指定包下面扫描你使用了哪些注解&#xff0c;并让其生效。例如&#xff1a; <!--spring会去扫描com.frank包及其子包下所有的注解&#xff0c;并让…

引号的艺术:用CSS quotes 属性打造个性化引用

引号的艺术&#xff1a;用CSS quotes 属性打造个性化引用 摘要 CSS 的 quotes 属性是一个强大的工具&#xff0c;它允许开发者自定义网页上的引用标记&#xff0c;从而增强网页的视觉效果和用户体验。本文将深入探讨 quotes 属性的使用方法&#xff0c;包括如何设置不同的引号…

ssrf漏洞之——漏洞复现

漏洞介绍 SSRF漏洞&#xff1a;SSRF(Server-Side Request Forgery:服务器端请求伪造) 是一种由恶意访问者构造url&#xff0c;由服务端对此url发起请求的一个安全漏洞。 漏洞原理 SSRF 形成的原因大都是由于服务端提供了从其他服务器应用获取数据的功能&#xff0c;并且没有对目…

Python :使用 YOLOv8、ultralytics 的 YOLO 模型识别图片里面有多少辆车

简简单单 Online zuozuo: 简简单单 Online zuozuo 简简单单 Online zuozuo 简简单单 Online zuozuo 简简单单 Online zuozuo :本心、输入输出、结果 简简单单 Online zuozuo :联系我们:VX :tja6288 / EMAIL: 347969164@qq.com 文章目录 Python :使用 YOLOv8、ultralytic…

S7通信协议从入门到精通_1_Sharp7(C#)类编写西门子 S7系列 plc驱动程序(扩展C++语言)

文章目录 1. 什么是Sharp71.1 什么是Sharp71.2 下载文件中包含的内容1.3 使用示例2. 如何使用Sharp7进行上位机开发2.1 S7-1200的配置2.2 创建示例项目2.3 连接到plc2.3 从DB读取2.4 写入DB2.5 示例代码3. 扩展3.1 C++上位软件通过Snap7开源库访问西门子S7-1200/S7-1500数据块的…

打卡52天------图论(应用题)

一、孤岛的总面积 基础题目 可以自己尝试做一做 。 代码随想录 二、沉没孤岛 和上一题差不多&#xff0c;尝试自己做做 代码随想录 三、水流问题 需要点优化思路&#xff0c;建议先自己读题&#xff0c;相处一个解题方法&#xff0c;有时间就自己写代码&#xff0c;没时间就…