调用属性getter,setter,deleter - Python

时间:2014-03-04 20:49:55

标签: python getter-setter

我收到错误“类型错误:'locationTile'在尝试调用其fdel()方法时不是可调用对象。代码:

class GamePlayLocationTiles(object):
    """The stuff needed for game play"""
    _locationTiles = []

    def locationTiles():
        doc = "property locationTiles's doc string"
        def fget(self):
            return self._locationTiles[0]
        def fset(self, value):
            self._locationTiles = value[0]
        def fdel(self):
            del self._locationTiles[0]
        return locals()  # credit: David Niergarth
    locationTiles = property(**locationTiles())

    def __init__(self):
        self.fill_location_tiles_list()

不同模块:

import game_play_model

class GamePlayController:
    """perform operations on the game_play_model"""

    def __init__(self):
        self.the_game_location_tiles = game_play_model.GamePlayLocationTiles()
        self.shuffle_location_tiles()


    def shuffle_location_tiles(self):
        self.the_game_location_tiles.locationTiles().fdel() //this line causes the error

def main():
    the_game_play_controller = GamePlayController()

if __name__ == '__main__':
    main()

尝试删除它作为使用getter,setter,deleter访问私有变量的测试。

3 个答案:

答案 0 :(得分:4)

def shuffle_location_tiles(self):
    del self.the_game_location_tiles.locationTiles

不应直接调用fdel函数。当实例尝试删除属性时,将为您调用它。

例如,

class Foo(object):
    def x():
        def fget(self):
            """I'm the 'x' property."""
            return self._x

        def fset(self, value):
            self._x = value

        def fdel(self):
            print('deleting x')
            del self._x
        return locals()
    x = property(**x())

    def __init__(self):
        self._x = None


c = Foo()
del c.x
# deleting x

self.the_game_location_tiles.locationTiles()引发错误

"Type error: 'locationTile' is not a callable

因为self.the_game_location_tiles.locationTiles调用fget并返回值self._locationTiles[0]。该值恰好不可调用。

您可以使用GamePlayLocationTiles.locationTiles访问该媒体资源,并使用

调用fdel
GamePlayLocationTiles.locationTiles.fdel(self.the_game_location_tiles)

但是当你可以使用语句

时没有理由这样做
del self.the_game_location_tiles.locationTiles

答案 1 :(得分:2)

使用property的关键是你没有直接使用这些函数,而是使用常见的Python习语来获取/设置/删除。

在这种情况下,您不会调用self.the_game_location_tiles.locationTiles().fdel(),而是调用del self.the_game_location_tiles.locationTiles,这会调用您的fdel()方法。

获取和设置同样如此:

  • self.the_game_location_tiles.locationTiles将使用您的fget
  • self.the_game_location_tiles.locationTiles = y将使用您的fset

答案 2 :(得分:0)

def locationTiles():
    doc = "property locationTiles's doc string"
    def fget(self):
        return self._locationTiles[0]
    def fset(self, value):
        self._locationTiles = value[0]
    def fdel(self):
        del self._locationTiles[0]
    return locals()  # credit: David Niergarth

locationTiles = property(**locationTiles()) # This redefines locationTiles to a variable

我看到你有一个函数和一个同名的变量。这可能会导致执行问题。当您尝试引用函数locationTiles()时,Python将其视为变量locationTiles

def shuffle_location_tiles(self):
    self.the_game_location_tiles.locationTiles().fdel() //this line causes the error