面向对象进阶

isinstance

isinstance(obj,cls)检查是否obj是否是类 cls 的对象

class Foo(object):
     pass
  
obj = Foo()
  
isinstance(obj, Foo)

反射

使用字符串数据类型的变量名来操作一个变量的值
使用反射获取某个命名空间中的值,
需要
有一个变量指向这个命名空间的
字符串数据类型的名字,
再使用getattr获取值,
如果是变量能直接获得结果
如果是函数,只能拿到内存地址,加上括号就是执行
使用类名反射 : 静态属性、类方法、静态方法
使用对象名反射 :对象属性、类中的普通方法
使用模块名反射 : 变量 函数 类
在自己所在的文件中反射全局变量 :getattr(sys.modules[__name__],'要反射的名字')

mmm文件

a = 1
b = 2

def wahaha():print('wahaha')

class QQxing:
    def __init__(self,name):
        self.name = name
    def ADCa(self):
        print('in ADCa',self.name)

# a
# b
# wahaha
# QQxing
# getattr('a')


import sys

print(sys.modules)
print(sys.modules[__name__])
print(getattr(sys.modules[__name__],'a'))
print(getattr(sys.modules[__name__],'b'))
getattr(sys.modules[__name__],'wahaha')()

getattr    hasattr

import mmm
# while True:
#     inp = input(">>>")
#     if hasattr(mmm,inp):
#         print(getattr(mmm,inp))
cls = getattr(mmm,"QQxing")("光头")
# taibai = cls("光头")
getattr(cls,"ADCa")()

setattr      delattr

class A:
    Name1 = 'alex'
    Name2 = 'egon'
    Name3 = 'guangtou'
    def __init__(self,name):
        self.name = name
    def func(self):
        print('in func',self.name)

A.Country = '中国'  #
A.Name1 = 'alex_sb' #
# setattr
print(A.__dict__)
setattr(A,'Country','印度')
print(A.__dict__)
# 'Role' = 'Person'
setattr(A,'Role','Person')
print(A.__dict__)
delattr(A,'Name1')
print(A.__dict__)

__str__和__repr__

在没有实现__str__方法的情况下,__repr__能够完全替代__str__的功能
但是__str__不能代替__repr__的工作
在__repr__和__str__都存在的情况下:
__repr__方法 和 repr() %r都是息息相关的
__str__方法 和 str() print() %s 都是息息相关的

class Foo:
    def __init__(self,name,price,peiod):
        self.name = name
        self.price = price
        self.peiod = peiod
    def __str__(self):
        return "%s %s %s" % (self.name,self.price,self.peiod)

courses = []
python = Foo("python","23000","6 months")
courses.append(python)
for i in courses:
    print(i)

同时存在

class Student:
    def __init__(self,name,sex,age):
        self.name = name
        self.sex = sex
        self.age = age
    def __str__(self):
        return self.name
    def __repr__(self):
        return '|'.join([self.name,self.sex,str(self.age)])

stu1 = Student('光头','male',40)
print('student : %r'%stu1)
print('student : %s'%stu1)
print(repr(stu1))
print(str(stu1))
print(stu1)

两个对象 的name 和 sex相同,那么这两个对象就是相同的对象

对100个对象进行去重

hash是否相等,值是否相等 __hash__值相等,__eq__值也相等

class Person:
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex
    def __hash__(self):
        return hash(self.name+self.sex)
    def __eq__(self, other):
        if type(self) == type(other) and 
            self.name == other.name and 
            self.sex == other.sex :
            return True
        else: return  False
    def __repr__(self):
        return "%s %s %s" % (self.name,self.age,self.sex)
person_lst = [Person('egon',i,'male') for i in range(100)]
print(set(person_lst))  # hash是否相等,值是否相等  __hash__值相等,__eq__值也相等
print(hash(person_lst[1]))
print(hash(person_lst[2]))
print(person_lst[1] == person_lst[2])

for i in person_lst:
    print(i)

__call__

类的内置方法 双下方法 魔术方法
对象加上(),可以出发这个类的__call__方法。

class Foo:

    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        print('__call__')


obj = Foo()  # 执行 __init__
obj()  # 执行 __call__
Foo()()  #类加两个括号执行 __call__

__len__

class Foo:

def __init__(self,student):
self.student = student

def __len__(self):
return len(self.student)

cls = Foo([1,2,3,4,5])
print(len(cls))

_eq__ 是判断值是否相等的时候依赖__eq__的结果

class Person:
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    def __eq__(self, other):
        if type(self) == type(other) and 
            self.name == other.name and 
            self.sex == other.sex :
            return True
        else: return  False

p1 = Person('egon',40,'male')
p2 = Person('egon',50,'male')
p3 = Person('光头',80,'male')
print(p1==p2)
print(p1==p3)
原文地址:https://www.cnblogs.com/luchenhui/p/9112972.html