Class Definitions

Manfred Lotz ml_news at posteo.de
Sat Nov 14 04:09:32 EST 2020


On 11 Nov 2020 19:21:57 GMT
ram at zedat.fu-berlin.de (Stefan Ram) wrote:

>   In my Python course I gave the assignment to define a
>   counter class "Main" so that
> 
> counter0 = Main()
> counter1 = Main()
> counter1.count(); counter1.count(); counter1.count()
> counter1.count(); counter1.count()
> print( counter0.value )
> print( counter1.value )
> 
>   would print
> 
> 0
> 5
> 
>   .
> 
>   I expected this solution:
> 
> class Main:
>     def __init__( self ):
>         self.value = 0
>     def count( self ):
>         self.value += 1
> 
>   but a student turned in the following solution:
> 
> class Main:
>     value = 0
>     def count(self):
>         self.value += 1
> 
>   .

I am still a Python beginner and didn't even believe that the student's
solution would work. I had expected an error as the instance variable
self.value was not initialized.


-- 
Manfred




More information about the Python-list mailing list