Python “if X == Y and Z” syntax

前端 未结 7 2128
温柔的废话
温柔的废话 2020-12-06 17:10

Does this:

if key == \"name\" and item:

mean the same as this:

if key == \"name\" and if key == \"item\":

相关标签:
7条回答
  • 2020-12-06 17:31

    If you look at the code for example 5.14:

    def __setitem__(self, key, item):         
        if key == "name" and item:            
            self.__parse(item)                
        FileInfo.__setitem__(self, key, item)
    

    item is a variable, similar to how key is.

    It can be used in the if statement if it evaluates into either true or false.

    eg.

    happy = True
    name = "Peter"
    if name == "Peter" and happy:
        print name + " is happy!"
    
    0 讨论(0)
提交回复
热议问题