类方法:有个默认参数cls,并且可以直接用类名去调用,可以与类属性交互(也就是可以使用类属性)

静态方法:让类里的方法直接被类调用,就像正常调用函数一样

类方法和静态方法的相同点:都可以直接被类调用,不需要实例化

类方法和静态方法的不同点:

  类方法必须有一个cls参数表示这个类,可以使用类属性

  静态方法不需要参数

绑定方法:分为普通方法和类方法

     普通方法:默认有一个self对象传进来,并且只能被对象调用-------绑定到对象

      类方法:默认有一个cls对象传进来,并且可以被类和对象(不推荐)调用-----绑定到类

非绑定方法:静态方法:没有设置默认参数,并且可以被类和对象(不推荐)调用-----非绑定

#!/usr/bin/env python
# -*- coding:utf-8 -*-
with open('student','w',encoding='utf-8') as f:
a = 'abcd,4567'''
f.write(a)
class Student:
f = open('student', encoding='utf-8')
def __init__(self):
pass
@classmethod #类方法 :有个默认参数cls,并且可以直接使用类名去
#调用,还可以与类属性交互(也就是可以使用类属性)
def show_student_info_class(cls):
# f = open('student', encoding='utf-8')
for line in cls.f:
name,sex = line.strip().split(',')
print(name,sex)
@staticmethod #静态方法:可以直接使用类名去调用,就像正常的函数调用一样
def show_student_info_static(): #不用传self
f = open('student',encoding='utf-8')
for line in f:
name,sex = line.strip().split(',')
print(name,sex)
# egon = Student()
# egon.show_student_info_static() #也可以这样调,但是还是推荐用类名去调
# egon.show_student_info_class() Student.show_student_info_class()#类名.方法名()
print('*******************')
Student.show_student_info_static()#类名.方法名() # staticmethod和classmethod

静态方法(staticmethod)和类方法(classmethod)的更多相关文章

  1. 静态方法staticmethod和类方法classmethod

    静态方法staticmethod和类方法classmethod 一.类方法classmethod 把一个方法变成一个类中的方法,这个方法可以直接利用类来调用,不需要依托任何的对象,即不需要实例化也可以 ...

  2. Python - 静态方法@staticmethod和类方法classmethod

    传送门 https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/Day09/%E9%9D%A2%E5%90%91%E5%AF ...

  3. python-静态方法staticmethod、类方法classmethod、属性方法property

    Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 def  ...

  4. python中静态方法(@staticmethod)和类方法(@classmethod)的区别

    一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某些应 ...

  5. 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别

    [面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...

  6. Python的3个方法:静态方法(staticmethod),类方法(classmethod)和实例方法

    Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法,如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  7. python类方法@classmethod与@staticmethod

    目录 python类方法@classmethod与@staticmethod 一.@classmethod 介绍 语法 举例 二.@staticmethod 介绍 语法 举例 python类方法@cl ...

  8. 类的静态方法@staticmethod

    静态方法 @staticmethod 静态方法是定义在类内部的函数,此函数的作用域是类的内部 说明: 静态方法需要使用 @staticmethod装饰器定义 静态方法与普通函数定义相同,不需要传入se ...

  9. python静态属性@property、类方法@classmethod、静态方法@staticmethod和普通方法

    静态属性:即将类的函数通过@property属性封装,封装后实例调用该函数时,不再需要在函数后面加(),而是用类似调用数据属性的方式直接调用函数名称即可执行函数. 静态属性既可以访问类的属性,也可以访 ...

  10. 特性(property)/静态方法(staticmethod)/类方法(classmethod)/__str__的用法

    property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 1 import math 2 class Circle: 3 def __init__(self,radius): #圆的 ...

随机推荐

  1. XCode各种问题

    2018.07.10 1.clang: warning: libstdc++ is deprecated; move to libc++ [-Wdeprecated] 2.2018.07.29  海康 ...

  2. 通过groovy表达式拓展oval——实现根据同一实体中的其他属性值对某个字段进行校验

    在java的参数校验中,开源验证框架OVAL基本能够满足所有需求,如下面通过简单的添加注解,就可实现对参数的非空和长度校验. @NotNull(message="计息周期月数不能为空&quo ...

  3. ios 容错处理JKDataHelper和AvoidCrash

    一.JKDataHelper 在大团队协同开发过程中,由于每个团队成员的水平不一,很难控制代码的质量,保证代码的健壮性,经常会发生由于后台返回异常数据造成app崩溃闪退的情况,为了避免这样情况使用JK ...

  4. Oracle数据库远程连接配置教程

    本人前一段时间做过Oracle数据库的相关工作.可是发现数据库的监听程序和服务名比較难搞定,并且网上也没有现成的教程.所以经过自己的探索之后将这片文章贡献给大家,如有不当之处还请谅解并请联系本人. 此 ...

  5. java.lang.IllegalStateException: Cannot run without an instance id.

    启动springboot,报错:quartz集群报错: Sep 09, 2016 5:33:47 AM org.apache.catalina.core.ApplicationContext log ...

  6. Mathematica查看内部定义

    << GeneralUtilities`; PrintDefinitions[IntegerReverse]

  7. MySQL字符集不一致的解决办法总结

    用SHOW CREATE TABLE table_name;可以看出具体的字符集设置. 错误代码: Illegal mix of collations (utf8mb4_unicode_ci,IMPL ...

  8. php curl post josn + header

    <meta charset="utf8"> <?php ini_set("max_execution_time",1800000); $arr ...

  9. replay的意义

    数据库重放: () 在测试环境中重新创建实际的生产数据库工作量. () 在生产中实施更改之前,确定和分析潜在的不稳定性. () 捕获生产中的工作量:

  10. 【2019年04月10日】股票的滚动市盈率PE最低排名

    仅根据最新的市盈率计算公式进行排名,无法对未来的业绩做出预测. 新钢股份(SH600782) - 滚动市盈率PE:3.87 - 滚动市净率PB:1.29 - 滚动年化股息收益率:1.31% - 钢铁 ...