Class Variable Access and Assignment

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sat Nov 5 04:56:41 CET 2005


On Fri, 04 Nov 2005 13:52:22 -0800, Paul Rubin wrote:

> Steven D'Aprano <steve at REMOVETHIScyber.com.au> writes:
>> Follow the logical implications of this proposed behaviour.
>> 
>> class Game:
>>     current_level = 1
>>     # by default, games start at level one  
> 
> That's bogus.  Initialize the current level in the __init__ method
> where it belongs.

It might be bogus to you, but it isn't to me. I prefer to delay setting
instance attributes until they are needed.

It also allows you to do something like this:

class ExpertGame(Game):
    current_level = 100


and then use ExpertGame anywhere you would have used Game with no problems.

Yes, there are other ways to do this. I won't say they are wrong, but I
don't believe they are better.

-- 
Steven.




More information about the Python-list mailing list