On Sat Sep 16, 2006 at 20:23:42 +1000, Philip Greggs wrote:
>Hi,
>
>
>>Anyway, back to the arithmetic thing:
>>
>>[pan: pan 1018]$ cat a_bit_stupid.rb
>>class Time
>>  def plus *nums
>>    nums.inject(self) {|sum,num| sum+num}
>>  end
>>end
>>class Fixnum
>>  def seconds
>>    self
>>  end
>>  def minutes
>>    self*60
>>  end
>>  def hours
>>    self*3600
>>  end
>>end
>>
>
>In Python, what is the equivalent for the above ?

import datetime

class Time(datetime.datetime):
    def __add__(self, other):
        return datetime.datetime.__add__(self, datetime.timedelta(0, other, 0))

class Fixnum(int):
    def seconds(self):
        return self
    
    def minutes(self):
        return self*60
    
    def hours(self):
        return self*3600


t = Time.now()
print t
print t + Fixnum(30).seconds()
print t + Fixnum(1).minutes()

... of course this is totally un-pythonic, so you just wouldn't do it.

Benno
_______________________________________________
coders mailing list
coders@slug.org.au
http://lists.slug.org.au/listinfo/coders

Reply via email to