Hướng dẫn python __list__ - python __list__

>>> class new_class():
...   def __init__(self, number):
...     self.multi = int(number) * 2
...     self.str = str(number)
... 
>>> a = new_class(2)
>>> a.__dict__
{'multi': 4, 'str': '2'}
>>> a.__dict__.keys()
dict_keys(['multi', 'str'])

Bạn cũng có thể tìm thấy Pprint hữu ích.

Nội dung chính ShowShow

  • Nhận thuộc tính của một đối tượng
  • Làm thế nào để bạn in các thuộc tính của một danh sách trong Python?
  • Làm thế nào để tôi nhận được các thuộc tính trong Python?
  • Làm thế nào để tôi thấy tất cả các thuộc tính của một đối tượng trong Python?
  • Làm thế nào để bạn sử dụng các thuộc tính danh sách trong Python?

Nội dung chính

  • Nhận thuộc tính của một đối tượng
  • Làm thế nào để bạn in các thuộc tính của một danh sách trong Python?
  • Làm thế nào để tôi nhận được các thuộc tính trong Python?
  • Làm thế nào để tôi thấy tất cả các thuộc tính của một đối tượng trong Python?
  • Làm thế nào để bạn sử dụng các thuộc tính danh sách trong Python?

Nội dung chínhApr 20, 2010 at 12:44

8

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__

Đã trả lời ngày 20 tháng 4 năm 2010 lúc 12:44Apr 20, 2010 at 12:44Apr 20, 2010 at 12:44

Sau đó, bạn có thể kiểm tra loại nào với

Hướng dẫn python __list__ - python __list__

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
2 hoặc nếu là phương pháp có
>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
3.

Piojo1 gold badge23 silver badges33 bronze badges

5,8811 Huy hiệu vàng23 Huy hiệu bạc33 Huy hiệu đồng1 gold badge23 silver badges33 bronze badges1 gold badge23 silver badges33 bronze badgesApr 20, 2010 at 13:43

Đã trả lời ngày 20 tháng 4 năm 2010 lúc 13:43Apr 20, 2010 at 13:43Apr 20, 2010 at 13:43Htechno

HtechnohtechnoHtechnoHtechno4 gold badges25 silver badges37 bronze badges

4

5.5814 Huy hiệu vàng25 Huy hiệu bạc37 Huy hiệu đồng4 gold badges25 silver badges37 bronze badges4 gold badges25 silver badges37 bronze badges

  1. >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    4
  2. >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    5
  3. >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    6
>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}

Tất cả các câu trả lời trước là chính xác, bạn có ba tùy chọn cho những gì bạn đang hỏi

wjandrea8 gold badges53 silver badges73 bronze badges

24.8k8 Huy hiệu vàng53 Huy hiệu bạc73 Huy hiệu đồng8 gold badges53 silver badges73 bronze badges8 gold badges53 silver badges73 bronze badgesSep 28, 2018 at 22:18

Đã trả lời ngày 28 tháng 9 năm 2018 lúc 22:18Sep 28, 2018 at 22:18Sep 28, 2018 at 22:18grepit

grepitgrepitgrepitgrepit6 gold badges98 silver badges79 bronze badges

2

Phim thương hiệu vàng 19.6K698 Huy hiệu bạc79 Huy hiệu đồng6 gold badges98 silver badges79 bronze badges6 gold badges98 silver badges79 bronze badges

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
7 Trả về các thuộc tính của một đối tượng.Aug 12, 2015 at 13:43

Đã trả lời ngày 12 tháng 8 năm 2015 lúc 13:43Aug 12, 2015 at 13:43Aug 12, 2015 at 13:43rohithpr

Rohithprrohithprrohithprrohithpr4 gold badges35 silver badges59 bronze badges

0

5.7704 Huy hiệu vàng35 Huy hiệu bạc59 Huy hiệu Đồng4 gold badges35 silver badges59 bronze badges4 gold badges35 silver badges59 bronze badges

Kiểm tra mô -đun:


Mô -đun kiểm tra cung cấp một số chức năng hữu ích để giúp có được thông tin về các đối tượng trực tiếp như mô -đun, lớp, phương thức, chức năng, tracbacks, đối tượng khung và đối tượng mã.

