python based learning Day 18 (a)

# Python based learning Day 18 (a)

A reflection

The concept is reflected by Smith in 1982 first proposed, mainly it refers to the program can access, the ability to detect and modify its own state or behavior (introspection). It puts forward the concept quickly led to research on the application of computer science reflective. It was first used in the field of programming language design, and has made achievements in Lisp and object-oriented aspects.

python reflective object-oriented: the object-related properties operated by a string. Everything is an object in python (could use reflection)

Four function can be achieved introspective

The following method is applicable to classes and objects (everything is an object, the class itself is a target)

1.1 reflection object

class Foo:
    f = '类的静态变量'
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def say_hi(self):
        print('hi,%s'%self.name)

obj=Foo('egon',73)

#检测是否含有某属性
print(hasattr(obj,'name'))
print(hasattr(obj,'say_hi'))

#获取属性
n=getattr(obj,'name')
print(n)
func=getattr(obj,'say_hi')
func()

print(getattr(obj,'aaaaaaaa','不存在啊')) #报错

#设置属性
setattr(obj,'sb',True)
setattr(obj,'show_name',lambda self:self.name+'sb')
print(obj.__dict__)
print(obj.show_name(obj))

#删除属性
delattr(obj,'age')
delattr(obj,'show_name')
delattr(obj,'show_name111')#不存在,则报错

print(obj.__dict__)

对实例化对象的示例

1.2 pairs of reflecting type

class Foo(object):

    staticField = "old boy"

    def __init__(self):
        self.name = 'wupeiqi'

    def func(self):
        return 'func'

    @staticmethod
    def bar():
        return 'bar'

print getattr(Foo, 'staticField')
print getattr(Foo, 'func')
print getattr(Foo, 'bar')

Reflecting the current module 1.3

import sys


def s1():
    print 's1'


def s2():
    print 's2'


this_module = sys.modules[__name__]

hasattr(this_module, 's1')
getattr(this_module, 's2')

Other modules reflecting 1.4

#一个模块中的代码
def test():
    print('from the test')
"""
程序目录:
    module_test.py
    index.py


当前文件:
    index.py
"""
# 另一个模块中的代码
import module_test as obj

#obj.test()

print(hasattr(obj,'test'))

getattr(obj,'test')()

1.5 Application of the reflected

Learn the four functions of reflection. Then the reflection in the end what use is it? What is its scenario is it?

Now let's open a browser and visit a website, you click to jump to the login screen to log on, you click Register Jump to register interface, and so on, in fact you actually click one of the links, each Links will have a function or method to deal with.

Previous solution did not learn reflection

class User:
    def login(self):
        print('欢迎来到登录页面')

    def register(self):
        print('欢迎来到注册页面')

    def save(self):
        print('欢迎来到存储页面')


while 1:
    choose = input('>>>').strip()
    if choose == 'login':
        obj = User()
        obj.login()

    elif choose == 'register':
        obj = User()
        obj.register()

    elif choose == 'save':
        obj = User()
        obj.save()

After studying reflection solution

class User:
    def login(self):
        print('欢迎来到登录页面')

    def register(self):
        print('欢迎来到注册页面')

    def save(self):
        print('欢迎来到存储页面')

user = User()
while 1:
    choose = input('>>>').strip()
    if hasattr(user,choose):
        func = getattr(user,choose)
        func()
    else:
        print('输入错误。。。。')

So that you can clearly feel the benefits of reflection, the way we have been using the dictionary is actually a reflection of thought

We just reflectance before writing method also handed

Guess you like

Origin www.cnblogs.com/bky20061005/p/12131402.html