位置: 编程技术 - 正文

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

  • 什么时候计提企业所得税
  • 原材料报废可以退税吗
  • 缴纳城镇土地使用税
  • 存货的期末余额在借方还是贷方
  • 电子税务局自然人如何注册
  • 待抵扣进项税什么时候抵扣
  • 汇算清缴产生的企业所得税如何做账
  • 材料已入库,发票账单未到的会计分录
  • 现金股利和股票股利的区别
  • 金税三期收费吗
  • 竞价服务费放在哪个会计科目?
  • 企业之间借款该怎么做账
  • 营改增后非增值税应税项目包括哪些
  • 餐饮企业卖套餐赠送单品要交增值税吗?
  • 收购不良资产收益怎么算
  • 小规模9万是含税吗
  • 小规模纳税人零申报什么意思
  • 应收账款坏账处理审计
  • 递延所得税怎么产生
  • 汽车加油票可以开专票吗
  • 存货盘亏损失
  • 香港公司的法人叫什么
  • 我国的社会保险费
  • 怎么做增值税
  • 评标专家评审费应该谁来付
  • 建设工程招标办
  • 小规模纳税人免增值税的账务处理
  • 携税宝费用可以抵扣增值税吗
  • 研发费用的支出类型有哪些
  • 传媒改革
  • 工程材料怎么开票
  • PHP:imagecolorset()的用法_GD库图像处理函数
  • 鲜艳的多彩的英文
  • 职工食堂外包账务怎么做
  • opencv开发项目
  • php自带的加密解密函数
  • 动静结合会计等式的不会重复算利润吗
  • js获取各种屏幕信息
  • php推送微信公众号客户消息
  • 资产负债率 70%
  • 财务制度备案信息是不是可以不填
  • 美容店销售收入预测表12个月表格
  • mysql中的外键的作用
  • php上传大视频
  • 核定征收企业所得税应税所得率
  • 税款所属期止
  • 报废产品属于什么会计科目
  • 厂房无偿使用交税吗
  • 小规模纳税人的增值税怎么算
  • 驾驶员培训费纳税吗
  • 个体户如何做账?
  • 成本法下被投资单位其他综合收益变动
  • 施工企业项目部管理人员对外行为的法律后果由谁承担
  • 生产性生物资产账务处理
  • 建筑业分包税收政策
  • 内部审计人员独立性
  • 内部资金管理实施细则
  • sqlserver游标的使用场景
  • sql server 获取今天的数据
  • mysql_info
  • 服务器上文件共享
  • 解决口苦最快的方法
  • win7系统怎么删除
  • 一打字就出现windows设置
  • win8免密码登录
  • Linux VPN 出现 807 错误的解决办法
  • python怎么编程
  • cocos2dx游戏开发
  • Python 正则表达式实现计算器功能
  • python引用方法
  • 用nodejs搭建服务器
  • 猫的喵喵
  • jQuery+formdata实现上传进度特效遇到的问题
  • javascript面向对象编程指南
  • 山东契税补贴政策2023年
  • 税控盘如何分配发票
  • 国家税务总局大连电子税务局
  • 太原市小店区电影院营业时间
  • 虚开增值税专用发票罪量刑标准2023
  • 深圳市国家税务局
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设