位置: 编程技术 - 正文

Python探索之静态方法和类方法的区别详解(python静态变量和静态方法)

编辑:rootadmin

推荐整理分享Python探索之静态方法和类方法的区别详解(python静态变量和静态方法),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:python中的静态方法如何调用,python 静态代码分析,python 静态代码分析,python 静态代码分析,python 静态代码分析,python 静态代码分析,python静态类型检查,python静态方法的作用,内容如对您有帮助,希望把文章链接给更多的朋友!

面相对象程序设计中,类方法和静态方法是经常用到的两个术语。逻辑上讲:类方法是只能由类名调用;静态方法可以由类名或对象名进行调用。

python staticmethod and classmethod

尽管 classmethod 和 staticmethod 非常相似,但在用法上依然有一些明显的区别。classmethod 必须有一个指向 类对象 的引用作为第一个参数,而 staticmethod 可以没有任何参数。

让我们看几个例子。

例子 ? Boilerplate

Let's assume an example of a class, dealing with date information (this is what will be our boilerplate to cook on):

This class obviously could be used to store information about certain dates (without timezone information; let's assume all dates are presented in UTC).

很明显,这个类的对象可以存储日期信息(不包括时区,假设他们都存储在 UTC)。

Here we have __init__, a typical initializer of Python class instances, which receives arguments as a typical instancemethod, having the first non-optional argument (self) that holds reference to a newly created instance.

这里的 init 方法用于初始化对象的属性,它的第一个参数一定是 self,用于指向已经创建好的对象。

Class Method

We have some tasks that can be nicely done using classmethods.Let's assume that we want to create a lot of Date class instances having date information coming from outer source encoded as a string of next format (‘dd-mm-yyyy'). We have to do that in different places of our source code in project.

利用 classmethod 可以做一些很棒的东西。

Python探索之静态方法和类方法的区别详解(python静态变量和静态方法)

