0

I just caught an issue with something I wrote, and was hoping I could get a proper explanation of what was happening, and the best way to get around it. I have a fix in place, but am not sure if it's the best solution

I have a variable interval that is a string. In this scenario, let's say interval = 'Day'

Code compares this against a tuple in an if and elif expression:

if interval in ('FiscalWeekDaily','Daily'):
  ...
  ...
elif interval in ('DayHourly'):

Now I thought 'Day' in ('DayHourly') would evaluate as False but it turns out it evaluates to True. What's the exact explanation for this? I would assume that since there is only 1 element, 'Day' is being compared against that element.

So if I try 'Day' in ('DayHourly', 'whatever'), that evaluates as False because 'Day' is evaluated against each element, right?

So my fix is simply elif interval in ['DayHourly']:. Is that the proper way to go about doing this?

simplycoding
  • 2,770
  • 9
  • 46
  • 91

1 Answers1

2

Tuples with single objects in them need to have a comma, it is the comma that makes the tuple, not the parentheses.

In other words:

('DayHourly') == 'DayHourly'

You want ('DayHourly',)

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172