2014-03-04 1 views
1

내가 오류 "입력 오류 받고 있어요 :. 그 프델() 메서드를 호출하려고 할 때 'locationTile'는 호출 대상이 아닌 코드 : 다른통화 속성 게터, 세터, Deleter가 - 파이썬

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() 

그냥 게터, 세터, Deleter가있는 전용 변수 액세스에 대한 테스트로 삭제하려고

답변

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

직접 호출 할 수 없습니다해야하는 fdel 기능으로보고있다. 인스턴스가 속성을 삭제하려고 할 때 호출됩니다. self.the_game_location_tiles.locationTiles 전화 fget하고 값을 반환하기 때문에 예를 들어

,

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()self._locationTiles[0], 오류를

"Type error: 'locationTile' is not a callable 

를 발생시킵니다. 이 값은 호출 할 수없는 경우가 발생합니다.

당신은 GamePlayLocationTiles.locationTiles를 사용하여 속성 자체를 액세스하고

GamePlayLocationTiles.locationTiles.fdel(self.the_game_location_tiles) 

fdel 전화 만 할 이유가 없습니다 수있는 당신은 문

del self.the_game_location_tiles.locationTiles 
+0

답변을 주셔서 감사합니다. getter, setter, deleter에서 fdel 등등의 코드에서 [0]을 삭제하려고 했으므로 한 멤버 만이 아니라 전체 목록을 삭제하고 _locationTiles가 읽기 전용이라는 fdel() 메서드 내부에서 오류가 발생했습니다. . 그래서 나는 그것을 얻을 수 있었지만 그것을 지울 수는 없었다. 왜 그런지 알아? – user3164083

+0

이것은 setter, getter, deleter가있는 목록입니다. 그렇게 목록을 작성할 수는 없습니다. 이제 작동합니다. : 나는 그것을 변경 '데프 프델 (자기) : 이 self._locationTiles 델 [0 : LEN (self._locationTiles)]' 감사합니다 :) – user3164083

+0

아,이 인스턴스가 클래스 속성을 삭제할 수 없습니다 단순히 , (최소한 그런 식으로.) – unutbu

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를 참조 할 때, 파이썬은 변수 locationTiles

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

감사합니다. http://code.activestate.com/recipes/205183-a-tidy-property-idiom/ 여기에서 배웠습니다 – user3164083

2

사용하는 포인트를 사용할 수있는 경우 property은 함수를 직접 사용하지 않지만 일반적인 파이썬 관용구를 사용하여 가져 오기/설정/삭제합니다.

이 경우 메서드를 호출하는 self.the_game_location_tiles.locationTiles().fdel() 대신 del self.the_game_location_tiles.locationTiles을 호출 할 수 있습니다.

동일 얻기 및 설정 간다 :

  • self.the_game_location_tiles.locationTilesfget을 사용합니다.
  • self.the_game_location_tiles.locationTiles = yfset을 사용합니다.