其他分享
首页 > 其他分享> > 反射

反射

作者:互联网

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# __author__=="HAI"


class dog(object):

    def player(self):
        print(' 在玩')

    def eat(self):
        print('在吃东西')


d = dog()
chioce = input('>>')

if hasattr(d, chioce):
    fn = getattr(d, chioce)
    fn()
else:
    val_e = input('这个属性不存在,重新设置 他的值value:')
    setattr(d, chioce, val_e)
    v = getattr(d, chioce)
    print(chioce, v)

    delattr(d, chioce)
    print(hasattr(d, chioce))

import sys

a = sys.modules

hasattr(sys.modules[__name__], 'aaa')

标签:__,反射,sys,chioce,fn,print,hasattr
来源: https://blog.csdn.net/Hai_peng_yu/article/details/86680949