文章来自《Python cookbook》.

翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!

::-- KenLai [DateTime(2006-03-18T09:40:36Z)] TableOfContents

描述

...

问题 Problem

You want to add a method to a class at an arbitrary point in your code for highly dynamic customization.

解决 Solution

The best way to perform this task works for both classic and new-style classes:

   1 def funcToMethod(func, clas, method_name=None):
   2     setattr(clas, method_name or func._ _name_ _, func)

If a method of the specified name already exists in the class, funcToMethod replaces it with the new implementation.

讨论 Discussion

...

参考 See Also