一、反射
1、反射是什么?
使用字符串数据类型的变量名来获取这个变量的值
2、常用几个场景?
input
根据用户的输入,获取相应的值
文件
从文件读出的字符串,想转成变量的名字
网络
将网络传输的字符串转成变量的名字
3、getattr 与 hasattr
getattr(变量名:命名空间,字符串:属于一个命名空间中的变量)
# 反射类中的静态属性,类方法,静态方法 class Foo: School = '希望小学' country = 'china' @classmethod def class_method(cls): print(cls.School) def static_method(): print('is static') while 1: inp = input('<<<').strip() print(getattr(Foo,inp))
# 反射对象中的变量,普通方法,对象属性 class Foo: def __init__(self,name,age): self.name = name self.age = age def eat(self): print('吃东西') f = Foo('xiaoming',12) print(getattr(f,'name')) print(getattr(f,'age')) getattr(f,'eat')()
# 反射本文件中的变量 a = 1 def fun(): print('xxx') class Foo:pass import sys # print(sys.modules['__main__']) #获取本文件的命名空间 F:/Demo/面向对象/反射与内置方法.py # print(sys.modules['__main__'].a) # print(__name__) print(getattr(sys.modules[__name__],'a')) print(getattr(sys.modules[__name__],'fun')()) obj = getattr(sys.modules[__name__],'Foo')() print(obj)
4、setattr 与 delattr
class Foo: country = 'china' def fun(): print('xxx') setattr(Foo,'country','us') #接收三个参数 命名空间 变量名 变量值 print(Foo.__dict__) print(getattr(Foo,'country')) print(Foo.country) setattr(Foo,'fun',fun) print(Foo.__dict__) getattr(Foo,'fun')() #一般不往空间中添加函数 delattr(Foo,'country') print(Foo.__dict__)
二、内置方法
1、不需要自己定义,本身就存在类中的方法
一般都是__变量名__,又叫双下方法,魔术方法
2、__str__ __repr__
class Course: def __init__(self,name,price,teacher): self.name = name self.price = price self.teacher = teacher def __str__(self): return 'str: %s %s %s' %(self.name,self.price,self.teacher) def __repr__(self): return 'repr: %s %s %s' % (self.name, self.price, self.teacher) course_list = [] python = Course('python',29800,'jing-l') linux = Course('linux',9800,'taix) course_list.append(python) course_list.append(linux) for id,course in enumerate(course_list,1): print(id,course) print('%s %s' %(id,course)) print(id,str(course)) print(id,repr(course)) print('%s %r' %(id,course)) # __str__ # 当你打印一个对象的时候 触发__str__ # 当你使用%s格式化的时候 触发__str__ # str强转数据类型的时候 触发__str__ # __repr__ # repr是str的备胎 # 有__str__的时候执行__str__,没有实现__str__的时候,执行__repr__ # repr(obj)内置函数对应的结果是 __repr__的返回值 # 当你使用%r格式化的时候 触发__repr__