Re: Python question

  • From: "Richard Dinger" <rrdinger@xxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Mon, 23 Nov 2009 14:24:53 -0800

Edward,

A few notes about your problem:

1. The signature for your isAfter method should look something like this:
 def isAfter(self, other):
then inside the body of the method you can refer to the attributes of each 
object like this:
  if self.hour > other.hour:
and so forth to make whatever comparisons you like.

2. Outside the class then, you call your isAfter function something like this 
for two objects named t1 and t2:

  t1.isAfter(t2)

3. Your problem of comparing may be simplified if you pick a better abstraction 
for your time.  Consider keeping the hour, minute and second in a container 
object such as a tuple, which can be compared directly.

4. You can also format your string representation directly from a tuple using 
the string formatting operator.
  "the time is %02d:%02d:%02d" % hr_min_sec_tuple

5. In your initialization (constructor) method, you do not check for valid 
input.  As a result negative or too large values may be entered.  Consider 
using an assertion to control input value errors:
  assert hour >= 0
  assert hours <= 24
  etc.
the assertion will halt execution if an error occurs.

6. Finally, you can overload the "<" and ">" operators in your class and then 
compare MakeTime objects directly such as:
  if t1 > t2:

Richard
----- Original Message ----- 
  From: edward 
  To: programmingblind@xxxxxxxxxxxxx 
  Sent: Saturday, November 21, 2009 4:48 PM
  Subject: Python question


  hello all 
  I am trying to check if one time comes immediately after another.  I wrote a 
method called isAfter.  the problem is how do I use it.  meaning to use a 
method in python the syntax is x.isAfter but I have two  variables not one.  so 
I need to x's.

  edward
  code starts
  class WhatTime:
   def __init__(self, hour,minute,second):
    self.second=second
    self.minute=minute
    self.hour=hour

   def isAfter(self,t1,t2):
    check=t2-t1
    return 1 in check
   def __str__(self):
    return "the time is: "+str(self.hour)+":"+str(self.minute)+" 
"+str(self.second)

  t=WhatTime(10,25,43)
  print t
  t1=WhatTime(10,12,13)
  t2=WhatTime(10,12,14)
  isAfter

GIF image

Other related posts: