类方法:有个默认参数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. [DevExpress使用随笔]之NavBarControl控件(一)【转】

    https://blog.csdn.net/HXC_HUANG/article/details/78614092 NavBarControl是具有可扩展组的侧导航控件.一.在Winform窗口中拖入N ...

  2. 【Spring】bean动态注册到spring

    /* * http://412887952-qq-com.iteye.com/blog/2348445 * http://www.jb51.net/article/106558.htm * https ...

  3. 终于考完PMP

    快到年底,上手了GO语言,搞定了广告后台,觉得年底前应该不忙; 我们的PM也报了PMP,可以一起报团学习,后来由于地理位置关系,我们项目组,三个人报了不同的班,也好,信息可以共享; 跟我们公司合作的培 ...

  4. wifipineapple外接网卡上网

    买了一台wifipineapple, pineapple有两种版本, 第一种是3G版本,可以外接3G上网卡, 还有一种是wifi版本, 包含一个物理的网络插槽, 我买的是第二种 wifipineapp ...

  5. 分布式架构探索 - 2. WebService RPC框架之Apache CXF

    Apache CXF是一个开源的WebService RPC框架. 例子: 1. 新建一个maven web项目, 添加pom 如下: <?xml version="1.0" ...

  6. cn_windows_10_enterprise_version_1703_updated_june_2017_x64_dvd_10720588.iso

    ed2k://|file|cn_windows_10_enterprise_version_1703_updated_june_2017_x64_dvd_10720588.iso|4959832064 ...

  7. maven仓库中心mirrors配置多个下载中心(执行最快的镜像)

    E:\Program FilesApache Software Foundationapache-maven-3.5.4-binconf\settings.xmlmaven仓库中心mirrors配置多 ...

  8. coon's patch

    作者:桂. 时间:2018-05-23  06:11:54 链接:https://www.cnblogs.com/xingshansi/p/9070761.html 前言 早晨突然想到计算机模型的各种 ...

  9. understanding-raid-setup-in-linux/

    https://www.tecmint.com/understanding-raid-setup-in-linux/ Part 1:Introduction to RAID, Concepts of ...

  10. CNN卷积核反传分析

    CNN(卷积神经网络)的误差反传(error back propagation)中有一个非常关键的的步骤就是将某个卷积(Convolve)层的误差传到前一层的池化(Pool)层上,因为在CNN中是2D ...