四时宝库

程序员的知识宝库

python中的类属性 vs 实例属性(python的类的属性)

类属性和实例属性

实例属性是由某个特定类的实例所独有的。也就是说,两个不同的实例中,它们的实例属性通常是不同的。我们在前面的章节中已经对实例属性进行了详细的讨论,所以你应该对它们已经有了深入的理解。

除了实例属性,我们还可以在类中定义类属性,这些属性是类本身所拥有的,并且被该类的所有实例共享。因此,对于该类的每个实例而言,类属性的值是相同的。类属性通常定义在所有方法之外,并位于类定义的顶部,紧跟在类的声明之后。

在下面的Python代码示例中,我们定义了一个名为"a"的类属性。你可以看到,无论是通过实例"x"还是"y",或者直接通过类名访问,这个属性的值都是一样的:

class MyClass:
    a = 10

x = MyClass()
y = MyClass()

print(x.a)  # 输出:10
print(y.a)  # 输出:10
print(MyClass.a)  # 输出:10

需要注意的是,如果你想要修改类属性的值,应该使用 ClassName.AttributeName 的方式进行。否则,你将只是在实例中创建了一个新的属性,而不会影响类属性。下面的代码示例展示了这个情况:

class MyClass:
    a = 10

x = MyClass()
y = MyClass()

x.a = 20

print(x.a)  # 输出:20
print(y.a)  # 输出:10
print(MyClass.a)  # 输出:10

在Python中,类属性和实例属性分别存储在两个不同的字典中。通过打印类和实例的__dict__属性,我们可以查看这些属性字典

class MyClass:
    a = 10

x = MyClass()
y = MyClass()

print(x.__dict__)  # 输出:{}
print(y.__dict__)  # 输出:{}

print(MyClass.__dict__)
# 输出:{'__module__': '__main__', 'a': 10, '__dict__': <attribute '__dict__' of 'MyClass' objects>, 
'__weakref__': <attribute '__weakref__' of 'MyClass' objects>, '__doc__': None}

实例对象的__dict__打印为空字典,因为它没有自己的实例属性。而类对象的__dict__?包含了类属性和其他重要信息。

使用类属性的示例:

1942年,艾萨克·阿西莫夫设计并引入了所谓的“机器人三大定律”。这些定律出现在他的故事《Runaround》中,并被许多科幻作家采用。随着我们开始在 Python 中制造机器人,是时候确保它们遵守阿西莫夫的三大定律了。由于这些定律对于每个实例(即机器人)都是相同的,我们将创建一个类属性 Three_Laws?。该属性是一个包含三大定律的元组。

class Robot:
    Three_Laws = (
        "A robot may not injure a human being or, through inaction, allow a human being to come to harm.",
        "A robot must obey the orders given it by human beings, except where such orders would conflict with the First Law.",
        "A robot must protect its own existence as long as such protection does not conflict with the First or Second Laws."
    )

# 创建机器人实例
robot1 = Robot()
robot2 = Robot()

# 访问类属性
print(Robot.Three_Laws)
# 输出:
# ('A robot may not injure a human being or, through inaction, allow a human being to come to harm.',
#  'A robot must obey the orders given it by human beings, except where such orders would conflict with the First Law.',
#  'A robot must protect its own existence as long as such protection does not conflict with the First or Second Laws.')

# 类属性可以通过实例访问
print(robot1.Three_Laws)
print(robot2.Three_Laws)
# 输出与上述相同的内容

在上述示例中,Three_Laws? 是一个类属性,它存储了机器人的三大定律。该属性在所有实例之间共享,可以通过类或实例访问。

正如之前提到的,我们可以通过实例或类名访问类属性。在下面的示例中,你可以看到我们不需要一个实例:

class Robot:
    Three_Laws = (
        "A robot may not injure a human being or, through inaction, allow a human being to come to harm.",
        "A robot must obey the orders given it by human beings, except where such orders would conflict with the First Law.",
        "A robot must protect its own existence as long as such protection does not conflict with the First or Second Laws."
    )

# 通过类名直接访问类属性
print(Robot.Three_Laws)
# 输出与上述相同的内容

