Re: [Tutor] pure function problem
Just home from vacation so jumping in late... "Roelof Wobben" 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 creating 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 default values) 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 know 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 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 http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pure function problem
> Date: Fri, 24 Sep 2010 06:29:03 -0400 > From: da...@ieee.org > To: rwob...@hotmail.com > CC: tutor@python.org > Subject: Re: [Tutor] pure function problem > > On 2:59 PM, Roelof Wobben wrote: >> >> >> >>> From: st...@pearwood.info >>> >>> On Fri, 24 Sep 2010 06:20:25 am Roelof Wobben wrote: >>> >>>> time =ijd() >>> [...] >>>> print time(uitkomst) >>> Why are you calling time as a function, when it is a tijd instance? >>> >>> >> >> Hello Steve, >> >> I found this in my tutorial. >> >> 13.8. Instances as return values¶ >> Functions can return instances. For example, find_center takes a Rectangle >> as an argument and returns a Point that contains the coordinates of the >> center of the Rectangle: >> def find_center(box): >> p =oint() >> p.x =ox.corner.x + box.width/2.0 >> p.y =ox.corner.y - box.height/2.0 >> return p >> To call this function, pass box as an argument and assign the result to a >> variable: >>>>> center =ind_center(box) >>>>> print_point(center) >> (50.0, 100.0) >> >> >> So i followed it but appearently not the good way. >> >> Roelof > There's a big difference between print_point() and print time(). > > print_point() in your tutorial is a function, presumably defined > someplace else. > > You used print time(), (no underscore), which uses the print statement, > and tries to call a function called time(). > > Since you defined time as an instance of your class, and didn't do > anything special, it's not callable. > > DaveA > Oke, I see it now. I have to us a function that i had to write a few questions before. Thanks everybody Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pure function problem
On 2:59 PM, Roelof Wobben wrote: From: st...@pearwood.info On Fri, 24 Sep 2010 06:20:25 am Roelof Wobben wrote: time =ijd() [...] print time(uitkomst) Why are you calling time as a function, when it is a tijd instance? Hello Steve, I found this in my tutorial. 13.8. Instances as return values¶ Functions can return instances. For example, find_center takes a Rectangle as an argument and returns a Point that contains the coordinates of the center of the Rectangle: def find_center(box): p =oint() p.x =ox.corner.x + box.width/2.0 p.y =ox.corner.y - box.height/2.0 return p To call this function, pass box as an argument and assign the result to a variable: center =ind_center(box) print_point(center) (50.0, 100.0) So i followed it but appearently not the good way. Roelof There's a big difference between print_point() and print time(). print_point() in your tutorial is a function, presumably defined someplace else. You used print time(), (no underscore), which uses the print statement, and tries to call a function called time(). Since you defined time as an instance of your class, and didn't do anything special, it's not callable. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pure function problem
On Fri, Sep 24, 2010 at 1:43 AM, Roelof Wobben wrote: > > > > >> From: st...@pearwood.info >> To: tutor@python.org >> Date: Fri, 24 Sep 2010 13:00:40 +1000 >> Subject: Re: [Tutor] pure function problem >> >> Roelof, please learn to delete unnecessarily quoted text. There's no >> need to quoted the entire discussion every time you answer. >> >> On Fri, 24 Sep 2010 06:20:25 am Roelof Wobben wrote: >> >>> time = tijd() >> [...] >>> print time(uitkomst) >> >> Why are you calling time as a function, when it is a tijd instance? >> >> >> >> >> -- >> Steven D'Aprano >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > > Hello Steve, > > I found this in my tutorial. > > 13.8. Instances as return values¶ > Functions can return instances. For example, find_center takes a Rectangle as > an argument and returns a Point that contains the coordinates of the center > of the Rectangle: > def find_center(box): > p = Point() > p.x = box.corner.x + box.width/2.0 > p.y = box.corner.y - box.height/2.0 > return p > To call this function, pass box as an argument and assign the result to a > variable: >>>> center = find_center(box) >>>> print_point(center) > (50.0, 100.0) > > > So i followed it but appearently not the good way. > > Roelof > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > If I'm understanding the question correctly(I skim alot) It looks like you're trying to use a class like a function. If you had: class tijd(object): def bob: pass then you would call bob from the class in an instance like this: aclass = tijd() calledClassFunction = aclass.bob but if you have aclass = tijd() calledClassFunction = aclass.notbob then you can't access it, because notbob is not in tijd(), therefore not in aclass, which is still the same as being tijd().bob, you just have to call the class instance before the function can be accessed. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pure function problem
> From: st...@pearwood.info > To: tutor@python.org > Date: Fri, 24 Sep 2010 13:00:40 +1000 > Subject: Re: [Tutor] pure function problem > > Roelof, please learn to delete unnecessarily quoted text. There's no > need to quoted the entire discussion every time you answer. > > On Fri, 24 Sep 2010 06:20:25 am Roelof Wobben wrote: > >> time = tijd() > [...] >> print time(uitkomst) > > Why are you calling time as a function, when it is a tijd instance? > > > > > -- > Steven D'Aprano > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Hello Steve, I found this in my tutorial. 13.8. Instances as return values¶ Functions can return instances. For example, find_center takes a Rectangle as an argument and returns a Point that contains the coordinates of the center of the Rectangle: def find_center(box): p = Point() p.x = box.corner.x + box.width/2.0 p.y = box.corner.y - box.height/2.0 return p To call this function, pass box as an argument and assign the result to a variable: >>> center = find_center(box) >>> print_point(center) (50.0, 100.0) So i followed it but appearently not the good way. Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pure function problem
Roelof, please learn to delete unnecessarily quoted text. There's no need to quoted the entire discussion every time you answer. On Fri, 24 Sep 2010 06:20:25 am Roelof Wobben wrote: > time = tijd() [...] > print time(uitkomst) Why are you calling time as a function, when it is a tijd instance? -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pure function problem
> From: rwob...@hotmail.com > To: tutor@python.org > Subject: RE: [Tutor] pure function problem > Date: Thu, 23 Sep 2010 10:15:07 + > > > >> Date: Thu, 23 Sep 2010 05:36:58 -0400 >> Subject: Re: [Tutor] pure function problem >> From: jemejo...@gmail.com >> To: tutor@python.org >> CC: rwob...@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 print time(uitkomst) AttributeError: tijd instance has no __call__ method Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pure function problem
> Date: Thu, 23 Sep 2010 05:36:58 -0400 > Subject: Re: [Tutor] pure function problem > From: jemejo...@gmail.com > To: tutor@python.org > CC: rwob...@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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pure function problem
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] pure function problem
Hello, I have to rewrite a function to a pure function. So i have 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 = tijd() uitkomst = increment(time, seconds) print uitkomst.minutes, uitkomst.seconds But now I get this error message : Traceback (most recent call last): File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 22, in 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. Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor