Updating/deleting string variables in a class through the class' methods (functions) in python -
i new user python , trying update class have called player(self, name, position) through new class call api(object). in class api(), have crud approach in creating player, retrieving player, updating player, , lastly, deleting player. struggling method in update , delete function.
for update, calling self.retrieve_player[name] since method reads existing players initialized dictionary (found in init file of class api.
also, main() function @ bottom instantiating these methods creating instance of object, calling api , specified methods. e.g.:
c = api() c.create_player('buster posey', 'catcher') c.retrieve_player('james') c.update_player('buster posey', 'firstbaseman')
the code struggling outputting updated object created:
def update_player(self, name, position): updated_player = self.retrieve_player(name) updates = self.create_player(name, position) updated_player = updates return updates print('updated now!') def delete_player(self, name, position): del_player = self.retrieve_player[name] if name in del_player: del self._team[key]
for update_player, playing concept since mentality updating variables like:
a = "apple" b = "orange" x = x = b #now apple replaced value orange
e.g.:
i created player (buster posey, catcher) in c.create_player. want update player reflect new position, e.g. (buster posey, firstbaseman) through method update_player.
afterwards, want delete player altogether using method delete_player.
right now, when use current method defined update_player, created_player , updated_player... python prints: buster posey, catcher buster posey, firstbaseman instead of printing buster posey, firstbaseman.
more code:
class player(object): def __init__(self, name, position): self._name = name self._position = position class api(object): playercount = 0 managercount = 0 def __init__(self): self._team = {} #acts db self._index = 0 api.playercount += 1 api.managercount += 1 ##this crud method entity player def create_player(self, name, position): new_player = player(name, position) self._team[self._index] = new_player self._index += 1 print(name, position) def retrieve_player(self, name): # need iterate through dictionary person in self._team.itervalues(): if person._name == name: return person print(name) elif person._name != name: print('sorry, gent not exist.')
tweaked code, think looking for.
class player(object): def __init__(self, name, position): self._name = name self._position = position def __repr__(self): return "%s --> %s" % (self._name, self._position) class api(object): playercount = 0 managercount = 0 def __init__(self): self._team = {} #acts db self._index = 0 api.playercount += 1 api.managercount += 1 ##this crud method entity player def create_player(self, name, position): new_player = player(name, position) self._team[self._index] = new_player self._index += 1 print(name, position) def retrieve_player(self, name): # need iterate through dictionary index, person in self._team.items(): if person._name == name: print('found guy.. : ' + str(name)) return person print('no team member name : ' + str(name)) def update_player(self, name, position): # predicting trying update position updated_player = self.retrieve_player(name) if updated_player: temp_pos = updated_player._position updated_player._position = position print('updated %s position %s %s' %( name, str(temp_pos), str(position))) return updated_player else: print('no team member name : ' + str(name)) def delete_player(self, name, position): index, person in self._team.items(): print(index, person._name) if person._name == name , person._position == position: self._team[index] = none del person print('deleted team member : ' + str(name)) return 1 print('no team member name : ' + str(name)) team = api() team.create_player('a', 'catcher') team.create_player('b', 'firstbatsman') print(team._team) player = team.retrieve_player('a') print(player._name) team.delete_player('b', 'firstbatsman') print(team._team)
Comments
Post a Comment