簡體   English   中英

調用屬性獲取,設置,刪除器-Python

[英]Call property getter, setter, deleter - Python

我收到錯誤消息“類型錯誤:嘗試調用其fdel()方法時,'locationTile'不是可調用對象。代碼:

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訪問私有變量的測試。

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訪問屬性本身,並使用以下GamePlayLocationTiles.locationTiles調用fdel

GamePlayLocationTiles.locationTiles.fdel(self.the_game_location_tiles)

但是當您僅使用語句時就沒有理由這樣做

del self.the_game_location_tiles.locationTiles

使用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
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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM