试图从Django源代码理解这个函数

2024-04-20 01:48:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图理解modelforms在Django中是如何工作的,所以我将阅读源代码,并将其绘制出来。你知道吗

我被困在这个特殊的函数中表单.widgets.py不过,我想知道有没有人能解释一下。非常感谢。你知道吗

“类型”参数是什么意思?“新建”中的参数是什么?我知道这是一门新课,但这是我的理解范围。你知道吗

Here is a link到原始代码。你知道吗

class MediaDefiningClass(type):
    """
    Metaclass for classes that can have media definitions.
    """
    def __new__(mcs, name, bases, attrs):
        new_class = super(MediaDefiningClass, mcs).__new__(mcs, name, bases, attrs)

        if 'media' not in attrs:
            new_class.media = media_property(new_class)

        return new_class

Tags: django函数name表单new参数源代码绘制
2条回答

This answer explains Python metaclasses and what ^{} is

A metaclass is the class of a class. Like a class defines how an instance of the class behaves, a metaclass defines how a class behaves. A class is an instance of a metaclass.

type is the usual metaclass in Python. In case you're wondering, yes, type is itself a class, and it is its own type. ... To create your own metaclass in Python you really just want to subclass type.

该位置中的类型表示它是类的基类。因为它定义了一个“类的类型”(为提供媒体而定义的类)。你知道吗

新建是创建实例的第一步。它被称为first,负责返回类的新实例。相反,init不返回任何内容;它只负责在创建实例之后初始化实例。你知道吗

新类的参数似乎通过名称来描述新类。在NAME中指定的名称、base中的基类和attrs中为类定义的属性。但需要更多的代码才能确定。你知道吗

相关问题 更多 >