Python-log 利用getattribute自动输出 调用的方法或者属性log


class F(object):

    def __init__(self):
        self.name = 'A'
    def hello(self):
        print('hello')
    def __getattribute__(self, item):
        if  item == "__dict__":
            return object.__getattribute__(self, item)

        elif (item in str(self.__dict__)) :
            if ('function' in str(self.__dict__[item])) :
                print("正在执行方法.......................%s() ................返回的结果为如下:"%item)
            else:
                print('正在获取属性..........................%s'%item)
                print("获取的值为.........................%s"%(self.__dict__[item]))
        elif (item in (F.__dict__).keys()):

            if ('function' in str(F.__dict__[item])):
                print("正在执行方法.......................%s() ................返回的结果为如下:" % item)
            else:
                print('正在获取属性..........................%s' % item)
                print("获取的值为.........................%s" % (F.__dict__[item]))
        else :
            return object.__getattribute__(self, item)
        return object.__getattribute__(self, item)
    def __getattr__(self, item):
        if item == 'age':
            return 40
        else :
            print("请确认你的属性..........%s,是否正确,请check下   "%item)
    def __setattr__(self, key, value):
        if ('function' in str(type(value))) :

                key='test'+key
                self.__dict__[key] = value
                print("创建方法成功方法名字为....................%s()"%key)
        else:

            self.__dict__[key] = value
            print("创建属性成功 ,属性的名字为......................... %s"%key)
def my():
    return  " 我正在自我介绍.."
def run():
    print(  " 我是马儿正在跑....")
a= F()
a.eat=my
a.name="奥巴马"
print(a.testeat())
print('........................................................')
setattr(a,"run",run)
a.testrun()
a.name
a.hello()
a.tt

打印结果为:

创建属性成功 ,属性的名字为… name
创建方法成功方法名字为…testeat()
创建属性成功 ,属性的名字为… name
正在执行方法…testeat() …返回的结果为如下:
我正在自我介绍…

创建方法成功方法名字为…testrun()
正在执行方法…testrun() …返回的结果为如下:
我是马儿正在跑…
正在获取属性…name
获取的值为…奥巴马
正在执行方法…hello() …返回的结果为如下:
hello
请确认你的属性…tt,是否正确,请check下

你可能感兴趣的:(Python,高级编程知识点汇总)