提问人:Alon Parag 提问时间:1/6/2021 最后编辑:Alon Parag 更新时间:1/8/2021 访问量:54
字符串比较L: 'TITLE' == 'TITLE' 返回 False,但 'TITLE' 中的 'TITLE' 返回 True
String ComparisonL: 'TITLE' == 'TITLE' returns False, but 'TITLE' in 'TITLE' returns True
问:
我想知道python的这种行为, 我有以下功能:
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)
其中当运算符返回时,当我使用 .但是当我使用它时,它会返回.
是什么导致了这种行为?在这种情况下,最佳实践是什么trig_type='TITLE'
==
False
str.__eq__()
trig_type in 'TITLE'
True
编辑:
下面是一个最小的可重复示例
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()
答: 暂无答案
评论
trig_type
'TITLE '
'TITLE\n'
print(repr(trig_type))
'TITLE'