>>> a = Example[]
>>> dir[a]
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__',
'__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', 'bool143', 'bool2', 'blah',
'foo', 'foobar2000', 'as_list']
Bạn thấy, điều đó cung cấp cho bạn tất cả các thuộc tính, vì vậy bạn sẽ phải lọc ra một chút. Nhưng về cơ bản,
0 là những gì bạn đang tìm kiếm.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Lặp lại các thuộc tính của một đối tượng trong Python #
Lặp lại các thuộc tính của một đối tượng:
- Sử dụng hàm
0 để có được danh sách tên của các thuộc tính của đối tượng.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
- Tùy chọn lọc các thuộc tính bắt đầu với hai dấu gạch dưới và phương thức.
- Sử dụng vòng lặp
3 để lặp lại các thuộc tính của đối tượng.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Hàm Dir trả về một danh sách các tên của các thuộc tính của đối tượng và đệ quy thuộc tính của các lớp cơ sở của nó.
Bạn cũng có thể chuyển một lớp cho chức năng
0, nó không chỉ chấp nhận các trường hợp.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # 👇️ {'name': 'bobbyhadz', 'salary': 100} print[emp1.__dict__]
Chúng ta có thể sử dụng phương thức
2 để có được một đối tượng xem chúng ta có thể lặp lại.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value]
Phương thức Dict.Items trả về một chế độ xem mới về các mục của từ điển [[khóa, giá trị]].
Bước cuối cùng là sử dụng vòng lặp
3 để lặp lại các thuộc tính của đối tượng.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Một giải pháp thay thế cho việc sử dụng thuộc tính
1 là sử dụng hàmCopied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
8 tích hợp.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Hàm VARS có một đối tượng và trả về thuộc tính
1 của mô -đun, lớp, thể hiện đã cho hoặc bất kỳ đối tượng nào khác có thuộc tínhCopied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
1.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Hàm
8 tăngCopied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
2 nếu đối tượng được cung cấp không có thuộc tínhCopied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # 👇️ {'name': 'bobbyhadz', 'salary': 100} print[emp1.__dict__]
1.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Nếu bạn cần bao gồm các biến lớp khi lặp lại, hãy sử dụng hàm
0.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Lặp lại các thuộc tính của một đối tượng bằng dir [] #
Lặp lại các thuộc tính của một đối tượng:
- Sử dụng hàm
0 để có được danh sách tên của các thuộc tính của đối tượng.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
- Tùy chọn lọc các thuộc tính bắt đầu với hai dấu gạch dưới và phương thức.
- Sử dụng vòng lặp
3 để lặp lại các thuộc tính của đối tượng.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] for attribute in dir[emp1]: print[attribute, getattr[emp1, attribute]] result = [attribute for attribute in dir[emp1] if not attribute.startswith['__']] print[result] # 👉️ ['cls_id', 'name', 'salary']
Hàm Dir trả về một danh sách các tên của các thuộc tính của đối tượng và đệ quy thuộc tính của các lớp cơ sở của nó.
Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'cls_id', 'name', 'salary'] print[dir[emp1]]
Bạn cũng có thể chuyển một lớp cho chức năng
0, nó không chỉ chấp nhận các trường hợp.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Nếu bạn cần loại trừ các thuộc tính bắt đầu với hai dấu gạch dưới, hãy sử dụng danh sách hiểu.
Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] attributes = [attribute for attribute in dir[emp1] if not attribute.startswith['__']] print[attributes] # 👉️ ['cls_id', 'name', 'salary'] for attr in attributes: # cls_id employee # name bobbyhadz # salary 100 print[attr, getattr[emp1, attr]]
Bạn có thể sử dụng hàm Copied!
8 nếu bạn cần lấy giá trị của một thuộc tính khi được đặt tên thuộc tính dưới dạng chuỗi.class Employee[]:
cls_id = 'employee'
def __init__[self, name, salary]:
self.name = name
self.salary = salary
emp1 = Employee['bobbyhadz', 100]
# 👇️ {'name': 'bobbyhadz', 'salary': 100}
print[emp1.__dict__]
Hàm GetAttr trả về giá trị của thuộc tính được cung cấp của đối tượng.
Hàm lấy các tham số sau:
sự vật | đối tượng có thuộc tính nên được truy xuất |
Tên | Tên của thuộc tính |
mặc định | giá trị mặc định khi thuộc tính không tồn tại trên đối tượng |
Nếu lớp của bạn có các phương thức, bạn cũng có thể muốn loại trừ các phương thức khi lặp lại các thuộc tính của đối tượng.
Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary def get_name[self]: return self.name emp1 = Employee['bobbyhadz', 100] attributes = [attribute for attribute in dir[emp1] if not attribute.startswith['__'] and not callable[getattr[emp1, attribute]]] print[attributes] # 👉️ ['cls_id', 'name', 'salary'] for attr in attributes: # cls_id employee # name bobbyhadz # salary 100 print[attr, getattr[emp1, attr]]
Hàm có thể gọi được lấy một đối tượng làm đối số và trả về
9 nếu đối tượng xuất hiện có thể gọi được, nếu khôngCopied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # 👇️ {'name': 'bobbyhadz', 'salary': 100} print[emp1.__dict__]
0 được trả về.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value]
Chúng tôi đã sử dụng chức năng để loại trừ tất cả các phương thức khỏi các thuộc tính.
Nếu bạn chỉ muốn lặp lại các biến của lớp, bạn sẽ chuyển lớp vào hàm
0, không phải là ví dụ.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]
Copied!
class Employee[]: cls_id = 'employee' another = 'foo' def __init__[self, name, salary]: self.name = name self.salary = salary class_variables = [attribute for attribute in dir[Employee] if not attribute.startswith['__'] and not callable[getattr[Employee, attribute]] ] print[class_variables] # 👉️ ['another', 'cls_id'] for attr in class_variables: # another foo # cls_id employee print[attr, getattr[Employee, attr]]
Chúng tôi đã chuyển lớp cho hàm
0 và lọc ra tất cả các biến lớp bắt đầu với hai dấu gạch dưới và tất cả các phương thức lớp trước khi lặp lại.Copied!
class Employee[]: cls_id = 'employee' def __init__[self, name, salary]: self.name = name self.salary = salary emp1 = Employee['bobbyhadz', 100] # ✅ iterate through an object's attributes for attribute, value in emp1.__dict__.items[]: # name bobbyhadz # salary 100 print[attribute, value] # ------------------------------------------- # ✅ using vars instead of __dict__ for attribute, value in vars[emp1].items[]: # name bobbyhadz # salary 100 print[attribute, value]