Class Variable Access and Assignment

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Nov 4 22:09:07 EST 2005


On Fri, 04 Nov 2005 16:06:45 -0800, Paul Rubin wrote:

> Steven D'Aprano <steve at REMOVETHIScyber.com.au> writes:
>> A basic usage case:
>> 
>> class Paper:
>>     size = A4
>>     def __init__(self, contents):
>>         # it makes no sense to have class contents,
>>         # so contents go straight into the instance
>>         self.contents = contents
> 
> So add:
> 
>          self.size = Paper.size
> 
> and you've removed the weirdness.  What do you gain here by inheriting?


Documents which don't care what paper size they are will automatically use
the default paper size on whatever system they are opened under. Send them
to somebody in the US, and they will use USLetter. Send to someone in
Australia, and they will use A4.

In any case, even if you conclude that there is little benefit to
inheritance in this particular example, the principle is sound:
sometimes you gain benefit by inheriting state.



-- 
Steven.




More information about the Python-list mailing list