繁体   English   中英

创建多个对象后如何访问特定的对象属性? 蟒蛇

[英]How to access specific object attributes after creating multiple objects? Python

我正在尝试通过创建管理电影商店的OO代码来练习Python编码。 我完成的代码可以正常工作,并且可以完成我想要的事情,但是在我的代码中对象Movie具有2个属性(名称,ncopies),我正在使用一个列表将每个电影名称与自己的ncopies属性相关联,想要通过使用诸如something.name之类的面向对象编程来访问这些属性,这给了我电影的名字,而不是必须维护2个相关列表并且每次我想要获取属性时都必须获取索引,任何想法?

class Movie:
    global lista_names 
    global lista_ncopies
    lista_names = [] #list of movie names
    lista_ncopies = []  #list of movie copies

    def __init__(self, name, ncopies):#keeps movie name and ncopies with same index
        self.name = name 
        self.ncopies = ncopies
        lista_names.append(self.name) #append movie name to the list
        lista_ncopies.append(self.ncopies) #append ncopies to the list

def showNumberOfCopies():
    pesquisa = input("digite o nome")#variable receives the movie name
    if pesquisa in lista_names: #checks if its in the movie names list
        indice = lista_names.index(pesquisa) #gets its index
        print ("num de copias de",pesquisa,"eh ",lista_ncopies[indice])        
    menu()   
def addMovie():
    name = input("digite o titulo") #get name of the movie
    ncopies = int(input("digite o numero de copias"))#get num of copies
    movie1 = Movie(name, ncopies) #create object 
    menu()

def updateMovie():
    pesquisa = input("type the name of the movie ")
    if pesquisa in lista_names: #checks if movie is in lista_names
        indice = lista_names.index(pesquisa) #gets the index
    else:
        opcao = input("o filme desejado nao esta em nosso acervo, deseja atualizar outro filme? s/n") #just in case the movie is not in lista_names
        if opcao == 's' or opcao == 'S':
            updateMovie()
        else:
            menu()
    opcao2 = input("update ncopies or movie name? type ncopias/nome")
    if opcao2 == 'ncopias':
        print ("the number of copies is: ",lista_ncopies[indice]) 
        ncopies_new = int(input("digite o numero de copias novo"))
        lista_ncopies[indice] = ncopies_new
        menu()
    elif opcao2 == 'nome':
        print ("name of the movie is",lista_names[indice])
        name_new = input("type the new name")
        lista_names[indice] = name_new
        print (lista_names)
        menu()            

def menu():
    opcao =  int(input('1-add filme\n2-search filme\n3-update\n4-exit'))
    if opcao == 1:
        addMovie()
    elif opcao == 2:
        showNumberOfCopies()
    elif opcao == 3:
        updateMovie()
    elif opcao == 4:
        quit    
    else:
        menu()        


menu() 

您可以改用dict object()将影片名称映射到ncopies。 就像是

class Movie:
    global m_dict 
    m_dict = {} #dict object

    def __init__(self, name, ncopies):#keeps movie name and ncopies with same index
        self.m_dict = m_dict 
        m_dict[name] = ncopies #The other way if you wish

然后您可以像M.m_dict这样M.m_dict并在dict中查找映射到副本数的电影的名称

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM