[Tutor] pure function problem

Alan Gauld alan.gauld at btinternet.com
Tue Sep 28 11:33:40 CEST 2010

Just home from vacation so jumping in late...

"Roelof Wobben" <rwobben at hotmail.com> wrote

> class tijd :
>    pass

Others have solved this for you but you don't appear to have picked
up on the point made by Jeremy that you are not initialising your
class's attributes.

By only creating the attributes within the function - and worse, by
creating different attributes depending on the logic flow - you are 
a huge layer of complexity for yourself. That is why you will nearly
always see a class definition have an __init__ method. It ensures that
the minimum set of attributes are there and valid (at least with 

If you carrry on defining classes as you are doing you will continue
to have these errors because the code that uses the object will not 
whether attributes exist or not. You will have to check the existence
of each atribute before you use it. That is hard work, much harder
than creating an __init__ method.

> 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

> Traceback (most recent call last):
>  File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 22, 
> in <module>
>    print uitkomst.minutes, uitkomst.seconds
> AttributeError: tijd instance has no attribute 'minutes'
> So it looks like uitkomst has no attribute minutes but uitkomst
> is a instance of tijd which has a attribute minutes.

No it doesn't if seconds <= 60.

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list