用描述符实现classmethod方法和staticmethod方法
2024-10-21 04:56:48
1. @classmethod
class ClassMethod:
def __init__(self, func):
self.func = func def __get__(self, instance, owner):
"""
类来调用,instance为None,owner为类本身。
实例来调用,instance为实例,owner为类本身。
:param instance:
:param owner:
:return:
""" def feedback(*args, **kwargs):
return self.func(owner, *args, **kwargs) return feedback class People:
name = 'edward' @ClassMethod
def say_hi(cls, msg):
print(f'你好{cls.name},{msg}') People.say_hi('你好帅')
2. @staticmethod
class StaticMethod:
def __init__(self, func):
self.func = func def __get__(self, instance, owner):
def feedback(*args, **kwargs):
return self.func(*args, **kwargs) return feedback class People:
name = 'edward' @StaticMethod
def say_hi(x, y, z): # say_hi = StaticMethod(say_hi)
print('------>', x, y, z) People.say_hi(1, 2, 3)
最新文章
- Hibernate插入数据后获得ID
- flex Vector
- jmeter jdbc request使用详解
- Win7安装visual c++ 2015 redistributable x64失败
- scala 学习笔记(04) OOP(上)主从构造器/私有属性/伴生对象(单例静态类)/apply方法/嵌套类
- Win7系统中提示:本地无法启动MySQL服务,报的错误:1067,进程意外终止的解决方法。
- C#读取物理网卡信息及其对应的IP地址
- Pass value from child popup window to parent page window using JavaScript--reference
- 10个最有用的 IntelliJ IDEA 插件
- 微信团队分享:Kotlin渐被认可,Android版微信的技术尝鲜之旅
- Prepare paddle in Docker
- EditText获取焦点
- DVWA的安装
- 关于ARM NEON学习的一些资料
- RN(八)——react-native-image-viewer &; react-native-swiper
- Spring框架的事务管理的基本概念
- ubuntu下配置JDK7环境变量
- nginx的简单使用和使用nginx在windows上搭建tomcat集群
- SonarQube Scanner execution execution Error --- Failed to upload report - 500: An error has occurred. Please contact your administrator
- Puppet全面详解