Python super and setting parent class property

我怕爱的太早我们不能终老 提交于 2019-11-27 02:08:00

问题


I'm having a really strange problem with Python super() and inheritance and properties. First, the code:

#!/usr/bin/env python3

import pyglet
import pygame

class Sprite(pyglet.sprite.Sprite):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.rect = pygame.Rect(0, 0, self.width, self.height)
        self.rect.center = self.x, self.y

    @property
    def x(self):
        return super().x

    @x.setter
    def x(self, value):
        super(Sprite, self.__class__).x.fset(self, value)
        self.rect.centerx = value

    @property
    def y(self):
        return super().y

    @y.setter
    def y(self, value):
        super(Sprite, self.__class__).y.fset(self, value)
        self.rect.centery = value

This works fine. However, what I want (what seems Pythonic to me)

#super(Sprite, self.__class__).x.fset(self, value)
super().x = value

doesn't work even though

super().x

gets the value fine. x in this case is a property of the superclass with both fset and fget defined. So why doesn't it work?


回答1:


I was trying to find the correct language to back up why this behavior is the way it is, so as not to give you a "because it just is" answer... But it seems this question has been asked more than once, and that it boils down to the behavior of super(). You can see a 2010 discussion about this exact behavior here: http://mail.python.org/pipermail/python-dev/2010-April/099672.html

Ultimately, it really does just come down to super() calls only letting you access getters directly, and not setters. Setters must be accessed via fset() or __set__(). It is probably easiest explained as "super() functionality just doesn't support it". It will resolve the property functionality of a "get" operation, not the setter in a left handed assignment, in the "set" operation (hence the fset() method call). As you can see from the date of this discussion thread, its obviously been this way since the introduction of super().

Maybe someone else has a more specifically technical reason, but frankly I'm not sure it even matters. If its not supported, thats pretty much a good enough reason.



来源:https://stackoverflow.com/questions/10810369/python-super-and-setting-parent-class-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!