Skip to main content
 首页 » 编程设计

python之这些 Python 类方法输出的意义是什么

2024年11月24日12lyhabc

<分区>

假设我们有一个定义了三个方法的类:

class MyClass: 
 
    def instance_method(self): 
        return "Instance method called", self 
 
    @classmethod 
    def class_method(cls): 
        return "Class method called", cls 
 
    @staticmethod 
    def static_method(): 
        return "Static method called" 

我们实例化它并调用实例方法:

obj1 = MyClass() 
obj1.instance_method() 
# --> ('Instance method called', <__main__.MyClass at 0x106634588>) 

然后我们调用类方法:

obj1.class_method() 
# --> ('Class method called', __main__.MyClass) 

我无法理解为什么实例方法被“<>”括起来,以及为什么它包含指向内存中某个位置的指针,而类方法却没有。