惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Vercel News
Vercel News
月光博客
月光博客
S
SegmentFault 最新的问题
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
U
Unit 42
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
小众软件
小众软件
WordPress大学
WordPress大学
G
Google Developers Blog
Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
博客园 - 【当耐特】
I
InfoQ

老董笔记

尚硅谷机构在哪?尚硅谷培训怎么样?靠谱吗-互联网IT百科 韩顺平介绍,传智讲师,开办泰牛,入尚硅谷等一系列-互联网IT百科 pandas多重索引标准样式(写入excel有空行)-互联网IT百科 cannot join with no overlapping index names-互联网IT百科 pandas多列变多行(即宽表变长表)melt和stack函数-互联网IT百科 pandas多行转多列(长表变宽表)pivot和unstack-互联网IT百科 Index contains duplicate entries, cannot reshape完美解决-互联网IT百科 single positional indexer is out-of-bounds-互联网IT百科 Can only compare identically-labeled Series objects-互联网IT百科 pandas transform用法详解(多个案例)-互联网IT百科 python四舍五入精确实现-互联网IT百科 pandas的groupby使用apply分组排序-互联网IT百科 index 0 is out of bounds for axis 0 with size 0-互联网IT百科 pandas分组过滤filter函数-互联网IT百科 联想Win10系统如何禁用触摸屏关闭触摸-互联网IT百科 groupby分组计算transform转换返回相同长度序列-互联网IT百科 brooks seo教程python教程,brooks seo教程网盘,布鲁seo资源-互联网IT百科 电脑右键文件夹一直转圈电卡死怎么回事-互联网IT百科 施琪嘉的心理成长课(荐)-互联网IT百科 百度SEO公司_SEO推广公司哪家好_SEO外包服务如何选-老董笔记 groupby后agg同1列用多个聚合函数、不同列用不同函数、自定义函数-互联网IT百科 pandas的groupby单列多列分组聚合运算-互联网IT百科 DataFrameGroupBy对象及分组个数、分组大小、组名索引、组数据详解-互联网IT百科 pandas中groupby之Grouper and axis must be same length-互联网IT百科 pandas中groupby的分组原理-互联网IT百科 pandas的groupby的使用详解大全-互联网IT百科 openpyxl单元格自动换行强制换行Alignment(wrapText=True)-互联网IT百科 python教程全套(可就业)-互联网IT百科 联想win10系统CPU显示100%,电脑呼呼响怎么回事-互联网IT百科 如何自制CPU,CPU原理是怎么样的?-互联网IT百科
静态方法@staticmethod概念及使用场景-互联网IT百科
2021-01-17 · via 老董笔记

  1 静态方法使用装饰器@staticmethod。

  2 静态方法是定义在类中的函数,但是没有self、cls这样的特殊参数,在静态方法中一般不涉对属性和方法的操作。

  3 静态方法中的操作完全可以在类外用普通函数实现,但是为了便于维护有时候会把一些功能代码写在类中,让代码显得有“逻辑性和归属性”。

  4 静态方法一般用类名调用,但是用对象进行调用并不会报错。

# -*- coding:utf-8 -*-


class Person:
    def __init__(self,name):
        self.name = name

    def PaiYunZhang(self,num):
        print(f'排云掌,用{num}成功力')

    def FengShenTui(self,num):
        print(f'风神腿,踢出{num}米长')

    @staticmethod
    def say():
        print('是时候表演真正的武功了')


if __name__ == "__main__":
    Person.say()
    p = Person('老王')
    p.PaiYunZhang(6)
    p.FengShenTui(3)
是时候表演真正的武功了
排云掌,用6成功力
风神腿,踢出3米长

  上述Person类中的say()方法可以直接写在类外,但是把一些相关操作“束缚”在类体内,这样会更便于维护。

  每个人的思路不同,同样的东西,实现的逻辑过程不同,如下代码的写作思路可以看下。

# -*- coding:utf-8 -*-


class Person:
    def __init__(self,name,max_num):
        self.name = name
        self.max_num = max_num

    def PaiYunZhang(self):
        Person.say(self.max_num)
        print(f'先打一套排云掌')

    @staticmethod
    def say(max_num):
        print(f'切记表演点到为止,最多用{max_num}成功力')


if __name__ == "__main__":
    p = Person('老王',3)
    p.PaiYunZhang()
切记表演点到为止,最多用3成功力
先打一套排云掌

很赞哦!

python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会