Hướng dẫn update class variable python - cập nhật biến lớp python

Cập nhật các biến lớp trong Python #

Cập nhật các biến thể hiện bằng cách truy cập chúng trực tiếp trên lớp, ví dụ: Employee.cls_variable = new_value. Các biến lớp được chia sẻ bởi tất cả các trường hợp và có thể được cập nhật trực tiếp trên lớp hoặc theo phương thức lớp.

Copied!

class Employee[]: # 👇️ class variable cls_id = 'employee' def __init__[self, name, salary]: # 👇️ instance variables self.name = name self.salary = salary # ✅ update class variable @classmethod def set_cls_id[cls, new_cls_id]: cls.cls_id = new_cls_id return cls.cls_id # ✅ update instance variable def set_name[self, new_name]: self.name = new_name return new_name print[Employee.cls_id] # 👉️ employee Employee.set_cls_id['new_employee_id'] print[Employee.cls_id] # 👉️ new_employee_id bob = Employee['Bobbyhadz', 100] bob.set_name['Bobbyhadz2'] print[bob.name] # 👉️ Bobbyhadz2

Lớp có biến lớp cls_id và các biến thể hiện name

Copied!

@classmethod def set_cls_id[cls, new_cls_id]: cls.cls_id = new_cls_id return cls.cls_id
0.

Các biến lớp được chia sẻ bởi tất cả các trường hợp và có thể được truy cập trực tiếp trên lớp, ví dụ:

Copied!

@classmethod def set_cls_id[cls, new_cls_id]: cls.cls_id = new_cls_id return cls.cls_id
1.

Các biến thể hiện là duy nhất cho mỗi trường hợp bạn tạo bằng cách khởi tạo lớp.

Chúng tôi đã sử dụng một phương thức lớp để cập nhật biến cls_id.

Copied!

@classmethod def set_cls_id[cls, new_cls_id]: cls.cls_id = new_cls_id return cls.cls_id

Phương pháp lớp được vượt qua lớp như là đối số đầu tiên.

Lưu ý rằng bạn cũng có thể đặt các biến lớp trực tiếp trên lớp, ví dụ:

Copied!

@classmethod def set_cls_id[cls, new_cls_id]: cls.cls_id = new_cls_id return cls.cls_id
3.

Copied!

class Employee[]: # 👇️ class variable cls_id = 'employee' def __init__[self, name, salary]: # 👇️ instance variables self.name = name self.salary = salary print[Employee.cls_id] # 👉️ employee # ✅ update class variable Employee.cls_id = 'new_employee_id' print[Employee.cls_id] # 👉️ new_employee_id

Các biến lớp được chia sẻ bởi tất cả các trường hợp của lớp, trong khi các biến thể hiện là duy nhất cho mỗi trường hợp.

Bạn sẽ thường xuyên hơn để cập nhật các biến thể hiện trong lớp. Đây là một ví dụ.

Copied!

class Employee[]: # 👇️ class variable cls_id = 'employee' def __init__[self, name, salary]: # 👇️ instance variables self.name = name self.salary = salary # ✅ update instance variable def set_name[self, new_name]: self.name = new_name return new_name alice = Employee['Alice', 150] bob = Employee['Bobbyhadz', 100] bob.set_name['Bobbyhadz2'] print[bob.name] # 👉️ Bobbyhadz2 print[alice.name] # 👉️ Alice

Cập nhật một biến thể hiện không cập nhật thuộc tính cho các phiên bản khác.

Mặt khác, khi bạn cập nhật một biến lớp, giá trị được cập nhật cho tất cả các trường hợp.

Copied!

class Employee[]: # 👇️ class variable cls_id = 'employee' def __init__[self, name, salary]: # 👇️ instance variables self.name = name self.salary = salary alice = Employee['Alice', 150] bob = Employee['Bobbyhadz', 100] print[bob.cls_id] # 👉️ employee Employee.cls_id = 'NEW_ID' print[alice.cls_id] # 👉️ NEW_ID print[bob.cls_id] # 👉️ NEW_ID

Cập nhật biến lớp cls_id được phản ánh trong tất cả các trường hợp.

Bạn có thể sử dụng lớp

Copied!

@classmethod def set_cls_id[cls, new_cls_id]: cls.cls_id = new_cls_id return cls.cls_id
5 nếu bạn cần truy cập một biến lớp từ một thể hiện của lớp.

Copied!

class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary bob = Employee['Bobbyhadz', 100] # 👇️ override class variable on the instance bob.cls_id = 'new' print[bob.cls_id] # 👉️ new # 👇️ access the actual class variable from the instance result = type[bob].cls_id print[result] # 👉️ employee

Trường hợp ghi đè biến cls_id, vì vậy để truy cập biến lớp thực tế, chúng tôi đã phải sử dụng lớp

Copied!

@classmethod def set_cls_id[cls, new_cls_id]: cls.cls_id = new_cls_id return cls.cls_id
5.

Loại loại trả về loại của một đối tượng.

Thông thường nhất giá trị trả về giống như truy cập thuộc tính

Copied!

@classmethod def set_cls_id[cls, new_cls_id]: cls.cls_id = new_cls_id return cls.cls_id
8 trên đối tượng.

Đoạn mã sau sử dụng thuộc tính

Copied!

@classmethod def set_cls_id[cls, new_cls_id]: cls.cls_id = new_cls_id return cls.cls_id
8 và đạt được kết quả tương tự.

Copied!

bob = Employee['Bobbyhadz', 100] bob.cls_id = 'new' print[bob.cls_id] # 👉️ new result = bob.__class__.cls_id print[result] # 👉️ employee

Bài Viết Liên Quan

Chủ Đề