Switch statements again

Steven Scott bodoni26 at resnet.gatech.edu
Wed Jan 15 17:46:07 EST 2003


I've been reading the claims about doing things a more elegant way in python
when it comes to switch statements, using if-elif-else and/or dictionaries
of function pointers.  the following can be done with if-elif-else (and
that's how I did it), but it sure is ugly.  my question is simply "what's
the best way to do this in python?"

    switch (calDayOfWeekMask)
      {
	  <snip>
	
      case MASK_SUNDAY:
		dayOfWeek--;		// and fall thru
      case MASK_MONDAY:
		dayOfWeek--;		// and fall thru
      case MASK_TUESDAY:
		dayOfWeek--;		// and fall thru
      case MASK_WEDNESDAY:
		dayOfWeek--;		// and fall thru
      case MASK_THURSDAY:
		dayOfWeek--;		// and fall thru
      case MASK_FRIDAY:
		dayOfWeek--;		// and fall thru
      case MASK_SATURDAY:
		if (calWeekNumber == LAST)
		  {
		    occurrence.setDay(occurrence.getDaysInMonth());
		    occurrence.addDays(- ((7 - dayOfWeek
					   + occurrence.getDayOfWeek()) %
7));
		  }
		else
		  {
		    occurrence.setDay(1 + ((calWeekNumber - 1) * 7));
		    occurrence.addDays((dayOfWeek + 7 -
occurrence.getDayOfWeek()) % 7);
		  }
		break;
		
      default:
		return FAIL;
      }


-- 
Steven Scott [Bodoni26 at resnet.gatech.edu]
Don't give up fighting, 'til nothing else stands in your way..Don't
give up talking, until there's nothing left to say....But no matter





More information about the Python-list mailing list