【发布时间】:2021-11-04 11:07:41
【问题描述】:
class Employee:
location = "south"
def describe(self):
print(self.location)
我应该使用 self.class_variable 在类方法中访问类变量吗?
class Employee:
location = "south"
def describe(self):
print(Employee.location)
或者我应该使用class_name.class_variable? 哪一个是正确的约定? 这两者有区别吗?
编辑 1: 因此,除了人们给出的其他答案之外,我发现 如果您更改 self.class_variable,它将仅针对该实例更改它 如果您更改 class_name.class_variable,它将为所有当前和未来的实例更改它。 希望对您有所帮助。
【问题讨论】:
标签: python class oop methods class-variables