Reputation: 1073
I'm getting the error "Type error: 'locationTile' is not a callable object when trying to call its fdel() method. Code:
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()
different module:
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()
Just trying to delete it as a test on accessing a private variable with a getter, setter, deleter.
Upvotes: 1
Views: 3441
Reputation: 880509
def shuffle_location_tiles(self):
del self.the_game_location_tiles.locationTiles
The fdel
function should not be called directly. It will be called for you when the instance tries to delete the property.
For example,
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()
raises the error
"Type error: 'locationTile' is not a callable
because self.the_game_location_tiles.locationTiles
calls fget
and returns the value, self._locationTiles[0]
. That value happens to not be callable.
You could access the property itself using GamePlayLocationTiles.locationTiles
, and call fdel
with
GamePlayLocationTiles.locationTiles.fdel(self.the_game_location_tiles)
but there is no reason to do that when you can just use the statement
del self.the_game_location_tiles.locationTiles
Upvotes: 4
Reputation: 1143
The point of using property
is that you're not using the functions directly, but use common Python idioms to get/set/delete.
In this case, you would not be calling self.the_game_location_tiles.locationTiles().fdel()
, but instead del self.the_game_location_tiles.locationTiles
, which would call your fdel()
method.
The same goes for getting and setting:
self.the_game_location_tiles.locationTiles
will use your fget
.self.the_game_location_tiles.locationTiles = y
will use your fset
.Upvotes: 3
Reputation: 4425
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
I see that you have a function and a variable both given the same name. This could cause problems with the execution. When you try to reference the function locationTiles(), Python sees it as the variable locationTiles
def shuffle_location_tiles(self):
self.the_game_location_tiles.locationTiles().fdel() //this line causes the error
Upvotes: 0