# 不需要实例,直接通过类名访问
print(Robot.Three_Laws[0])
# 输出:A robot may not injure a human being or, through inaction, allow a human being to come to harm.

在上述示例中,我们展示了通过类名直接访问类属性的方式。无需创建实例,即可通过类名访问类属性,并使用索引方式获取其中的元素。

在下面的示例中,我们演示了如何使用类属性来计算实例的数量。我们需要做的是:

  1. 创建一个类属性,在我们的示例中我们称之为 counter?。
  2. 每次创建一个新实例时,将该属性增加1。
  3. 每次销毁一个实例时,将该属性减少1。
class C: 
    counter = 0
    def __init__(self): 
        type(self).counter += 1
    def __del__(self):
        type(self).counter -= 1
if __name__ == "__main__":
    x = C()
    print("Number of instances: : " + str(C.counter))
    y = C()
    print("Number of instances: : " + str(C.counter))
    del x
    print("Number of instances: : " + str(C.counter))
    del y
    print("Number of instances: : " + str(C.counter))

输出:

Number of instances: : 1
Number of instances: : 2
Number of instances: : 1
Number of instances: : 0

理论上,我们可以将 type(self).counter? 改写为 C.counter?,因为 type(self)? 最终会被评估为 "C"。然而,如果我们将这样的类用作超类,那么使用 type(self)? 将更有意义,稍后我们将理解这个问题。

使用 type(self)? 而不是类名直接引用类属性有以下好处:

  1. 继承的灵活性:通过使用 type(self)?,我们可以保证子类在继承父类时能够正确地访问和更新继承的类属性。如果我们在子类中直接使用父类名字来引用类属性,当子类重新定义了该类属性时,可能会导致意外的行为。
  2. 代码的可读性和可维护性:使用 type(self)? 更加明确地表达了我们想要操作当前实例所属的类的类属性。这使得代码更易于理解和维护,尤其在复杂的类继承关系中。

综上所述,虽然 C.counter? 在特定情况下可能有效,但使用 type(self).counter? 更具一般性和可扩展性,在处理类继承关系时更为推荐。

静态方法

在前面的部分中,我们将类属性用作公共属性。当然,我们也可以将公共属性变为私有属性。我们可以通过再次添加双下划线来实现这一点。如果这样做了,我们需要一种访问和修改这些私有类属性的方法。我们可以使用实例方法来实现这个目的:

class Robot:
    __counter = 0
    def __init__(self):
        type(self).__counter += 1
    def RobotInstances(self):
        return Robot.__counter
if __name__ == "__main__":
    x = Robot()
    print(x.RobotInstances())
    y = Robot()
    print(x.RobotInstances())

输出:

1
2

但是实例方法需要一个对实例的引用,因此如果我们尝试使用类名 Robot.RobotInstances()? 调用该方法,会收到错误消息,因为它需要一个实例作为参数。

Robot.RobotInstances()

输出:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-35-f53600e3296e> in <module>
----> 1Robot.RobotInstances()
TypeError: RobotInstances() missing 1 required positional argument: 'self'

下一个想法是将实例方法转换为静态方法,这样就不需要对实例进行引用。我们可以在方法头前面直接加上 @staticmethod? 装饰器。这是装饰器语法。

class Robot:
    __counter = 0
    def __init__(self):
        type(self).__counter += 1
    @staticmethod
    def RobotInstances():
        return Robot.__counter

if __name__ == "__main__":
    print(Robot.RobotInstances())
    x = Robot()
    print(x.RobotInstances())
    y = Robot()
    print(x.RobotInstances())
    print(Robot.RobotInstances())

输出:

0
1
2
2

现在我们可以按照自己的方式使用方法 RobotInstances?,并且它可以通过类名或实例名调用。

静态方法不应与类方法混淆。类方法和静态方法一样不绑定实例,但与静态方法不同的是,类方法绑定到一个类上。类方法的第一个参数是对类的引用,即类对象。它们可以通过实例或类名调用。

