나는 super()
의 작동 방식을 이해하려고합니다. 나는 그것이 무엇을하는지 이해하지만 나는 뒤에서 무슨 일이 벌어지는 지에 대한 메커니즘을 이해하지 못합니다.super()가 메소드의 처음이나 끝에서 호출되는 것과 다른 점은 무엇입니까?
class b(a):
def __init__(self, name, age):
self.name=name
self.age=age
super(b, self).__init__(name, age)
과 : 내가 정확히 이해하지 못하는 한 가지의 차이점은 어쩌면이 두 가지 예에서 차이가 없습니다
class b(a):
def __init__(self, name, age):
super(b, self).__init__(name, age)
self.name=name
self.age=age
을,하지만 난 다른 상황이 알고 곳의 위치 super()
중요합니다. 예를 들어, 장고에 대한 도움이 필요했던이 장고 메서드는 아래쪽이 아닌 if 문 위로 super()
을 이동하라는 지시를 받았습니다. 왜 이것이 중요한지 알고 싶습니다.
class Mymodel(models.Model):
photo = models.ImageField(upload_to="...", blank=True)
def save(self, *args, **kwargs):
image_resized = kwargs.pop('image_resized',False)
super(Mymodel, self).save(*args, **kwargs)
if self.photo and image_resized:
basewidth = 300
filename = self.get_source_filename()
image = Image.open(filename)
wpercent = (basewidth/float(image.size[0]))
hsize = int((float(image.size[1])*float(wpercent)))
img = image.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
self.photo = img
self.save(image_resized = True)
생각해보십시오. 당신의 코드가 슈퍼 클래스'__init__'에서만 초기화되는 인스턴스 속성에 의존한다면 어떻게 될까요? 일찍 전화하면할수록 좋습니다. 또한 '슈퍼'입니다. – jonrsharpe
마지막으로 호출하고 싶은 상황이 있습니다. 보통'__new__'에서와 같습니다. –
** 단지 부모 클래스에서 찾을 수있는 메소드 인 ** 일 뿐이지 만 ** 메소드 호출 **입니다. 그러나 * 당신이 그것을 부를 때 다른 어떤 방법을 부르는 것과 같은 영향을 미친다. 물건이 생겼어. 전화하지 않으면 그 일은 일어나지 않습니다. –