python - 在 Python 中覆盖嵌套类成员的更好方法是什么?

标签 python class inheritance syntax nested-class

我需要“覆盖”一些基类的嵌套类成员,同时保持其余部分不变。
这就是我所做的:

class InternGenericForm(ModelForm):                
    class Meta:
        model = Intern
        exclude = ('last_achievement', 'program',)
        widgets = {
            'name': TextInput(attrs={'placeholder': 'Имя и фамилия' }),
        }

class InternApplicationForm(InternGenericForm):
    class Meta:
        # Boilerplate code that violates DRY
        model = InternGenericForm.Meta.model
        exclude = ('is_active',) + InternGenericForm.Meta.exclude
        widgets = InternGenericForm.Meta.widgets

事实上,我希望InternApplicationForm.Meta完全InternGenericForm.Meta,除了它的exclude 元组应该再包含一项。

在 Python 中执行此操作的更漂亮的方法是什么?
我希望我不必编写像 model = InternGenericForm.Meta.model 这样也容易出错的样板代码。

最佳答案

class InternGenericForm(ModelForm):                
    class Meta:
        model = Intern
        exclude = ('last_achievement', 'program',)
        widgets = {
            'name': TextInput(attrs={'placeholder': 'Имя и фамилия' }),
        }

class InternApplicationForm(InternGenericForm):
    class Meta(InternGenericForm.Meta):
        exclude = ('is_active',) + InternGenericForm.Meta.exclude

关于python - 在 Python 中覆盖嵌套类成员的更好方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7717089/

相关文章:

python csv writer 添加额外的引号

python - 比较两个字典的键并使用与另一个字典中的键不匹配的键、值对创建一个字典 - Python

Java:如何传递代表 Enum 类的值?

css - 删除 CSS 样式表中的类

c# - xml 元素列表到公共(public)基类型的数组

C# UserControl 多重继承

python - 用父类初始化子类

python - 只能使用分区数相同的 RDD 进行 zip 错误

python - 如果某些多个关键字匹配则过滤字符串

java - 创建传递的 Class.class 参数的新实例