静态方法(staticmethod)和类方法(classmethod)
类方法:有个默认参数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)的更多相关文章
- 静态方法staticmethod和类方法classmethod
静态方法staticmethod和类方法classmethod 一.类方法classmethod 把一个方法变成一个类中的方法,这个方法可以直接利用类来调用,不需要依托任何的对象,即不需要实例化也可以 ...
- Python - 静态方法@staticmethod和类方法classmethod
传送门 https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/Day09/%E9%9D%A2%E5%90%91%E5%AF ...
- 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 ...
- python中静态方法(@staticmethod)和类方法(@classmethod)的区别
一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某些应 ...
- 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别
[面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...
- Python的3个方法:静态方法(staticmethod),类方法(classmethod)和实例方法
Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法,如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- python类方法@classmethod与@staticmethod
目录 python类方法@classmethod与@staticmethod 一.@classmethod 介绍 语法 举例 二.@staticmethod 介绍 语法 举例 python类方法@cl ...
- 类的静态方法@staticmethod
静态方法 @staticmethod 静态方法是定义在类内部的函数,此函数的作用域是类的内部 说明: 静态方法需要使用 @staticmethod装饰器定义 静态方法与普通函数定义相同,不需要传入se ...
- python静态属性@property、类方法@classmethod、静态方法@staticmethod和普通方法
静态属性:即将类的函数通过@property属性封装,封装后实例调用该函数时,不再需要在函数后面加(),而是用类似调用数据属性的方式直接调用函数名称即可执行函数. 静态属性既可以访问类的属性,也可以访 ...
- 特性(property)/静态方法(staticmethod)/类方法(classmethod)/__str__的用法
property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 1 import math 2 class Circle: 3 def __init__(self,radius): #圆的 ...
随机推荐
- APB协议
https://wenku.baidu.com/view/2663f629ef06eff9aef8941ea76e58fafab04592.html https://www.cnblogs.com/l ...
- Android开发中常见的设计模式 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Scikit-learn 概述
https://www.leiphone.com/news/201701/ZJMTak4Y8ch3Nwd0.html
- Android设置屏幕旋转后保存数据
1.onCreate()方法中最后判断需要保存的状态值 if(savedInstanceState != null){ mCurrentIndex = savedInstanceState.getIn ...
- 阿里的Json解析包FastJson使用
阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征: 速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser ...
- InfluxDB源码阅读之httpd服务
操作系统 : CentOS7.3.1611_x64 go语言版本:1.8.3 linux/amd64 InfluxDB版本:1.1.0 服务模块介绍 源码路径: github.com/influxda ...
- oracle 在已有表新增列内批量加数据
创建每列随机值的语句 create table TEST_ZHAA01A_03 as select rownum as id, to_char(sysdate + rownum/24/3600, 'y ...
- [转]tensorflow中的gather
原文链接 tensorflow中取下标的函数包括:tf.gather , tf.gather_nd 和 tf.batch_gather. 1.tf.gather(params,indices,vali ...
- python3 --- locale命名空间让程序更加安全了
[简介] 由于python-2.x 并没有locale这个层次的命名空间,所以临时变量有可能会泄漏,进而影响到了包涵它的命名空间 [看一下pyhont-2.x是怎么泄漏临时变量的] python Py ...
- CountDownLatch、CyclicBarrier和Semaphore 使用示例及原理
备注:博客园的markDown格式支持的特别不友好.也欢迎查看我的csdn的此篇文章链接:CountDownLatch.CyclicBarrier和Semaphore 使用示例及原理 CountDow ...