简体   繁体   English

使用__getitem__方法,该语句似乎无效

[英]Statement seems to have no effect, using __getitem__ method

Well, I'm a newcomer in classes world, I'm reading a python tutorial in order to understand how it works. 好吧,我是类世界的新手,我正在阅读python教程,以了解其工作原理。 and I found this: 我发现了:

class GetTest(object):             
    def __init__(self):            
        self.info = {              
            'name': 'Mauro',       
            'country': 'Argentina',
            'number': 12345812     
        }                          

    def __getitem__(self, i):      
        return self.info[i]        


foo = GetTest()                    

foo['country'] # <- Highlighted statement (Statement seems to have no effect) foo ['country']#<-突出显示的语句(该语句似乎无效)

There's no error, it just doesn't do anything. 没有错误,它什么也没做。 I think it suppose to give as output 'Argentina'. 我认为应该给出“阿根廷”作为输出。

Your final statement is wasted in your code. 您的最终声明浪费在您的代码中。 Unless you're in an interpreter shell, you need to call print : 除非您在解释器外壳中,否则需要调用print

print(foo['country'])

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

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