patternpythonMinor
Python Time Class definition and methods, use of __init__
Viewed 0 times
definition__init__timemethodspythonanduseclass
Problem
I've written a Time class that records the time of day and performs simple timing operations (add 2 times, convert a time object to an integer and back again,etc.) following the prompts in How to Think Like a Computer Scientist: Learning with Python.
```
class Time(object):
"""Attributes: hours, minutes, seconds"""
def __init__(self,hours,minutes,seconds):
self.hours =hours
self.minutes=minutes
self.seconds=seconds
def print_time(self):
"""prints time object as a string"""
print "%.2d:%.2d:%.2d" % (self.hours, self.minutes, self.seconds)
def _str_(self):
"""returns time object as a string"""
return "%.2d:%.2d:%.2d" % (self.hours, self.minutes, self.seconds)
def name(self,name):
"""names an instance"""
self.name=name
return self.name
def after(t1,t2):
"""checks to see which of two time objects is later"""
if t1.convert_to_seconds()t2.convert_to_seconds:
return "%s is later" %(t1.name)
else:
return "these events occur simultaneously"
def convert_to_seconds(self):
"""converts Time object to an integer(# of seconds)"""
minutes=self.hours*60+self.minutes
seconds=minutes*60+self.seconds
return seconds
def make_time(self,seconds):
"""converts from an integer to a Time object"""
self.hours=seconds/3600
seconds=seconds-self.hours*3600
self.minutes=seconds/60
seconds=seconds-self.minutes*60
self.seconds=seconds
def increment_time(self, seconds):
"""Modifier adding a given # of seconds to a time object
which has been converted to an integer(seconds); permanently
alters object"""
sum=self.convert_to_seconds()+seconds
self.make_time(sum)
return self._str_()
def add_time(self, addedTime):
"""adds 2 Time objects represented as seconds;
does not permanently modify either object"""
import copy
end_time=copy.deepcopy(self)
seconds=self.convert_to_seconds()+addedTime.convert_to_seconds()
end_tim
```
class Time(object):
"""Attributes: hours, minutes, seconds"""
def __init__(self,hours,minutes,seconds):
self.hours =hours
self.minutes=minutes
self.seconds=seconds
def print_time(self):
"""prints time object as a string"""
print "%.2d:%.2d:%.2d" % (self.hours, self.minutes, self.seconds)
def _str_(self):
"""returns time object as a string"""
return "%.2d:%.2d:%.2d" % (self.hours, self.minutes, self.seconds)
def name(self,name):
"""names an instance"""
self.name=name
return self.name
def after(t1,t2):
"""checks to see which of two time objects is later"""
if t1.convert_to_seconds()t2.convert_to_seconds:
return "%s is later" %(t1.name)
else:
return "these events occur simultaneously"
def convert_to_seconds(self):
"""converts Time object to an integer(# of seconds)"""
minutes=self.hours*60+self.minutes
seconds=minutes*60+self.seconds
return seconds
def make_time(self,seconds):
"""converts from an integer to a Time object"""
self.hours=seconds/3600
seconds=seconds-self.hours*3600
self.minutes=seconds/60
seconds=seconds-self.minutes*60
self.seconds=seconds
def increment_time(self, seconds):
"""Modifier adding a given # of seconds to a time object
which has been converted to an integer(seconds); permanently
alters object"""
sum=self.convert_to_seconds()+seconds
self.make_time(sum)
return self._str_()
def add_time(self, addedTime):
"""adds 2 Time objects represented as seconds;
does not permanently modify either object"""
import copy
end_time=copy.deepcopy(self)
seconds=self.convert_to_seconds()+addedTime.convert_to_seconds()
end_tim
Solution
It is perfectly fine to implement an
Alternatives are (1) as you've already implied: removal or (2) giving these variables default values.
For example:
With the above you are giving the user the option to define these later or now with no penalty either way. Just keep the following simple philosophy of Python in mind:
Easier to ask for forgiveness than permission
(From the Python Glossary)
I don't really see a point in naming instances. Do you have a rationale for that? Also, if you choose to name them, I think that returning said name after setting is an unexpected behavior and gives the
In your
Overall, everything looks pretty good. I hope this was somewhat helpful, if you have any questions be sure to comment.
__init__ method in this case. I think the only thing you should note is, by the way it's defined, the Time class forces the programmer to give values for hours, minutes and seconds to define a Time object. So, with that constraint in mind, it's really up to you as to whether this is an advantage or disadvantage. Do you want to force the programmer (most likely yourself) to enter these values here? Or should you allow him to first construct an object and then define them later? This is your decision; I don't think Pythoneers will try to sway you one way or another.Alternatives are (1) as you've already implied: removal or (2) giving these variables default values.
For example:
def __init__(self,hours=None,minutes=None,seconds=None):
self.hours =hours
self.minutes=minutes
self.seconds=secondsWith the above you are giving the user the option to define these later or now with no penalty either way. Just keep the following simple philosophy of Python in mind:
Easier to ask for forgiveness than permission
(From the Python Glossary)
I don't really see a point in naming instances. Do you have a rationale for that? Also, if you choose to name them, I think that returning said name after setting is an unexpected behavior and gives the
name function more responsibility than it needs.In your
add_time method, I would suggest constructing a new Time object using the values of the self object and then returning that with the incrementation. And, in general, import statements occur at the top of the Python module, unless it is a really special case.Overall, everything looks pretty good. I hope this was somewhat helpful, if you have any questions be sure to comment.
Code Snippets
def __init__(self,hours=None,minutes=None,seconds=None):
self.hours =hours
self.minutes=minutes
self.seconds=secondsContext
StackExchange Code Review Q#19502, answer score: 3
Revisions (0)
No revisions yet.