比如我们可以支持从特定格式的日期字符串来创建对象,它的格式是 (‘dd-mm-yyyy')。很明显,我们只能在其他地方而不是 init 方法里实现这个功能。

So what we must do here is:Parse a string to receive day, month and year as three integer variables or a 3-item tuple consisting of that variable.Instantiate Date by passing those values to initialization call.This will look like:

大概步骤:

解析字符串,得到整数 day, month, year。

使用得到的信息初始化对象

代码如下

理想的情况是 Date 类本身可以具备处理字符串时间的能力,解决了重用性问题,比如添加一个额外的方法。

For this purpose, C++ has such feature as overloading, but Python lacks that feature- so here's when classmethod applies. Lets create another “constructor”.

C++ 可以方便的使用重载来解决这个问题,但是 python 不具备类似的特性。 所以接下来我们要使用 classmethod 来帮我们实现。

Let's look more carefully at the above implementation, and review what advantages we have here:We've implemented date string parsing in one place and it's reusable now.Encapsulation works fine here (if you think that you could implement string parsing as a single function elsewhere, this solution fits OOP paradigm far better).cls is an object that holds class itself, not an instance of the class. It's pretty cool because if we inherit our Date class, all children will have from_string defined also.

让我们在仔细的分析下上面的实现,看看它的好处。

我们在一个方法中实现了功能,因此它是可重用的。 这里的封装处理的不错(如果你发现还可以在代码的任意地方添加一个不属于 Date 的函数来实现类似的功能,那很显然上面的办法更符合 OOP 规范)。 cls 是一个保存了 class 的对象(所有的一切都是对象)。 更妙的是, Date 类的衍生类都会具有 from_string 这个有用的方法。

Static methodWhat about staticmethod&#; It's pretty similar to classmethod but doesn't take any obligatory parameters (like a class method or instance method does).Let's look at the next use case.We have a date string that we want to validate somehow. This task is also logically bound to Date class we've used so far, but still doesn't require instantiation of it.Here is where staticmethod can be useful. Let's look at the next piece of code:

staticmethod 没有任何必选参数,而 classmethod 第一个参数永远是 cls, instancemethod 第一个参数永远是 self。

So, as we can see from usage of staticmethod, we don't have any access to what the class is- it's basically just a function, called syntactically like a method, but without access to the object and it's internals (fields and another methods), while classmethod does.

所以,从静态方法的使用中可以看出,我们不会访问到 class 本身 ? 它基本上只是一个函数,在语法上就像一个方法一样,但是没有访问对象和它的内部(字段和其他方法),相反 classmethod 会访问 cls, instancemethod 会访问 self。

总结

标签: python静态变量和静态方法

本文链接地址:https://www.jiuchutong.com/biancheng/368742.html 转载请保留说明!

上一篇:Python探索之爬取电商售卖信息代码示例(python爬取教程)

下一篇:Python编程之Re模块下的函数介绍(python模拟reversed功能)

  • 外贸出口退税流程视频
  • 租赁合同管理台账范本
  • 自查补交以前年度增值税怎么做账
  • 报关时电子口岸电子委托应该什么时候做
  • 申请100万增值税发票资格
  • 工商年报的应交税费包含哪些
  • 特许权使用费怎样向海关申报
  • 三栏式和多栏式的账目有哪些
  • 核定和查账区别
  • 企业付装修费怎么入账
  • 影视公司招演员
  • 无形资产研发成功并申请专利
  • 现金折扣 会计处理
  • 地税迁移期间能开发票吗
  • 房地产教育培训
  • 认证后的发票可以留存多久
  • 农产品收购发票使用范围
  • 金税盘不交年费可以正常使用吗
  • 工程价款是什么意思
  • 公司注销了帐户钱能办款
  • 个人购买余额宝安全吗?
  • 独立核算的分公司可以享受小微企业吗
  • 汇算清缴工资填哪个数
  • 如何在没有开瓶器的情况下开红酒
  • 其它权益工具投资处置时其他综合收益转那
  • 外账的结转成本是什么
  • PHP:curl_multi_select()的用法_cURL函数
  • 预收账款调增应纳税所得额
  • 银行对账单干嘛用
  • 利息收入可以冲减研发费用
  • 销货退回与折让属于什么科目
  • 环境保护税的征税范围及计税依据
  • 正则表达式大全(整理版)
  • 什么是大语言模型(LLM)?
  • typescript类型别名
  • php gdb
  • 固定资产计提折旧的方法
  • 应付职工薪酬属于什么会计科目
  • 房东租金收据
  • 上传附件不成功怎么回事
  • sql有什么
  • notepad+v
  • 预算收入的核算
  • 给离职员工买社保的风险
  • 企业赠送客户礼品涉税
  • 季度利息收入分录
  • 专用发票超过360天未认证
  • 月入一万怎么样
  • 购买材料发票未到如何做账?
  • 差额征税要交多少钱
  • sqlserver2008无法连接到local用户登录失败
  • 一般纳税人领票提交什么资料
  • 弥补以前年度亏损怎么算
  • 先给发票后付款做账
  • 给客户退款怎么说
  • 车辆保险费印花税计税金额含进项税吗
  • 暂时性差异的产生
  • 销售软件产品
  • 运输公司车辆保养记录表格
  • 100%直接控制的母子公司之间,母公司向子公司
  • mysql5.5.27安装教程与配置
  • MySQL 5.7双主同步部分表的实现过程详解
  • win10预览版21277
  • mac迅雷不限速
  • 怎么调用windows api
  • win10如何更换
  • 升级打装备的手游
  • extjs 为某个事件设置拦截器
  • 如何获取硬盘所有文件的列表
  • nodejs 入门
  • jquery遍历object
  • jq filter过滤
  • js扩展名是什么文件
  • unity3d documentation
  • js获取
  • 企业购房契税怎么入账
  • 北京海淀区国税有几个办税大厅?
  • 青岛税务局局长是什么级别?
  • 版权登记条件
  • 国内进口税最低是哪里
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设