首页 > 其他 > 详细

type类型

时间:2021-04-21 15:52:27      阅读:20      评论:0      收藏:0      [点我收藏+]

1. type类:

python中万物皆对象,如下面的变量a, b, c都是对象

class A(object):
    pass


if __name__ == __main__:
    a = A()
    b = 1
    c = "abc"
    print(type(a))  # <class ‘__main__.A‘>
    print(type(b))  # <class ‘int‘>
    print(type(c))  # <class ‘str‘>
  • a、b、c都是对象,且本别为自定义对象A, int, str类的实例

不仅仅a、b、c是对象,他们的数据类型A、int、str本身也是对象,包括其他的数据类型,它们都是type类的实例。

type类是一个比较特殊的类,所有数据类型都是它的实例。

2. type类的源码:

class type(object):
    """
    type(object_or_name, bases, dict)
    type(object) -> the object‘s type
    type(name, bases, dict) -> a new type
    """
    def mro(self): # real signature unknown; restored from __doc__
        """
        mro() -> list
        return a type‘s method resolution order
        """
        return []

    def __call__(self, *args, **kwargs): # real signature unknown
        """ Call self as a function. """
        pass

    def __delattr__(self, *args, **kwargs): # real signature unknown
        """ Implement delattr(self, name). """
        pass

    def __dir__(self): # real signature unknown; restored from __doc__
        """
        __dir__() -> list
        specialized __dir__ implementation for types
        """
        return []

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __init__(cls, what, bases=None, dict=None): # known special case of type.__init__
        """
        type(object_or_name, bases, dict)
        type(object) -> the object‘s type
        type(name, bases, dict) -> a new type
        # (copied from class doc)
        """
        pass

    def __instancecheck__(self): # real signature unknown; restored from __doc__
        """
        __instancecheck__() -> bool
        check if an object is an instance
        """
        return False

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __prepare__(self): # real signature unknown; restored from __doc__
        """
        __prepare__() -> dict
        used to create the namespace for the class statement
        """
        return {}

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __setattr__(self, *args, **kwargs): # real signature unknown
        """ Implement setattr(self, name, value). """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """
        __sizeof__() -> int
        return memory consumption of the type object
        """
        return 0

    def __subclasscheck__(self): # real signature unknown; restored from __doc__
        """
        __subclasscheck__() -> bool
        check if a class is a subclass
        """
        return False

    def __subclasses__(self): # real signature unknown; restored from __doc__
        """ __subclasses__() -> list of immediate subclasses """
        return []

    __abstractmethods__ = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default


    __bases__ = (
        object,
    )
    __base__ = object
    __basicsize__ = 864
    __dictoffset__ = 264
    __dict__ = None # (!) real value is "mappingproxy({‘__dict__‘: <attribute ‘__dict__‘ of ‘type‘ objects>, ‘__name__‘: <attribute ‘__name__‘ of ‘type‘ objects>, ‘__getattribute__‘: <slot wrapper ‘__getattribute__‘ of ‘type‘ objects>, ‘__new__‘: <built-in method __new__ of type object at 0x0000000062FA3030>, ‘__module__‘: <attribute ‘__module__‘ of ‘type‘ objects>, ‘__doc__‘: <attribute ‘__doc__‘ of ‘type‘ objects>, ‘__delattr__‘: <slot wrapper ‘__delattr__‘ of ‘type‘ objects>, ‘__weakrefoffset__‘: <member ‘__weakrefoffset__‘ of ‘type‘ objects>, ‘__subclasses__‘: <method ‘__subclasses__‘ of ‘type‘ objects>, ‘__instancecheck__‘: <method ‘__instancecheck__‘ of ‘type‘ objects>, ‘__call__‘: <slot wrapper ‘__call__‘ of ‘type‘ objects>, ‘__prepare__‘: <method ‘__prepare__‘ of ‘type‘ objects>, ‘__mro__‘: <member ‘__mro__‘ of ‘type‘ objects>, ‘__abstractmethods__‘: <attribute ‘__abstractmethods__‘ of ‘type‘ objects>, ‘__subclasscheck__‘: <method ‘__subclasscheck__‘ of ‘type‘ objects>, ‘__bases__‘: <attribute ‘__bases__‘ of ‘type‘ objects>, ‘__setattr__‘: <slot wrapper ‘__setattr__‘ of ‘type‘ objects>, ‘__dir__‘: <method ‘__dir__‘ of ‘type‘ objects>, ‘__dictoffset__‘: <member ‘__dictoffset__‘ of ‘type‘ objects>, ‘__base__‘: <member ‘__base__‘ of ‘type‘ objects>, ‘__qualname__‘: <attribute ‘__qualname__‘ of ‘type‘ objects>, ‘__text_signature__‘: <attribute ‘__text_signature__‘ of ‘type‘ objects>, ‘__basicsize__‘: <member ‘__basicsize__‘ of ‘type‘ objects>, ‘__repr__‘: <slot wrapper ‘__repr__‘ of ‘type‘ objects>, ‘__init__‘: <slot wrapper ‘__init__‘ of ‘type‘ objects>, ‘__sizeof__‘: <method ‘__sizeof__‘ of ‘type‘ objects>, ‘__flags__‘: <member ‘__flags__‘ of ‘type‘ objects>, ‘__itemsize__‘: <member ‘__itemsize__‘ of ‘type‘ objects>, ‘mro‘: <method ‘mro‘ of ‘type‘ objects>})"
    __flags__ = -2146675712
    __itemsize__ = 40
    __mro__ = (
        None, # (!) forward: type, real value is "<class ‘type‘>"
        object,
    )
    __name__ = type
    __qualname__ = type
    __text_signature__ = None
    __weakrefoffset__ = 368

3.使用type类

3.1 下面两种方法等效:

class定义类

class A(object):
    p = ppp

type类直接定义类

A = type("A", (object,), {"p": "ppp"})

参数1:类名,参数2:父类,参数3:自定义的类变量

3.2 自定义type:

class my_type(type):
    def __init__(self, name, bases, dict):
        super(my_type, self).__init__(name, bases, dict)
        print("my_type类初始化")


if __name__ == __main__:
    A = my_type("A", (object,), {"p": "ppp"})
    print(A.p)

执行结果:

my_type类初始化
ppp

 

type类型

原文:https://www.cnblogs.com/liuxuelin/p/14684338.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!