class Robot:
    __counter = 0
    def __init__(self):
        type(self).__counter += 1
    @classmethod
    def RobotInstances(cls):
        return cls, Robot.__counter

if __name__ == "__main__":
    print(Robot.RobotInstances())
    x = Robot()
    print(x.RobotInstances())
    y = Robot()
    print(x.RobotInstances())
    print(Robot.RobotInstances())

输出:

(<class '__main__.Robot'>, 0)
(<class '__main__.Robot'>, 1)
(<class '__main__.Robot'>, 2)
(<class '__main__.Robot'>, 2)

以下是类方法的使用案例:

  1. 在所谓的工厂方法的定义中使用。
  2. 经常在我们需要调用其他静态方法的静态方法中使用。如果我们必须使用静态方法,那么需要硬编码类名,这在存在继承类的情况下会成为问题。

下面的程序包含一个分数类,但尚未完整。在使用分数时,您需要能够约分分数,例如,分数 8/24 可以约分为 1/3。我们可以通过将分子和分母都除以最大公约数(GCD)来将分数约分为最简形式。

我们定义了一个静态方法 gcd? 来计算两个数的最大公约数。最大公约数是可以整除这些数字而不产生余数的最大正整数。类方法 reduce? 调用我们的静态方法 gcd?,使用 cls.gcd(n1, n2)?。CLS? 是对 fraction? 的引用。

class fraction(object):
    def __init__(self, top, bottom):
        self.num = top
        self.den = bottom
    @staticmethod
    def gcd(m, n):
        while m % n != 0:
            m, n = n, m % n
        return n
    def simplify(self):
        common = Fraction.gcd(self.num, self.den)
        self.num //= common
        self.den //= common
    def __str__(self):
        return str(self.num) + "/" + str(self.den)

if __name__ == "__main__":
    my_fraction = Fraction(8, 24)
    print(my_fraction)
    my_fraction.simplify()
    print(my_fraction)

输出:

8/24
1/3

在这个例子中,我们将 gcd? 方法定义为静态方法。静态方法使用 @staticmethod? 装饰器来标识,因此可以在类上直接调用静态方法,而无需创建类的实例。在 simplify? 方法中,我们可以使用 Fraction.gcd? 来调用 gcd? 方法。

值得注意的是,如果我们将 gcd? 方法定义为实例方法,我们需要使用实例来调用它,例如 self.gcd(m, n)?。

静态方法和类方法都不需要创建类的实例来调用它们,但是类方法的第一个参数是类本身。类方法通常用于创建工厂方法,而静态方法用于实现辅助函数。

类方法

静态方法不应与类方法混淆。类方法和静态方法一样不绑定实例,但与静态方法不同的是,类方法绑定到一个类上。类方法的第一个参数是对类的引用,即类对象。它们可以通过实例或类名调用。

class Robot:
    __counter = 0
    def __init__(self):
        type(self).__counter += 1
    @classmethod
    def RobotInstances(cls):
        return cls, Robot.__counter
if __name__ == "__main__":
    print(Robot.RobotInstances())
    x = Robot()
    print(x.RobotInstances())
    y = Robot()
    print(x.RobotInstances())
    print(Robot.RobotInstances())

输出:

(<class '__main__.Robot'>, 0)
(<class '__main__.Robot'>, 1)
(<class '__main__.Robot'>, 2)
(<class '__main__.Robot'>, 2)

以下是类方法的使用案例:

  1. 在所谓的工厂方法的定义中使用。
  2. 经常在我们需要调用其他静态方法的静态方法中使用。如果我们必须使用静态方法,那么需要硬编码类名,这在存在继承类的情况下会成为问题。

下面的程序包含一个分数类,但尚未完整。在使用分数时,您需要能够约分分数,例如,分数 8/24 可以约分为 1/3。我们可以通过将分子和分母都除以最大公约数(GCD)来将分数约分为最简形式。

我们定义了一个静态方法 gcd 来计算两个数的最大公约数。最大公约数是可以整除这些数字而不产生余数的最大正整数。例如,8 和 24 的最大公约数是 8。类方法 "reduce" 调用我们的静态方法 "gcd",使用 "cls.gcd(n1, n2)"。"CLS" 是对 "fraction" 的引用。

