|
|__sizeof__($self, /)
|Return the size of the object in bytes.
|
|__subclasshook__($self, /, subclass)
|Abstract classes can override this to customize issubclass().
|
|----------------------------------------------------------------------
|Data descriptors inherited from object:
|
|__class__
|type(object_or_name) -> the object's type
|
|__delattr__
|x.__delattr__('name') <==> del x.name
|
|__dict__
|dictionary for instance variables (if defined)
|
|__dir__
|Default dir() implementation.
|
|__doc__
|str(object='') -> str
|str(bytes_or_buffer[, encoding[, errors]]) -> str
|
|Create a new string object from the given object. If encoding or
|errors is specified, then the object must expose a data buffer
|that will be decoded using the given encoding and error handler.
|Otherwise, returns the result of object.__str__() (if defined)
|or repr(object).
|encoding defaults to sys.getdefaultencoding().
|errors defaults to 'strict'.
|
|__eq__
|Return self==value.
|
|__format__
|Default object formatter.
|
|__ge__
|Return self>=value.
|
|__getattribute__
|Return getattr(self, name).
|
|__gt__
|Return self>value.
|
|__hash__
|Return hash(self).
|
|__init_subclass__
|This method is called when a class is subclassed.
|
|__le__
|Return self<=value.
|
|__lt__
|Return self
|__ne__
|Return self!=value.
|
|__new__
|Create and return a new object.See help(type) for accurate signature.
|
|__reduce__
|Helper for pickle.
|
|__reduce_ex__
|Helper for pickle.
|
|__repr__
|Return repr(self).
|
|__setattr__
|Implement setattr(self, name, value).
|
|__sizeof__
|__sizeof__() -> int
|size of object in memory, in bytes
|
|__str__
|Return str(self).
|
|__subclasshook__
|Abstract classes can override this to customize issubclass().
```
從輸出結果可以看出,help函數不僅可以查看類的方法,還可以查看類的屬性和繼承關系等信息 。
二、使用dir函數
dir函數可以列出一個對象的所有屬性和方法 。對于類來說,dir函數可以列出該類及其基類的所有屬性和方法 。代碼如下:
```python
class MyClass:
def func1(self):
"""
This is func1
"""
pass
def func2(self):
"""
This is func2
"""
pass
print(dir(MyClass))
```
運行上述代碼后,我們可以看到如下輸出:
```
['__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__', 'func1', 'func2']
```
從輸出結果可以看出,dir函數返回一個列表,其中包含類的所有方法和屬性 。
三、使用inspect模塊
Python的inspect模塊可以幫助我們獲取類的方法和屬性的更多信息,例如方法的參數和返回值等 。代碼如下:
```python
import inspect
class MyClass:
def func1(self, x: int, y: int) -> int:
"""
This is func1
"""
return x + y
def func2(self):
"""
This is func2
"""
pass
print(inspect.signature(MyClass.func1))
```
運行上述代碼后,我們可以看到如下輸出:
猜你喜歡
- python中如何進行希爾排序?
- 吧椅如何選購?吧椅的清潔與保養
- 防盜窗的選購技巧-防盜窗如何清潔?
- 和田地毯如何清洗?和田地毯是什么?
- 指甲保養也是一門學問 如何處理指甲表面不光滑的情況
- 純銀飾品日常保養方法 如何正確處理發黑銀飾
- 衛生間有異味如何去除?水果去異味
- 浴缸應該如何清潔與保養
- 教你如何保養白色衣服
- python實現帶錯誤處理功能的遠程文件讀取方法
