‘’‘类对象中特殊的属性’’’
class A:
pass
class B:
pass
class C(A,B):
def __init__(self,name,age):
self.name=name
self.age=age
class D(A):
pass
x=C('张三',20)
print(x.__dict__ )
print(C.__dict__ )
print(x.__class__ )
print(C.__bases__ )
print(C.__base__ )
print(C.__mro__ )
print(A.__subclasses__() )
‘’‘类对象中特殊的方法’’’
class Student:
def __init__(self,name):
self.name=name
def __len__(self):
return len(self.name)
def __add__(self, other):
return self.name+other.name
stu1=Student('张三')
stu2=Student('李四')
stu3=stu1+stu2
print(stu3)
stu4=stu1.__add__(stu2)
print(stu4)
print(len(stu3))
print('-------------------------------------')
class Person:
def __new__(cls, *args, **kwargs):
print('__new__被调用执行了,cls的id值为:{0}'.format(id(cls)))
obj=super().__new__(cls)
print('创建的对象(obj)的值为:{0}'.format(id(obj)))
return obj
def __init__(self,name,age):
print('__init__被调用了,self的id值为:{0}'.format(id(self)))
self.name=name
self.age=age
print('object的id值为:{0}'.format(id(object)))
print('Person的id值为:{0}'.format(id(Person)))
p1=Person('张三',20)
print('创建的实例对象(p1)的id值为:{0}'.format(id(p1)))
'''object的id值为:140728660494752
Person的id值为:2146785008560
__new__被调用执行了,cls的id值为:2146785008560
创建的对象(obj)的值为:2146791471760
__init__被调用了,self的id值为:2146791471760
创建的实例对象(p1)的id值为:2146791471760'''