用法:
class type(object)class type(name, bases, dict, **kwds)使用一個參數,返回
object的類型。返回值是一個類型對象,通常與object.__class__返回的對象相同。建議使用
isinstance()內置函數來測試對象的類型,因為它考慮了子類。使用三個參數,返回一個新的類型對象。這本質上是
class語句的動態形式。name字符串是類名並成為__name__屬性。bases元組包含基類並成為__bases__屬性;如果為空,則添加所有類的最終基礎object。dict字典包含類主體的屬性和方法定義;它可以在成為__dict__屬性之前被複製或包裝。以下兩個語句創建相同的type對象:>>> class X: ... a = 1 ... >>> X = type('X', (), dict(a=1))另請參見類型對象。
提供給三個參數形式的關鍵字參數以與類定義中的關鍵字(除了
metaclass)相同的方式傳遞給適當的元類機製(通常是__init_subclass__())。另請參閱自定義類創建。
在 3.6 版中更改:的子類type不覆蓋
type.__new__可能不再使用單參數形式來獲取對象的類型。
相關用法
- Python type()用法及代碼示例
- Python types.SimpleNamespace用法及代碼示例
- Python types.GenericAlias用法及代碼示例
- Python typing.get_type_hints用法及代碼示例
- Python typing.Concatenate用法及代碼示例
- Python typing.Optional用法及代碼示例
- Python typing.Final用法及代碼示例
- Python typing.TypedDict.__optional_keys__用法及代碼示例
- Python typing.Protocol用法及代碼示例
- Python typing.NoReturn用法及代碼示例
- Python typing.TypedDict.__total__用法及代碼示例
- Python typing.is_typeddict用法及代碼示例
- Python typing.TypeVar用法及代碼示例
- Python typing.AsyncGenerator用法及代碼示例
- Python typing.final用法及代碼示例
- Python typing.ClassVar用法及代碼示例
- Python typing.ParamSpec用法及代碼示例
- Python typing.Literal用法及代碼示例
- Python typing.overload用法及代碼示例
- Python typing.TYPE_CHECKING用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 type。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
