1

first you have only one dictionary,everything's fine with

def getNPCName(self,ID):
    return self.npcs[ID].name

then,got another dictionary,the code become this

def getNPCName(self,ID):
    for x in (self.npcs,self.deadnpcs):
        if ID in x:
            return x[ID].name 
    return ''

i don't think this would be a good practice.

there should be some dictionary already exists, they union several dictionaries,not really merge them, but act just like one dictionary for some apis.

self.allnpclist = some_kind_of_dictionary(self.alive,self.dead)

my code should be this

def getNPCName(self,ID):
    return self.allnpclist[ID].name

i think i shouldn't write it by myself

4

1 回答 1

0

我可以考虑的一般解决方案:

npcs={...}
deadnpcs={...}
undeadnpcs={...}
npc_dicts={'npcs':npcs,'deadnpcs':deadnpcs,...}

def getNPCName(ID):
    for one in npc_dicts:
        choosen_one=getattr(npc_dicts[one],ID,None)
        if choosen_one:
            return choosen_one.name
于 2012-09-21T10:31:35.243 回答