通过python中的类'方法(函数)更新/删除类中的字符串变量

时间:2015-07-22 16:19:33

标签: python function class python-2.7

我是python的新用户,我正在尝试通过新的类调用API(对象)更新我所拥有的名为Player(self,name,position)的类。在类API()中,我有创建播放器,检索播放器,更新播放器以及最后删除播放器的CRUD方法。我在更新和删除功能中遇到了这个方法。

对于更新,我调用self.retrieve_player [name],因为它是一个已经从初始化字典中读取现有播放器的方法(可在类API的 init 文件中找到。

此外,底部的main()函数通过创建对象的实例,调用API和指定的方法来实例化这些方法。例如:

c = API()
c.create_player('Buster Posey', 'Catcher')
c.retrieve_player('james')
c.update_player('Buster Posey', 'Firstbaseman')

我正在努力解决的代码是输出我创建的更新对象:

    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]

对于update_player,我正在玩概念,因为更新变量的心态如下:

a = "apple"
b = "orange"
x = a
x = b #now apple is replaced with the value orange

e.g:

我在c.create_player中创建了播放器(Buster Posey,Catcher)。我想更新这个播放器以反映他的新位置,例如(Buster Posey,FirstBaseman)通过方法update_player。

之后,我想使用delete_player方法完全删除播放器。

现在,当我使用为update_player定义的当前方法时,我得到created_player和updated_player ... python打印: 巴斯特波西,麦田守望者 Buster Posey,FirstBaseman 而不是只打印Buster Posey,FirstBaseman。

更多代码:

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 like a db
        self._index = 0
        API.playercount += 1
        API.managercount += 1

##This is the CRUD method for the 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 to iterate through the dictionary
        for person in self._team.itervalues():
            if person._name == name:
                return person
                print(name)
            elif person._name != name: 
                print('Sorry, this gent does not exist.') 

2 个答案:

答案 0 :(得分:0)

调整你的代码,我认为这就是你要找的。

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 like a db
        self._index = 0
        API.playercount += 1
        API.managercount += 1

    ##This is the CRUD method for the 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 to iterate through the dictionary
         for index, person in self._team.items():
             if person._name == name:
                 print('Found this guy.. : ' + str(name))
                 return person
         print('No team member with name : ' + str(name))

    def update_player(self, name, position): 
        # predicting that you are trying to update position
        updated_player = self.retrieve_player(name)
        if updated_player:
            temp_pos = updated_player._position
            updated_player._position = position
            print('updated %s position from %s to %s' %( name, str(temp_pos), str(position)))
            return updated_player
        else:
            print('No team member with name : ' + str(name))

    def delete_player(self, name, position):
        for index, person in self._team.items():
            print(index, person._name)
            if person._name == name and person._position == position:
                self._team[index] = None
                del person
                print('Deleted team member : ' + str(name))
                return 1
        print('No team member with 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)

答案 1 :(得分:0)

感谢gsb-eng帮助我澄清了这个过程。我的问题是在我改变位置时尝试更改名称,这需要调用retrieve_player,我需要遍历字典,而不是为以前创建的播放器实现新的位置变量和值。

这使得更容易澄清delete_player方法,该方法需要再次遍历字典并使用内置函数del。

最后,我忘了在打印变量时使用str()。

CODE:

def update_player(self, name, position): # call retrieve () player than update
    #update position of player since name doesn't change 
    updated_player = self.retrieve_player(name)
    if updated_player:
        temp_pos = updated_player._position
        updated_player._position = position
        print('Updated %s position from %s to %s' %( name, str(temp_pos), str(position)))
        return updated_player
    else:
        print('No team member with name : ' + str(name))

def delete_player(self, name, position):
    for index, person in self._team.items():
        print(index, person._name)
        if person._name == name and person._position == position:
            self._team[index] = None
            del person
            print('Deleted team member : ' + str(name))
            return 1
    print('No team member with name : ' + str(name))