开发者

How to I modify a class of a library to get it to use my extension of another library class?

Short story:

I want to make slight changes to the behavior of a MainClass, and a HelperClass on which it depends, in a popular library. I can easily extend both by subclassing, but how do I tell the top-level class to use my extended version of the helper class?

The MainClass generates instances of HelperClass via simple instantiation (e.g., helperItem = HelperClass()) and from yield(). HelperClass is coded in the same module as MainClass.

Longer:

For a Django Form, I want to generate a nested dictionary ho开发者_JAVA百科lding the data specifying the HTML display of that form. Django Form objects generate HTML by wrapping Field objects in a BoundField class, which has methods to reach into the Field datastructures to generate the appropriate HTML strings.

I want to:

  • extend / modify Form to use my extended version of BoundField, and
  • extend Form to add a method that cycles through its fields calling getHtmlSpec() on each.

(Here I'm glossing over important Django implementation details, like whether to extend Form or BaseForm, and whether to extend BoundField or / and Input widgets.)

Obviously I could do this by extending Form to reach in to 'fields' and generate this stuff, and that might be better design. But this seems more elegant, and I'm curious even if it isn't the best approach.


That's some ugly way to design a class, and I guess there's an even uglier way to hack around it:

from django.forms import forms

class MyBoundField(object):
    pass

forms.BoundField = MyBoundField
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