Files
python3-cookbook/cookbook/c09/p12_patch_class.py
2015-04-13 17:40:24 +08:00

32 lines
585 B
Python

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 使用类装饰器来增强类功能
Desc :
"""
def log_getattribute(cls):
# Get the original implementation
orig_getattribute = cls.__getattribute__
# Make a new definition
def new_getattribute(self, name):
print('getting:', name)
return orig_getattribute(self, name)
# Attach to the class and return
cls.__getattribute__ = new_getattribute
return cls
# Example use
@log_getattribute
class A:
def __init__(self, x):
self.x = x
def spam(self):
pass