What are Python metaclasses useful for?

2020-05-15 07:42发布

What can be done with metaclasses that can't be in any other way?

Alex Martelli told that there are tasks that can't be achieved without metaclasses here Python metaclasses vs class decorators I'd like to know which are?

8条回答
迷人小祖宗
2楼-- · 2020-05-15 08:25

The first commentator states that the use of metaclasses here was 'a gemstone' that helped him track down unexpected errors that had been occurring for (many) 'days'.

查看更多
贪生不怕死
3楼-- · 2020-05-15 08:27

They are rarely needed, but come in useful in places where you want to add behavior to the basic behavior of an object -- compare Aspect Oriented Programming, or the instrumentation done in persistence frameworks like Hibernate.

For example, you may want a class which persists or logs each new object.

查看更多
登录 后发表回答