位置: 编程技术 - 正文

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功能)

  • 怎么把销项税额进项税额给转掉
  • 年度应纳税所得额计算公式
  • 工会经费按什么交
  • 简易计税36个月不能变指的是这个项目吗?
  • 出库单上面的单位写谁的
  • 小微企业城建税优惠政策2022
  • 个人向个人借款100万交税吗
  • 远期转账支票怎么取钱
  • 研发加计扣除的标准
  • 其他应收款包括的内容
  • 药店可以开具专票吗
  • 增值税普通发票怎么开
  • 支付职工补贴计入什么科目
  • 基本账户代发代扣怎么操作
  • 应收账款减值准备计提比例
  • 行政单位库存物资管理办法
  • 安装费要交税吗
  • 本月合计怎么做
  • 客户支付货款时扣除了手续费
  • 收到投资款怎么处理帐
  • windows 临时文件夹
  • 如何更改windows用户名
  • 政策性退税流程
  • 如何在qq好友旁边打字
  • php的数组函数
  • 抵债资产账务处理程序
  • 财务人员如何审核合同
  • 收购农产品进项税抵扣税率是多少
  • kindeditor编辑器图片上传
  • 消费税会计分录完整案例
  • 桌山 开普敦
  • 罚款属于应付账款吗
  • 预缴的附加税怎么填表抵减
  • 知识产权专利费包括哪些费用
  • 基建账并账规定
  • yolov3实例
  • 记一次调试YOLOv5+DeepSort车辆跟踪项目的经过
  • 安卓京东抢购
  • 知识图谱的构建方法有两种
  • 公司扣个税查不到怎么办
  • 为什么增值税申报表保存不了
  • 小规模纳税人抵税是普票还是专票
  • 买资产买负债
  • python的元组有什么用
  • 酒店周转材料怎么摊销
  • 销售送客户礼物
  • 没有收据不开发票
  • 现在勾选认证是可以下月初认证?
  • 无形资产如何计提减值
  • 三包适用范围
  • 关于外币折算会计处理的表述中
  • 一般会计准则是什么
  • 暂估入库估多了怎么办
  • 外派人员房租
  • 什么是四大行业
  • 房地产企业什么时候停止预缴增值税
  • 销项负数发票怎么冲减成本
  • 权益性投资包括哪些
  • 如何处理记账凭证
  • SQLServer:探讨EXEC与sp_executesql的区别详解
  • 提高sql执行效率的方法
  • linux系统垃圾怎么清理
  • mac的itunes怎么下载歌曲
  • linux命令nano
  • Linux查看文件内容编码
  • win10系统收不到wifi信号
  • 附件中的应用程序
  • win7 win8.1双系统安装教程
  • Win8.1 32位和64位有什么区别 Win8.1 32位和64区别详细介绍
  • jquery window
  • nodejs入门教程
  • angularjs定义全局变量
  • dos命令大全及用法
  • shell脚本实例精讲
  • javascript()
  • Unity3D游戏开发标准教程
  • android面试题2019
  • 广东国税局发票查询系统
  • 甘肃国税电子税务局
  • 贵州电子税务总局
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设