python staticmethod,classmethod,普通函数的区别

本文深入探讨了Python中静态方法和类方法的区别与应用。通过具体示例解释了如何使用@staticmethod和@classmethod装饰器来定义这两种类型的方法,并展示了它们如何与实例方法交互,以及如何在类中修改和获取变量。
class Test01:

    arg1 = 'xx'

    @staticmethod
    def static_fun_self(self):  # self只是变量
        print('static fun self:', self)

    @staticmethod
    def static_fun_cls(cls):   # cls只是变量
        print('static fun cls:', cls)

    @classmethod
    def set_cls_arg1(cls, arg1):
        cls.arg1 = arg1   # 设置类的值

    @classmethod
    def class_fun(cls):
        print('class fun' + cls.arg1)

    def fun(self):
        print('fun' + self.arg1)

    def set_arg1(self, arg1):
        self.arg1 = arg1  # 设置实例的值


if __name__ == '__main__':
    t1 = Test01()
    t1.set_arg1('ss')
    t1.set_cls_arg1('sx')
    t1.static_fun_cls('cls')
    t1.class_fun()
    t1.fun()

 

--------------------------

输出结果1.

t1 = Test01()
t1.set_arg1('ss')
t1.set_cls_arg1('sx')
t1.static_fun_cls('cls')
t1.class_fun()
t1.fun()

----

static fun cls: cls
class funsx
funss

设置cls self的值不同获取的值也不一样

输出结果2.

t1 = Test01()
t1.set_arg1('ss')
# t1.set_cls_arg1('sx')
t1.static_fun_cls('cls')
t1.class_fun()
t1.fun()

----

static fun cls: cls
class funxx
funss

设置self的值 cls获取不到

 

输出结果3.

t1 = Test01()
# t1.set_arg1('ss')
t1.set_cls_arg1('sx')
t1.static_fun_cls('cls')
t1.class_fun()
t1.fun()

----

static fun cls: cls
class funsx
funsx

设置 cls的值 self, cls都可以获取到

输出结果4.

t1 = Test01()
# t1.set_arg1('ss')
# t1.set_cls_arg1('sx')
t1.static_fun_cls('cls')
t1.class_fun()
t1.fun()

static fun cls: cls
class funxx
funxx

---------------------------

通过@staticmethod修饰的函数在类中获取不到self,cls, 相当于一个普通函数

通过@classmethod修饰的函数可以通过cls.变量名 改变类中变量的值其他函数的值也更改

instance函数可以通过self.变量名 改变调用者变量的值,其他函数的值不更改

Python中,`@staticmethod`和普通函数的主要区别如下: 1. **绑定方式**: - 普通函数:定义在类中时,会自动绑定到类的实例(通过`self`参数),或者绑定到类本身(通过`cls`参数,使用`@classmethod`)。 - `@staticmethod`:不绑定任何实例或类,它只是一个普通函数,但被放在类的命名空间中。 2. **调用方式**: - 普通函数:必须通过实例(如`obj.method()`)或类(如`Class.method()`,如果是`@classmethod`)调用。 - `@staticmethod`:可以通过实例或类直接调用(如`obj.static_method()`或`Class.static_method()`),因为它不需要任何绑定。 3. **参数传递**: - 普通函数:通常需要`self`(实例方法)或`cls`(类方法)作为第一个参数。 - `@staticmethod`:不需要任何特殊参数,和普通函数一样。 4. **用途**: - 普通函数:用于操作实例或类的状态。 - `@staticmethod`:用于定义与类相关但不依赖实例或类状态的函数,通常用于工具函数。 ### 示例代码 ```python class MyClass: def instance_method(self): print("这是一个实例方法") @classmethod def class_method(cls): print("这是一个类方法") @staticmethod def static_method(): print("这是一个静态方法") # 调用实例方法 obj = MyClass() obj.instance_method() # 输出: 这是一个实例方法 # 调用类方法 MyClass.class_method() # 输出: 这是一个类方法 # 调用静态方法 MyClass.static_method() # 输出: 这是一个静态方法 obj.static_method() # 输出: 这是一个静态方法 ``` ### 总结 - `@staticmethod`是一个装饰器,用于定义一个与类相关但不依赖实例或类状态的函数。 - 普通函数在类中通常是实例方法或类方法,需要`self`或`cls`参数。 - `@staticmethod`可以通过类或实例直接调用,而不需要任何绑定。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值