Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

python - String ComparisonL: 'TITLE' == 'TITLE' returns False, but 'TITLE' in 'TITLE' returns True

I'm wondering about this behavior of python, i have the following function:

def create_trigger(trig_type,arg1, arg2=None):
        """
        Assumes:
             trig_type: str which is either TITLE?, DESCRIPTION?, AFTER?, BEFORE?, NOT?, AND?, OR?
        Returns:
             correspoding trigger object
        """
        if trig_type == 'TITLE?':
            print('>>entering',trig_type,'trigger')
            return TitleTrigger(arg1)
        elif trig_type ==  'DESCRIPTION?':
            print('>>entering',trig_type,'trigger')
            return DescriptionTrigger(arg1)
        elif trig_type ==  'AFTER?':
            print('>>entering',trig_type,'trigger')
            return AfterTrigger(arg1)
        elif trig_type ==  'BEFORE?':
            print('>>entering',trig_type,'trigger')
            return BeforeTrigger(arg1)
        elif trig_type ==  'NOT?':
            print('>>entering',trig_type,'trigger')
            return NotTrigger(arg1)
        elif trig_type ==  'AND?':
            print('>>entering',trig_type,'trigger')
            return AndTrigger(arg1, arg2)
        elif trig_type ==  'OR':
            print('>>entering',trig_type,'trigger')
            return OrTrigger(arg1, arg2)

in which when trig_type='TITLE' the == operator returns False, also when I use str.__eq__(). but when I use trig_type in 'TITLE' it returns True. What is causing this behavior? what is the best practice in this case

edit:

below is a minimal reproducible example

def read_trigger_config():
    """
    filename: the name of a trigger configuration file

    Returns: a list of trigger objects specified by the trigger configuration
        file.
    """
    class TitleTrigger(object):
        """
        mock class of TittleTrigger
        """
        def __init__(self, phrase):
            self.__phrase = phrase

    def create_trigger(trig_type,arg1, arg2=None):
        """
        Assumes:
             trig_type: str which is either TITLE?, DESCRIPTION?, AFTER?, BEFORE?, NOT?, AND?, OR?
        Returns:
             correspoding trigger object
        """
        
        print('checking if',trig_type,'== TITLE', trig_type == 'TITLE?')
        if trig_type == 'TITLE?':
            return TitleTrigger(arg1)
            
    lines = [['t1', 'TITLE', 'election']]
    triggers_dict = {}

    print(lines)
    for line in lines:
        if line[0] != 'ADD':
            if not line[1] in ('NOT', 'AND', 'OR'):
                triggers_dict[line[0]] = create_trigger(line[1], line[2])
            else:

                triggers_dict[line[0]]=create_trigger(line[1], triggers_dict[line[2]], triggers_dict[line[3]])
    print(triggers_dict)

read_trigger_config()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...