django模型中python super函数的使用
这是我正在学习的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
super(Snippet, self)
使 Python 查找 MRO self 类的 (即snippet
之后列出的 next 类的self.__class__.mro()
。它返回一个超级
对象它充当该类的代理。也就是说,调用 super 对象上的方法就像调用该类上的方法一样。 ) 调用该类的
save
方法,并将self
绑定到第一个参数,因此
super(Snippet, self).save(...)
不会调用Snippet
的save
方法;它会调用其他类的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 afterSnippet
. It returns asuper
object which acts as a proxy for that class. That is, calling a method on thesuper
object acts like calling that method on the class.super(Snippet, self).save(...)
calls that class'ssave
method, withself
bound to the first argument.So
super(Snippet, self).save(...)
will not callSnippet
'ssave
method; it will call some other class'ssave
method. It is tempting to think this "other class" is the "parent class" or "superclass" ofSnippet
, that is,models.Model
, but that may not be true and it is absolutely wrong to apprehendsuper
this way. Which classsuper(Snippet, self)
ultimately represents depends onself
and in particular its class's MRO.A very good description of the
MRO
andsuper
(complete with pictures!) can be found here.我不会再次解释 unutbu 对超类的解释,但是使用以下代码可以获得相同的效果:
这不是更好的编写方法,因为如果您通过在 Model 之间添加中间类来更改类继承和片段,您还必须更改此行(您很可能会忘记)。无论如何,知道这是一件好事。
我还可以补充一点,只有当您继承的类从对象扩展时, super 指令才有效,否则您会得到异常。
I won't explain again what unutbu explained on super classes, however you obtain the same effect with the following code :
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.