1 对象、类、类型
实例化:如果一个类后面加一个括号就返回一个对象,这个对象就是这个类的实例
class A:
pass
a = A()
print(a.__class__)
继承:如果一个类继承自一个类,继承的这个类是子类,被继承的类是超类或者是父类,Python中可以单继承,也可以多继承
class A:
pass
class B(A):
pass
print(issubclass(B,A))
class A:
def __init__(self,a):
self.a = a
class B(A):
def __init__(self,a,b):
A.__init__(self,a)
self.b = b
class C(A):
def __init__(self, a, c):
A.__init__(self, a)
self.c = c
class D(C,B):
def __init__(self,a,b,c,d):
B.__init__(self,a,b)
C.__init__(self,a,c)
self.d = d
print(D.__mro__)
"""
OUT:类的继承顺序链
(<class '__main__.D'>, <class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>)
"""
2 对象的类
Python中一切皆对象,int是一个对象,代表整数类型,2也是一个对象,代表一个整数。
class A:
pass
a = A()
print(a.__class__)
"""
a是通过A类实例化出来的对象
我们说Python中一切皆对象,那么A类是是由什么实例化出来的呢?
"""
print(A.__class__)
""" 继续看type的什么类实例化出来的 """
print(type.__class__)
""" type类实例化了自己形成了type类 """
print(a.__class__,type(a))
print(A.__class__,type(A))
print(type.__class__,type(type))
"""
<class '__main__.A'> <class '__main__.A'>
<class 'type'> <class 'type'>
<class 'type'> <class 'type'>
"""
结论,Python3中类和类型是一样的
3 Python中的"新式类"
不聊Python2,在Python3版本中,所有的类默认继承object类
class A:
pass
class B():
pass
class C(object):
pass
print(A.__bases__)
print(B.__bases__)
print(C.__bases__)
"""
以上三种写法都是默认继承了object类
__bases__属性会获取一个类的所有超类
注意:实例对象没有__bases__属性
(<class 'object'>,)
(<class 'object'>,)
(<class 'object'>,)
"""
print(A().__bases__)
"""也可以使用instance方法检查两个类之间继承关系"""
print(issubclass(A,object))
print(issubclass(B,object))
print(issubclass(C,object))
4 object类有什么魔法
object类在buildtins.py文件中,里面包含了大量的魔法方法,如果子类不重写这些方法,那么所有的对象将“天然”带上这些魔力
class object:
def __delattr__(self, *args, **kwargs):
""" Implement delattr(self, name). """
pass
def __dir__(self, *args, **kwargs):
""" Default dir() implementation. """
pass
def __eq__(self, *args, **kwargs):
""" Return self==value. """
pass
def __format__(self, *args, **kwargs):
""" Default object formatter. """
pass
def __getattribute__(self, *args, **kwargs):
""" Return getattr(self, name). """
pass
def __ge__(self, *args, **kwargs):
""" Return self>=value. """
pass
def __gt__(self, *args, **kwargs):
""" Return self>value. """
pass
def __hash__(self, *args, **kwargs):
""" Return hash(self). """
pass
def __init_subclass__(self, *args, **kwargs):
"""
This method is called when a class is subclassed.
The default implementation does nothing. It may be
overridden to extend subclasses.
"""
pass
def __init__(self):
""" Initialize self. See help(type(self)) for accurate signature. """
pass
def __le__(self, *args, **kwargs):
""" Return self<=value. """
pass
def __lt__(self, *args, **kwargs):
""" Return self<value. """
pass
@staticmethod
def __new__(cls, *more):
""" Create and return a new object. See help(type) for accurate signature. """
pass
def __ne__(self, *args, **kwargs):
""" Return self!=value. """
pass
def __reduce_ex__(self, *args, **kwargs):
""" Helper for pickle. """
pass
def __reduce__(self, *args, **kwargs):
""" Helper for pickle. """
pass
def __repr__(self, *args, **kwargs):
""" Return repr(self). """
pass
def __setattr__(self, *args, **kwargs):
""" Implement setattr(self, name, value). """
pass
def __sizeof__(self, *args, **kwargs):
""" Size of object in memory, in bytes. """
pass
def __str__(self, *args, **kwargs):
""" Return str(self). """
pass
@classmethod
def __subclasshook__(cls, subclass):
"""
Abstract classes can override this to customize issubclass().
This is invoked early on by abc.ABCMeta.__subclasscheck__().
It should return True, False or NotImplemented. If it returns
NotImplemented, the normal algorithm is used. Otherwise, it
overrides the normal algorithm (and the outcome is cached).
"""
pass
__class__ = None
__dict__ = {}
__doc__ = ''
__module__ = ''
class A:
pass
a = A()
a.xx = 'hello'
print(a.xx)
del a.xx
print(a.xx)
里面的特殊方法都可以使用,对于一些防止递归的场合尤其合适,对于__new__方法可以用来控制做单例模式。
5 object与type
print(object.__class__)
"""<class 'type'> 这个输出说明object对象是type类的一个实例,type实例化得到了object"""
print(isinstance(object,type))
print(object.__bases__)
"""() object没有超类,他是对象的顶端,不继承任何类"""
print(type.__bases__)
"""(<class 'object'>,)type是object的一个子类"""
print(issubclass(type,object))
结论 对于object和type确实不好理解,首先python一切都是对象,类是对象,实例是对象,函数是对象,整数也是。。。这个先确认好。 类到对象的过程是实例化的过程,通过实例的__class__属性或者type()内置函数可以看到对象来源于哪个类 超类到子类的过程是继承,通过类的__bases__属性可以看到所有超类 object是Python中所有类对象的超类 object类是通过type类型实例化得到的
|