位置: 编程技术 - 正文

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

  • 注册资本没有到位可以注销吗
  • 增值税专用发票有效期是多长时间
  • 职工报销的医药费计入什么科目
  • 取得交易性金融资产投资收益为什么在借方
  • 计提工会经费的贷方账户为
  • 股东借给公司的钱怎么归还
  • 保险公司营销人员个人先进事迹材料
  • 可供出售金融资产会计处理
  • 资产负债表要素包括几项
  • 新收入准则建筑企业按完工百分比
  • 公司代扣代缴个税完税凭证在哪里可以打印
  • 企业收回以前注销时的坏账准备余额怎么处理?
  • 车过户怎么处理
  • 按工人工资比例结转制造费用的科目
  • 在建工程完工验收报告
  • 服务类的企业发票税率
  • 过节政府有补助钱吗
  • 旅游景区税收标准
  • 工会是否具有有法律效力
  • 投标报价里税金和税率是一个吗
  • 分公司负债,总公司要负担
  • 以公司名义投资好还是以个人名义投资好
  • 固定资产报废处理流程图
  • 工会经费计税依据是应发还是实发
  • 高新创投企业所得税税率
  • 新公司没收入之前费用都走开办费吗
  • 隐藏资源管理器里的cd驱动器
  • 采购费用应计入材料采购成本
  • 跨行汇入会计分录
  • 多交增值税可以退吗
  • 取得土地所有权范围内的树如何处理
  • 接受捐赠收入要计入销售收入吗
  • 坏坏狼故事绘本
  • 苹果发布首款MR头显
  • 结转消费税的会计分录
  • win10开机强制进入安全模式重置
  • latex双栏图片
  • post请求怎么用
  • 短视频小程序源码
  • golang、python、php、c++、c、java、Nodejs性能对比
  • 申请减免费用的报告格式
  • 先申报工资还是先发工资
  • 企业确认坏账损失领导如何回复
  • 长期股权投资会计准则
  • 外地预交的企业所得税如何在季度企业所得税申报
  • 研发失败能做加工企业吗
  • 织梦开发教程
  • 公司购买基金入什么科目
  • sql server2005使用方法
  • 物业公司购买空调合法吗
  • 分公司注销怎么起诉公司
  • 印花税计入哪个会计分录
  • 本年利润的借方科目
  • 成本结转怎么结转
  • 企业为职工购买互助保障项目经费可以有什么开支
  • 税控盘和金税盘一样吗
  • 让渡是什么
  • 会计交接事项有哪些
  • mysql复制命令
  • sqlserver2005网络配置里没有东西
  • 微软6月24号
  • windowsserver2008r2版本区别
  • mac的command键相当于pc的什么键
  • 如何在xp系统中调整移动硬盘
  • win7系统显卡驱动怎么安装
  • jquery width
  • JQuery ZTree使用方法详解
  • 批处理 判断文件是否存在
  • js简单实现鼠标移动后面文字也移动
  • unity游戏换皮教程
  • android:padding="10dp"
  • django 接收前端的json
  • jqueryif判断
  • java learning
  • Android的事件处理机制是一种______机制
  • 苏州买房退契税政策2023
  • 房产税江苏2021年征收标准
  • 柬埔寨现在的领导
  • 残疾人个人所得税减免政策2023
  • 税务转为非正常户的后果
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设