class fraction(object):
    def __init__(self, n, d):
        self.numerator, self.denominator = fraction.reduce(n, d)
    @staticmethod
    def gcd(a,b):
        while b != 0:
            a, b = b, a%b
        return a
    @classmethod
    def reduce(cls, n1, n2):
        g = cls.gcd(n1, n2)
        return (n1 // g, n2 // g)
    def __str__(self):
        return str(self.numerator)+'/'+str(self.denominator)

使用该类:

from fraction1 import fraction
x = fraction(8,24)
print(x)

输出:

1/3

类方法 vs 静态方法和实例方法

在继承中,类方法是非常有用的。我们可以定义一个Pet类,其中包含about方法。这个方法应该提供一些类级别的信息。Cat类会被继承到子类Dog和Cat中,about方法也会被继承。我们将演示如果将about定义为普通实例方法或者静态方法会遇到问题。

我们从将about定义为实例方法开始:

class Pet:
    _class_info = "pet animals"
    def about(self):
        print("This class is about " + self._class_info + "!")   
class Dog(Pet):
    _class_info = "man's best friends"
class Cat(Pet):
    _class_info = "all kinds of cats"
p = Pet()
p.about()
d = Dog()
d.about()
c = Cat()
c.about()

输出:

This class is about pet animals!
This class is about man's best friends!
This class is about all kinds of cats!

初看起来设计没有问题。但仔细一想,我们会意识到这是糟糕的设计。我们必须创建Pet、Dog和Cat类的实例,才能询问类是关于什么的。

如果我们能直接写Pet.about()、Dog.about()和Cat.about()得到前面的结果会更好。但我们不能这么做。我们必须写Pet.about(p)、Dog.about(d)和Cat.about(c)。

现在,我们将about方法定义为静态方法,展示这种方法的缺点。如我们前面学习的,静态方法没有第一个self参数。所以about没有参数。因此,我们可以直接调用Pet.about()、Dog.about()和Cat.about()。但一个问题存在于about的定义中。访问_class_info变量的唯一方式是在类名前面。我们任意地使用Pet。我们也可以使用Cat或Dog。不管我们做什么,结果都不会是我们想要的:

class Pet:
    _class_info = "pet animals"
    @staticmethod
    def about():
        print("This class is about " + Pet._class_info + "!")   
class Dog(Pet):
    _class_info = "man's best friends"
class Cat(Pet):
    _class_info = "all kinds of cats"
Pet.about()
Dog.about()
Cat.about()

输出:

This class is about pet animals!
This class is about pet animals!
This class is about pet animals!

用其他话说,我们别无他法区分Pet类和它的子类Dog和Cat。问题在于about方法不知道它是通过Pet类、Dog类还是Cat类调用的。

类方法是解决所有问题的办法。我们会用类方法装饰器装饰about方法,而不是静态方法装饰器:

class Pet:
    _class_info = "pet animals"
    @classmethod
    def about(cls):
        print("This class is about " + cls._class_info + "!")   
class Dog(Pet):
    _class_info = "man's best friends"
class Cat(Pet):
    _class_info = "all kinds of cats"
Pet.about()
Dog.about()
Cat.about()

输出:

This class is about pet animals!
This class is about man's best friends!
This class is about all kinds of cats!

现在about方法知道它是通过哪个类调用的,并且能打印出对应的类信息。

类方法给cls参数,它是一个引用调用该方法的类。因此about()方法能通过cls.class_info访问对应的类信息。

因此,使用类方法代替静态方法能很好地解决这个问题。

更多

《Python Tricks》专栏是我最近在写的一本针对Python开发人员的实用编程指南,涵盖了Python中最强大和有用的特性和技巧。从基础知识入手,深入介绍函数式编程、面向对象编程、并发编程、网络编程、Web开发、数据处理和机器学习等内容。透彻解析Python语言特性,提供实践案例和示例代码,帮助您优雅解决各种问题。

如果您对python asyncio异步编程模型感兴趣,可以关注我的《python asyncio从入门到精通》专栏。

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接