静态方法(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): #圆的 ...
随机推荐
- C# SemaphoreSlim 实现
当多个任务或线程并行运行时,难以避免的对某些有限的资源进行并发的访问.可以考虑使用信号量来进行这方面的控制(System.Threading.Semaphore)是表示一个Windows内核的信号量对 ...
- 提升SQLite数据插入效率低、速度慢的方法(转)
前言 SQLite数据库由于其简单.灵活.轻量.开源,已经被越来越多的被应用到中小型应用中.甚至有人说,SQLite完全可以用来取代C语言中的文件读写操作.因此我最近编写有关遥感数据处理的程序的时候, ...
- Xposed 框架 hook 简介 原理 案例 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- svn文件夹解锁批处理
清除svn文件的bat脚本整理 从svn上检出的项目,不在myeclipse工具中脱离svn的管辖,怎么办呢,下面有我的方法,也是借鉴别人的,用了特别好使,故推荐给大家. 首先创建一个xxx.bat文 ...
- jquery监测文本框变化
$("#mobile").on('keyup paste blur', function () { var mobile = $(this).val(); ...
- grid - 网格项目对齐方式(Box Alignment)
CSS的Box Alignment Module补充了网格项目沿着网格行或列轴对齐方式. <view class="grid"> <view class='ite ...
- linux驱动工程面试必问知识点
linux内核原理面试必问(由易到难) 简单型 1:linux中内核空间及用户空间的区别?用户空间与内核通信方式有哪些? 2:linux中内存划分及如何使用?虚拟地址及物理地址的概念及彼此之间的转化, ...
- Mathematica 11.1.0 下载及注册流程
新版本注册机: http://files.cnblogs.com/files/dabaopku/Mathematica_11.1.0_Keygen.exe.zip 类似于11.0, 在控制台运行, 比 ...
- 前端项目添加自定义icont图标步骤
文章转自https://blog.csdn.net/weixin_36185028/article/details/53416185 这里就用到了两个文件,一个是icontfont.css,另外一个是 ...
- SQL Server does not purge row versioning records even the transaction are committed if there are other open transaction running in the databases with read-committed snapshot enabled .
This is a by-design behavior. There is only one allocation unit in tempdb that istracking the versio ...