Sử dụng

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
8, bạn có thể thấy tất cả các thuộc tính của lớp, cùng với giá trị của chúng. Để loại trừ các thuộc tính riêng tư hoặc được bảo vệ sử dụng
>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
9. Để loại trừ các phương thức hoặc chức năng, sử dụng
dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
10 hoặc
dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
11.
dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
1

Lưu ý rằng

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
12 được sử dụng trên phần tử thứ hai của
dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
13 vì thứ nhất chỉ đơn giản là một chuỗi (tên của nó).

Offtopic: Sử dụng Camelcase cho tên lớp.Jan 19, 2017 at 9:09

Đã trả lời ngày 19 tháng 1 năm 2017 lúc 9:09Jan 19, 2017 at 9:09Jan 19, 2017 at 9:09user

Người dùnguseruser7 gold badges46 silver badges70 bronze badges

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
6

5.1497 Huy hiệu vàng46 Huy hiệu bạc70 Huy hiệu đồng7 gold badges46 silver badges70 bronze badges7 gold badges46 silver badges70 bronze badges

Điều này tất nhiên sẽ in bất kỳ phương thức hoặc thuộc tính nào trong định nghĩa lớp. Bạn có thể loại trừ các phương thức "riêng tư" bằng cách thay đổi

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
14 thành
dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
15Apr 20, 2010 at 12:32

Đã trả lời ngày 20 tháng 4 năm 2010 lúc 12:32Apr 20, 2010 at 12:32Apr 20, 2010 at 12:32SilentGhost

SilentghostsilentghostSilentGhostSilentGhost64 gold badges301 silver badges291 bronze badges

0

294K64 Huy hiệu vàng301 Huy hiệu bạc291 Huy hiệu Đồng64 gold badges301 silver badges291 bronze badges64 gold badges301 silver badges291 bronze badges

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

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
16 để lấy các thuộc tính và
dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
17 để nhận các giá trị

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
1

cách sử dụng :

Điều này đặc biệt hữu ích nếu đối tượng của bạn không có __dict__. Nếu đó không phải là trường hợp bạn cũng có thể thử var (your_object)Dec 17, 2018 at 17:29

Đã trả lời ngày 17 tháng 12 năm 2018 lúc 17:29Dec 17, 2018 at 17:29Dec 17, 2018 at 17:29Jason Angel

Jason Angeljason AngelJason AngelJason Angel1 gold badge13 silver badges14 bronze badges

0

2.0611 Huy hiệu vàng13 Huy hiệu bạc14 Huy hiệu đồng1 gold badge13 silver badges14 bronze badges1 gold badge13 silver badges14 bronze badges

Nó thường được đề cập rằng để liệt kê một danh sách đầy đủ các thuộc tính bạn nên sử dụng

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
4. Tuy nhiên, lưu ý rằng trái với niềm tin phổ biến
>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
4 không đưa ra tất cả các thuộc tính. Ví dụ: bạn có thể nhận thấy rằng
dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
60 có thể bị thiếu trong danh sách
>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
4 của lớp mặc dù bạn có thể truy cập nó từ chính lớp. Từ tài liệu trên
>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
4 (Python 2, Python 3):

Một chức năng như sau đây có xu hướng đầy đủ hơn, mặc dù không có sự hoàn chỉnh nào vì danh sách được trả về bởi

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
4 có thể bị ảnh hưởng bởi nhiều yếu tố bao gồm thực hiện phương thức
dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
64 hoặc tùy chỉnh
dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
65 hoặc
dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
66 trên lớp hoặc một trong các phụ huynh của nó. Xem liên kết được cung cấp để biết thêm chi tiết.
>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
1

Đã trả lời ngày 9 tháng 10 năm 2016 lúc 15:34Oct 9, 2016 at 15:34Oct 9, 2016 at 15:34Oct 9, 2016 at 15:34

Michael Ekokamichael EkokaMichael EkokaMichael EkokaMichael Ekoka

18.2K10 Huy hiệu vàng74 Huy hiệu bạc78 Huy hiệu đồng10 gold badges74 silver badges78 bronze badges10 gold badges74 silver badges78 bronze badges10 gold badges74 silver badges78 bronze badges

Có nhiều hơn một cách để làm điều đó:

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
2

Khi chạy, mã này tạo ra:

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
3

người sử dụng

