类的静态方法和类方法

一:调用区别

class Demo(object):

    @staticmethod
    def static_method():
        return "static_method"
    @classmethod
    def instance_method(self):
        return "instance_method"

    def normal_method(self):
        return "normal_method"



d = Demo()
static_result = Demo.static_method()
print(d.static_method()) # 实例调用静态方法
print(static_result) # 类名调用静态方法
instance_result = d.instance_method()
print(instance_result) # 实例调用类方法
print(Demo.instance_method()) # 类名调用实例方法
normal_result = d.normal_method()
print(normal_result) # 实例调用普通方法 
print(Demo.normal_method()) # 类名调用普通方法,出错,TypeError: normal_method() missing 1 required positional argument: 'self'



# 结果
static_method
static_method
Traceback (most recent call last):
instance_method
instance_method
normal_method
  File "F:/pycharm测试功能文件夹/python知识点/类的静态方法和实例方法.py", line 24, in <module>
    print(Demo.normal_method())
TypeError: normal_method() missing 1 required positional argument: 'self'

类中的普通方法,必须创建实例后进行调用,不能通过类名直接调用

类中的静态方法和类方法,可以通过类名调用,也可以通过实例进行调用

二:获取属性的区别

静态方法:静态方法中不能使用实例属性和类属性

class Demo(object):
    class_attribute = 1

    def __init__(self):
        self.normal_attribute= 2

    @staticmethod
    def static_method(self):
         print("静态方法获取类属性",self.class_attribute)
         print("静态方法获取实例属性",self.normal_attribute)
    # @classmethod
    # def instance_method(self):
    #     print("类方法获取类属性", self.class_attribute)
    #     print("类方法获取实例属性", self.normal_attribute)
    #
    # def normal_method(self):
    #     print("实例方法获取类属性", self.class_attribute)
    #     print("实例方法获取实例属性", self.normal_attribute)



d = Demo()

Demo.static_method()
d.static_method()

# Demo.instance_method()
# d.instance_method()
#
# d.normal_method()
# Demo.normal_method()

# 结果
Traceback (most recent call last):
  File "F:/pycharm测试功能文件夹/python知识点/类的静态方法和实例方法.py", line 24, in <module>
    Demo.static_method()
TypeError: static_method() missing 1 required positional argument: 'self'

类方法:只能使用类属性,不能使用实例属性

class Demo(object):
    class_attribute = 1

    def __init__(self):
        self.normal_attribute= 2

    # @staticmethod
    # def static_method(self):
    #      print("静态方法获取类属性",self.class_attribute)
    #      print("静态方法获取实例属性",self.normal_attribute)
    @classmethod
    def instance_method(self):
        print("类方法获取类属性", self.class_attribute)
        # AttributeError: type object 'Demo' has no attribute 'normal_attribute'
        # print("类方法获取实例属性", self.normal_attribute)  
    #
    # def normal_method(self):
    #     print("实例方法获取类属性", self.class_attribute)
    #     print("实例方法获取实例属性", self.normal_attribute)



d = Demo()

# Demo.static_method()
# d.static_method()

Demo.instance_method()
d.instance_method()
#
# d.normal_method()
# Demo.normal_method()


# 结果
类方法获取类属性 1
类方法获取类属性 1

实例方法:既可以使用实例属性,也可以使用类属性

class Demo(object):
    class_attribute = 1

    def __init__(self):
        self.normal_attribute= 2

    # @staticmethod
    # def static_method(self):
    #      print("静态方法获取类属性",self.class_attribute)
    #      print("静态方法获取实例属性",self.normal_attribute)
    # @classmethod
    # def instance_method(self):
    #     print("类方法获取类属性", self.class_attribute)
        # AttributeError: type object 'Demo' has no attribute 'normal_attribute'
        # print("类方法获取实例属性", self.normal_attribute)
    #
    def normal_method(self):
        print("实例方法获取类属性", self.class_attribute)
        print("实例方法获取实例属性", self.normal_attribute)



d = Demo()

# Demo.static_method()
# d.static_method()

# Demo.instance_method()
# d.instance_method()
#
d.normal_method()
# Demo.normal_method()


# 结果
实例方法获取类属性 1
实例方法获取实例属性 2

三:获取方法的区别

静态方法:方法内部既不能调用实例方法也不能调用类方法

class Demo(object):
    class_attribute = 1

    def __init__(self):
        self.normal_attribute= 2

    @staticmethod
    def static_method(self):
        print(self.instance_method())
        print(self.normal_method())
        return "static_method"
    @classmethod
    def instance_method(self):
        return "instance_method"

    def normal_method(self):
        return "normal_method"



d = Demo()

Demo.static_method()
d.static_method()

# Demo.instance_method()
# d.instance_method()
#
# d.normal_method()
# Demo.normal_method()


# 结果
Traceback (most recent call last):
  File "F:/pycharm测试功能文件夹/python知识点/类的静态方法和实例方法.py", line 23, in <module>
    Demo.static_method()
TypeError: static_method() missing 1 required positional argument: 'self'

类方法:内部可以调用实例方法(需要参数self),也可以调用静态方法(需要参数self)

class Demo(object):
    class_attribute = 1

    def __init__(self):
        self.normal_attribute= 2

    @staticmethod
    def static_method(self):
        return "static_method"
    @classmethod
    def instance_method(self):
        print(self.static_method(self))
        print(self.normal_method(self))
        return "instance_method"

    def normal_method(self):
        return "normal_method"



d = Demo()

# Demo.static_method()
# d.static_method()

Demo.instance_method()
d.instance_method()
#
# d.normal_method()
#  Demo.normal_method()

# 结果
static_method
normal_method
static_method
normal_method

实例方法:内部可以调用类方法和静态方法(需要传递self)

class Demo(object):
    class_attribute = 1

    def __init__(self):
        self.normal_attribute= 2

    @staticmethod
    def static_method(self):
        return "static_method"
    @classmethod
    def instance_method(self):
        return "instance_method"

    def normal_method(self):
        print(self.static_method(self))
        print(self.instance_method())
        return "normal_method"



d = Demo()

# Demo.static_method()
# d.static_method()

# Demo.instance_method()
# d.instance_method()
#
d.normal_method()
# Demo.normal_method()

# 结果
static_method
instance_method

#TODO

原文地址:https://www.cnblogs.com/meloncodezhang/p/12626970.html