如何在 Python 中访问类变量
直接在类上访问类变量,例如 Employee.cls_variable
。 类变量由所有实例共享。 我们可以使用 self
对象访问实例变量,例如 self.name
。 实例变量对于每个实例都是唯一的。
class Employee():
# 👇️ class variable
cls_id = 'employee'
def __init__(self, name, salary):
# 👇️ instance variables
self.name = name
self.salary = salary
def get_name(self):
# 👇️ class variable
print(Employee.cls_id)
# 👇️ access instance variable
return self.name
bob = Employee('Bobbyhadz', 100)
print(bob.get_name()) # 👉️ Bobbyhadz
print(bob.cls_id) # 👉️ employee
print(Employee.cls_id) # 👉️ employee
该类有一个 cls_id
类变量以及 name 和 salary 实例变量。
类变量由所有实例共享,可以直接在类上访问,例如 员工.cls_id。
实例变量对于我们通过实例化类创建的每个实例都是唯一的。
我们还可以通过定义类方法来访问类变量。
class Employee():
# 👇️ class variable
cls_id = 'employee'
def __init__(self, name, salary):
# 👇️ instance variables
self.name = name
self.salary = salary
@classmethod
def get_cls_id(cls):
return cls.cls_id
print(Employee.get_cls_id()) # 👉️ employee
我们将 get_cls_id
方法标记为类方法。 传递的第一个参数类方法是类。
常规方法会传递一个 self
对象,该对象为我们提供对实例的引用。
class Employee():
# 👇️ class variable
cls_id = 'employee'
def __init__(self, name, salary):
# 👇️ instance variables
self.name = name
self.salary = salary
def get_name(self):
print(Employee.cls_id)
return self.name
alice = Employee('Alice', 200)
print(alice.get_name()) # 👉️ Alice
print(alice.cls_id) # 👉️ employee
bob = Employee('jiyik', 100)
print(bob.get_name()) # 👉️ jiyik
print(bob.cls_id) # 👉️ bob
name 和 salary 实例变量对于每个实例都是唯一的,但实例共享相同的 cls_id
类变量。
如果需要从类的实例访问类变量,可以使用 type()
类。
class Employee():
# 👇️ class variable
cls_id = 'employee'
def __init__(self, name, salary):
# 👇️ instance variables
self.name = name
self.salary = salary
def get_name(self):
# 👇️ class class variable
print(Employee.cls_id)
# 👇️ access instance variable
return self.name
bob = Employee('jiyik', 100)
# 👇️ 覆盖实例上的类变量
bob.cls_id = 'new'
print(bob.cls_id) # 👉️ new
# 👇️ 从实例访问实际的类变量
result = type(bob).cls_id
print(result) # 👉️ employee
该实例覆盖了 cls_id
变量,因此要访问实际的类变量,我们必须使用 type()
类。
类型类返回对象的类型。
最常见的返回值与访问对象的 __class__
属性相同。
以下代码片段使用 __class__
属性并获得相同的结果。
bob = Employee('jiyik', 100)
bob.cls_id = 'new'
print(bob.cls_id) # 👉️ new
result = bob.__class__.cls_id
print(result) # 👉️ employee
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。