Override -Class Attribute- getter

后端 未结 2 523
悲&欢浪女
悲&欢浪女 2021-01-06 10:44

I\'m defining a Debug class like so:

_debug = False

class Debug:
    DrawOutlines = True
    InvinciblePlayer = True

I want to override th

2条回答
  •  长发绾君心
    2021-01-06 11:01

    You override __getattribute__ to intercept all attribute access, or __getattr__ to get called only for attributes that do not exist:

    _debug = True
    class A(object):
        def __getattribute__(self, attr):
           if _debug:
               print attr
           return  object.__getattribute__(self, attr)
    

提交回复
热议问题