Help With Better Design

Sells, Fred fred at adventistcare.org
Mon Jun 25 12:47:17 EDT 2007


> IMHO ... untested
> 
> class LightBulb:
> 	def __init__(self, on=False): self.IsOn = on
> 
> 	def turnOn(self): self.switchIt(True)
> 	def turnOff(self):self.switchIt(False)
> 
> 	def switchIt(self, turnon):
> 		if self.isOn==turnon: print "The Switch is Already %s" %
(["ON", "OFF"][self.isOn])
> 		else: self.isOn = turnon #or alternatively =!self.isOn
> 
> 	def __repr__(self): return ["ON", "OFF"][self.IsOn]
> 
> 
> 
> > --- apollonius2 at gmail.com wrote:
> > > 
> > > ON  = "ON"
> > > OFF = "OFF"
> > > 
> > > class LightBulb:
> > >     def __init__(self, initial_state):
> > >         self.state = initial_state
> > > 
> > >     def TurnOn(self):
> > >         if self.state == OFF:
> > >             self.state = ON
> > >         else:
> > >             print "The Bulb Is Already ON!"
> > > 
> > >     def TurnOff(self):
> > >         if self.state == ON:
> > >             self.state = OFF
> > >         else:
> > >             print "The Bulb Is Aleady OFF!"
> > > 
> > 
> > I've written code that looks a lot like that, and it's
> > a perfectly acceptable pattern IMHO.  I don't bother
> > with the variables ON and OFF, though, as they add no
> > clarity to simply using 'ON' and 'OFF' for the states.
> > 
> > 
> > > [...]
> > > The test portion of the code is actually longer than
> > > the class
> > > itself :-) 
> > 
> > That's usually a good thing!  It means your code is
> > concise, and your tests are exhaustive.  (But that
> > doesn't mean you can't also refactor your tests.)
> > 
> > 
> > 
> >        
> > ______________________________________________________________
> > ______________________
> > Be a better Globetrotter. Get better travel answers from 
> > someone who knows. Yahoo! Answers - Check it out.
> > http://answers.yahoo.com/dir/?link=list&sid=396545469
> > -- 
> > http://mail.python.org/mailman/listinfo/python-list
> > 
> 



More information about the Python-list mailing list