[Tutor] pure function problem

Roelof Wobben rwobben at hotmail.com
Thu Sep 23 22:20:25 CEST 2010




________________________________
> From: rwobben at hotmail.com
> To: tutor at python.org
> Subject: RE: [Tutor] pure function problem
> Date: Thu, 23 Sep 2010 10:15:07 +0000
>
>
>
>> Date: Thu, 23 Sep 2010 05:36:58 -0400
>> Subject: Re: [Tutor] pure function problem
>> From: jemejones at gmail.com
>> To: tutor at python.org
>> CC: rwobben at hotmail.com
>>
>> The problem is that your class definition doesn't do anything to
>> explicitly set those attributes.
>>
>> On Thu, Sep 23, 2010 at 4:58 AM, Roelof Wobben wrote:
>>
>>> class tijd :
>>> pass
>>
>> You're not doing any explicit setting of attributes at the class level.
>>
>>
>>> time = tijd()
>>> time.hour = 20
>>> time.minutes = 20
>>> time.seconds = 20
>>
>> You set them on this instance.
>>
>>> seconds = 20
>>> uitkomst = tijd()
>>
>> But not on this one.
>>
>> What you probably want to do is something like this:
>>
>> class tijd(object):
>> def __init__(self):
>> self.hour = 20
>> self.minutes = 20
>> self.seconds = 20
>>
>> Or if you prefer to set these when you create the instance, you can
>> pass in values like this:
>>
>> class tijd(object):
>> def __init__(self, hour=20, minutes=20, seconds=20):
>> self.hour = hour
>> self.minutes = minutes
>> self.seconds = seconds
>>
>> I noticed something odd just a sec ago. You have this:
>>> uitkomst = tijd()
>>> uitkomst = increment(time, seconds)
>>> print uitkomst.minutes, uitkomst.seconds
>>
>> You're creating a tijd instance, binding uitkomst to it, then
>> overwriting that instance with what you return from increment().
>>
>> Anyway, hth.
>>
>> - jmj
>
>
> Correct,
>
> I try to find a way to solve this error message.
>
> Roelof
>

 
Oke, 
 
I changed everything to this : 
 
class tijd :
    pass
 
def increment(time, seconds):
    sum = tijd()
    sum.seconds = time.seconds + seconds 
    
    if sum.seconds> 60 :
        minutes, seconds = divmod(sum.seconds, 60)
        sum.seconds = seconds 
        sum.minutes = time.minutes + minutes
    return sum
 
time = tijd()
time.hour = 20 
time.minutes = 20
time.seconds = 20 
seconds = 20
uitkomst = increment(time, seconds)
print time(uitkomst)
 
But now Im getting this error message :
 
Traceback (most recent call last):
  File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 21, in <module>
    print time(uitkomst)
AttributeError: tijd instance has no __call__ method
 
 
 
Roelof
  		 	   		  


More information about the Tutor mailing list