5.1497 Huy hiệu vàng46 Huy hiệu bạc70 Huy hiệu đồng7 gold badges46 silver badges70 bronze badges7 gold badges46 silver badges70 bronze badges7 gold badges46 silver badges70 bronze badges

Điều này tất nhiên sẽ in bất kỳ phương thức hoặc thuộc tính nào trong định nghĩa lớp. Bạn có thể loại trừ các phương thức "riêng tư" bằng cách thay đổi

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
14 thành
dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
15Jun 12, 2016 at 20:43Jun 12, 2016 at 20:43Jun 12, 2016 at 20:43

0

Đã trả lời ngày 20 tháng 4 năm 2010 lúc 12:32

  • Silentghostsilentghost

    294K64 Huy hiệu vàng301 Huy hiệu bạc291 Huy hiệu Đồng

  • Bạn có thể sử dụng

    dir(instance)
    # or (same value)
    instance.__dir__()
    # or
    instance.__dict__
    
    16 để lấy các thuộc tính và
    dir(instance)
    # or (same value)
    instance.__dir__()
    # or
    instance.__dict__
    
    17 để nhận các giá trị
  • cách sử dụng :

  • Điều này đặc biệt hữu ích nếu đối tượng của bạn không có __dict__. Nếu đó không phải là trường hợp bạn cũng có thể thử var (your_object)

Đã trả lời ngày 20 tháng 4 năm 2010 lúc 13:42Apr 20, 2010 at 13:42Apr 20, 2010 at 13:42Apr 20, 2010 at 13:42

Mike Grahammike GrahamMike GrahamMike GrahamMike Graham

71K14 Huy hiệu vàng97 Huy hiệu bạc129 Huy hiệu đồng14 gold badges97 silver badges129 bronze badges14 gold badges97 silver badges129 bronze badges14 gold badges97 silver badges129 bronze badges

1

Vui lòng xem tập lệnh Python Shell đã được thực thi theo trình tự, ở đây bạn sẽ nhận được các thuộc tính của một lớp ở định dạng chuỗi được phân tách bằng dấu phẩy.

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
8

Tôi đang sử dụng Python 3.4

berna1111

1.7591 Huy hiệu vàng19 Huy hiệu bạc20 Huy hiệu Đồng1 gold badge19 silver badges20 bronze badges1 gold badge19 silver badges20 bronze badges1 gold badge19 silver badges20 bronze badges

Đã trả lời ngày 30 tháng 3 năm 2017 lúc 12:38Mar 30, 2017 at 12:38Mar 30, 2017 at 12:38Mar 30, 2017 at 12:38

1

Ngoài những câu trả lời này, tôi sẽ bao gồm một hàm (Python 3) để phun ra hầu như toàn bộ cấu trúc của bất kỳ giá trị nào. Nó sử dụng

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
11 để thiết lập danh sách đầy đủ các tên thuộc tính, sau đó sử dụng
>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
14 với mỗi tên. Nó hiển thị loại của mọi thành viên của giá trị và khi có thể cũng hiển thị toàn bộ thành viên:
dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
0

Bây giờ bất kỳ điều nào sau đây sẽ cung cấp cái nhìn sâu sắc:

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
1

Đã trả lời ngày 9 tháng 9 năm 2019 lúc 14:35Sep 9, 2019 at 14:35Sep 9, 2019 at 14:35Sep 9, 2019 at 14:35

Gershom Maesgershom MaesGershom MaesGershom MaesGershom Maes

6.6802 Huy hiệu vàng32 Huy hiệu bạc49 Huy hiệu đồng2 gold badges32 silver badges49 bronze badges2 gold badges32 silver badges49 bronze badges2 gold badges32 silver badges49 bronze badges

1

Nhận thuộc tính của một đối tượng

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
2

Đầu ra

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
3

Đã trả lời ngày 11 tháng 6 năm 2019 lúc 22:33Jun 11, 2019 at 22:33Jun 11, 2019 at 22:33Jun 11, 2019 at 22:33

Như đã viết trước khi sử dụng

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
15 có thể xử lý các trường hợp phổ biến nhưng một số lớp không có thuộc tính
>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
6 và sử dụng
>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
17 (chủ yếu là cho hiệu suất bộ nhớ).

Ví dụ cho một cách làm điều này kiên cường hơn:

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
4

