django模型中python super函数的使用

发布于 2024-11-30 17:11:18 字数 1186 浏览 0 评论 0原文

这是我正在学习的 django 教程中的一些代码。我以前从未在 python 中遇到过 super 函数,而且这里使用它的方式与我在网上看到的示例不同。即,通常当你使用super时,你不是有多个类吗?它在最后一行:super(Snippet, self).save(force_insert,force_update) 您能否准确解释一下那里发生了什么以及编写该内容的替代方法是什么。看起来好像 save 方法在这里调用了自己?

class Snippet(models.Model):
    title = models.CharField(max_length=255)
    language = models.ForeignKey(Language)
    author = models.ForeignKey(User)
    description = models.TextField()
    description_html = models.TextField(editable=False)
    code = models.TextField()
    highlighted_code = models.TextField(editable=False)
    tags = TagField()
    pub_date = models.DateTimeField(editable=False)
    updated_date = models.DateTimeField(editable=False)

    class Meta:
        ordering = ['-pub_date']

    def __unicode__(self):
        return self.title

    def save(self, force_insert=False, force_update=False):
        if not self.id:
            self.pub_date = datetime.datetime.now()
        self.updated_date = datetime.datetime.now()
        self.description_html = markdown(self.description)
        self.highlighted_code = self.highlight()
        super(Snippet, self).save(force_insert, force_update)

Here's some code in a django tutorial that I'm going through. I've never come across the super function in python before and the way it's used here is different from the examples I've seen online. I.e., usually when you use super, don't you have multiple classes? It's in the last line: super(Snippet, self).save(force_insert, force_update)
Could you explain exactly what's going on there and what would be an alternative way to write that. It just seems like the save method is calling itself here?

class Snippet(models.Model):
    title = models.CharField(max_length=255)
    language = models.ForeignKey(Language)
    author = models.ForeignKey(User)
    description = models.TextField()
    description_html = models.TextField(editable=False)
    code = models.TextField()
    highlighted_code = models.TextField(editable=False)
    tags = TagField()
    pub_date = models.DateTimeField(editable=False)
    updated_date = models.DateTimeField(editable=False)

    class Meta:
        ordering = ['-pub_date']

    def __unicode__(self):
        return self.title

    def save(self, force_insert=False, force_update=False):
        if not self.id:
            self.pub_date = datetime.datetime.now()
        self.updated_date = datetime.datetime.now()
        self.description_html = markdown(self.description)
        self.highlighted_code = self.highlight()
        super(Snippet, self).save(force_insert, force_update)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以歌曲疗慰 2024-12-07 17:11:18

super(Snippet, self) 使 Python 查找 MRO self 类的 (即 snippet 之后列出的 next 类的 self.__class__.mro() 。它返回一个超级对象它充当该类的代理。也就是说,调用 super 对象上的方法就像调用该类上的方法一样

。 ) 调用该类的 save 方法,并将 self 绑定到第一个参数,

因此 super(Snippet, self).save(...) 不会调用Snippetsave 方法;它会调用其他类的 save 方法,很容易认为这个“其他类”是“父类”。 Snippet 的“类”或“超类”,即
models.Model,但这可能不是真的,以这种方式理解 super 是绝对错误的。 super(Snippet, self) 最终代表哪个类取决于 self,特别是其类的 MRO。

可以在 这里

super(Snippet, self) causes Python to look in the MRO of the class of self (i.e. self.__class__.mro() for the next class listed after Snippet. It returns a super object which acts as a proxy for that class. That is, calling a method on the super object acts like calling that method on the class.

super(Snippet, self).save(...) calls that class's save method, with self bound to the first argument.

So super(Snippet, self).save(...) will not call Snippet's save method; it will call some other class's save method. It is tempting to think this "other class" is the "parent class" or "superclass" of Snippet, that is,
models.Model, but that may not be true and it is absolutely wrong to apprehend super this way. Which class super(Snippet, self) ultimately represents depends on self and in particular its class's MRO.

A very good description of the MRO and super (complete with pictures!) can be found here.

浅紫色的梦幻 2024-12-07 17:11:18

我不会再次解释 unutbu 对超类的解释,但是使用以下代码可以获得相同的效果:

models.Model.save(self, force_insert, force_update)

这不是更好的编写方法,因为如果您通过在 Model 之间添加中间类来更改类继承和片段,您还必须更改此行(您很可能会忘记)。无论如何,知道这是一件好事。

我还可以补充一点,只有当您继承的类从对象扩展时, super 指令才有效,否则您会得到异常。

I won't explain again what unutbu explained on super classes, however you obtain the same effect with the following code :

models.Model.save(self, force_insert, force_update)

This is NOT a better way to write it since, if you come to change the class inheritance by adding an intermediate class between Model and Snippet you would also have to change this line (which you would most likely forget). Anyway it's a good thing to know.

I can also add that the super instruction only works if the class you inherit from extends from object, otherwise you get an exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文