What are some (concrete) use-cases for metaclasses?

前端 未结 19 1123
悲&欢浪女
悲&欢浪女 2020-12-07 06:51

I have a friend who likes to use metaclasses, and regularly offers them as a solution.

I am of the mind that you almost never need to use metaclasses. Why? because I

19条回答
  •  庸人自扰
    2020-12-07 07:52

    You never absolutely need to use a metaclass, since you can always construct a class that does what you want using inheritance or aggregation of the class you want to modify.

    That said, it can be very handy in Smalltalk and Ruby to be able to modify an existing class, but Python doesn't like to do that directly.

    There's an excellent DeveloperWorks article on metaclassing in Python that might help. The Wikipedia article is also pretty good.

提交回复
热议问题