Đầu ra của mã này:

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
5

Note1: Python là một ngôn ngữ năng động và tốt hơn hết là biết các lớp bạn đang cố gắng để có được các thuộc tính vì ngay cả mã này có thể bỏ lỡ một số trường hợp. Python is a dynamic language and it is always better knowing the classes you trying to get the attributes from as even this code can miss some cases. Python is a dynamic language and it is always better knowing the classes you trying to get the attributes from as even this code can miss some cases.
Python is a dynamic language and it is always better knowing the classes you trying to get the attributes from as even this code can miss some cases.

Note2: Mã này chỉ xuất ra các biến thể hiện có nghĩa là các biến lớp không được cung cấp. Ví dụ: this code outputs only instance variables meaning class variables are not provided. for example: this code outputs only instance variables meaning class variables are not provided. for example:
this code outputs only instance variables meaning class variables are not provided. for example:

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
6

đầu ra mã:

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
7

Mã này không in thuộc tính lớp

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
18 và có thể bỏ qua các thuộc tính lớp mong muốn. Đôi khi chúng ta có thể nghĩ rằng một thuộc tính là một thành viên thể hiện nhưng nó không và sẽ không được hiển thị bằng ví dụ này. Sometimes we might think an attribute is an instance member but it is not and won't be shown using this example. Sometimes we might think an attribute is an instance member but it is not and won't be shown using this example.
Sometimes we might think an attribute is an instance member but it is not and won't be shown using this example.

người sử dụng

5.1497 Huy hiệu vàng46 Huy hiệu bạc70 Huy hiệu đồng7 gold badges46 silver badges70 bronze badges7 gold badges46 silver badges70 bronze badges7 gold badges46 silver badges70 bronze badges

Đã trả lời ngày 19 tháng 1 năm 2017 lúc 7:01Jan 19, 2017 at 7:01Jan 19, 2017 at 7:01Jan 19, 2017 at 7:01

ShmulikashmulikaShmulikAShmulikAShmulikA

3.2603 Huy hiệu vàng20 Huy hiệu bạc39 Huy hiệu đồng3 gold badges20 silver badges39 bronze badges3 gold badges20 silver badges39 bronze badges3 gold badges20 silver badges39 bronze badges

1

  • Sử dụng
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    6 hoặc
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    10 không hoạt động vì nó bỏ lỡ
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    17.does not work because it misses out
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    17.does not work because it misses out
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    17.does not work because it misses out
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    17.
  • Sử dụng
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    6 và
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    17 không hoạt động vì nó bỏ lỡ
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    17 từ các lớp cơ sở.does not work because it misses out
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    17 from base classes.does not work because it misses out
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    17 from base classes.does not work because it misses out
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    17 from base classes.
  • Sử dụng
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    11 không hoạt động vì nó bao gồm các thuộc tính lớp, chẳng hạn như các phương thức hoặc thuộc tính, cũng như các thuộc tính đối tượng.does not work because it includes class attributes, such as methods or properties, as well as the object attributes.does not work because it includes class attributes, such as methods or properties, as well as the object attributes.does not work because it includes class attributes, such as methods or properties, as well as the object attributes.
  • Sử dụng
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    10 tương đương với việc sử dụng
    >>> dir(a)
    ['__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__', 'multi', 'str']
    >>> vars(a)
    {'multi': 4, 'str': '2'}
    >>> a.__dict__
    {'multi': 4, 'str': '2'}
    
    6.

Đây là điều tốt nhất tôi có:

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
8

GED

6876 Huy hiệu bạc17 Huy hiệu đồng6 silver badges17 bronze badges6 silver badges17 bronze badges6 silver badges17 bronze badges

Đã trả lời ngày 19 tháng 2 năm 2020 lúc 11:31Feb 19, 2020 at 11:31Feb 19, 2020 at 11:31Feb 19, 2020 at 11:31

c zc zc zc zc z

6.8693 Huy hiệu vàng41 Huy hiệu bạc53 Huy hiệu Đồng3 gold badges41 silver badges53 bronze badges3 gold badges41 silver badges53 bronze badges3 gold badges41 silver badges53 bronze badges

2

Vui lòng xem thực thi kịch bản Python Shell sau đây theo trình tự, nó sẽ cung cấp giải pháp từ việc tạo lớp để trích xuất tên trường của các trường hợp.

dir(instance)
# or (same value)
instance.__dir__()
# or
instance.__dict__
9

Đã trả lời ngày 19 tháng 1 năm 2017 lúc 5:49Jan 19, 2017 at 5:49Jan 19, 2017 at 5:49Jan 19, 2017 at 5:49

Hygullhygullhygullhygullhygull

8.1202 Huy hiệu vàng41 Huy hiệu bạc47 Huy hiệu đồng2 gold badges41 silver badges47 bronze badges2 gold badges41 silver badges47 bronze badges2 gold badges41 silver badges47 bronze badges

1

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
0

Đã trả lời ngày 8 tháng 9 năm 2016 lúc 9:43Sep 8, 2016 at 9:43Sep 8, 2016 at 9:43Sep 8, 2016 at 9:43

shammerw0wshammerw0wshammerw0wshammerw0wshammerw0w

1.8811 Huy hiệu vàng13 Huy hiệu bạc10 Huy hiệu đồng1 gold badge13 silver badges10 bronze badges1 gold badge13 silver badges10 bronze badges1 gold badge13 silver badges10 bronze badges

1

>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
18 đưa ra danh sách các thuộc tính của một thể hiện.
>>> dir(a)
['__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__', 'multi', 'str']
>>> vars(a)
{'multi': 4, 'str': '2'}
>>> a.__dict__
{'multi': 4, 'str': '2'}
1

Đã trả lời ngày 16 tháng 3 năm 2018 lúc 23:15Mar 16, 2018 at 23:15Mar 16, 2018 at 23:15Mar 16, 2018 at 23:15

VivekvivekVivekVivekVivek

3001 huy hiệu bạc5 huy hiệu đồng1 silver badge5 bronze badges1 silver badge5 bronze badges1 silver badge5 bronze badges

1

Làm thế nào để bạn in các thuộc tính của một danh sách trong Python?

Sử dụng Dir của Python để in các thuộc tính của một đối tượng Một trong những cách dễ nhất để truy cập các thuộc tính của đối tượng Python là hàm Dir ().Hàm này được tích hợp trực tiếp vào Python, do đó không cần phải nhập bất kỳ thư viện nào. One of the easiest ways to access a Python object's attributes is the dir() function. This function is built-in directly into Python, so there's no need to import any libraries. One of the easiest ways to access a Python object's attributes is the dir() function. This function is built-in directly into Python, so there's no need to import any libraries. One of the easiest ways to access a Python object's attributes is the dir() function. This function is built-in directly into Python, so there's no need to import any libraries.

Làm thế nào để tôi nhận được các thuộc tính trong Python?

Chức năng Python getAttr ().Hàm python getAttr () được sử dụng để nhận giá trị của thuộc tính của một đối tượng và nếu không tìm thấy thuộc tính nào của đối tượng đó, giá trị mặc định được trả về.. Python getattr() function is used to get the value of an object's attribute and if no attribute of that object is found, default value is returned.. Python getattr() function is used to get the value of an object's attribute and if no attribute of that object is found, default value is returned.. Python getattr() function is used to get the value of an object's attribute and if no attribute of that object is found, default value is returned.

Làm thế nào để tôi thấy tất cả các thuộc tính của một đối tượng trong Python?

Để liệt kê tất cả các thuộc tính của một đối tượng, hãy sử dụng hàm dir () tích hợp.Nó trả về một danh sách dài các tên thuộc tính, nghĩa là, phương thức và tên biến của đối tượng.use the built-in dir() function. It returns a long list of attribute names, that is, method and variable names of the object.use the built-in dir() function. It returns a long list of attribute names, that is, method and variable names of the object.use the built-in dir() function. It returns a long list of attribute names, that is, method and variable names of the object.

Làm thế nào để bạn sử dụng các thuộc tính danh sách trong Python?

Các đặc điểm quan trọng của danh sách Python như sau:....

Danh sách được đặt hàng ..

Danh sách có thể chứa bất kỳ đối tượng tùy ý nào ..

Danh sách các yếu tố có thể được truy cập bởi Index ..

Danh sách có thể được lồng đến độ sâu tùy ý ..

Danh sách có thể thay đổi ..

Danh sách rất năng động ..