Private attribute

Ken Starks straton at lampsacos.demon.co.uk
Mon Aug 25 16:44:49 EDT 2008


André wrote:
> On Aug 25, 3:47 pm, Ken Starks <stra... at lampsacos.demon.co.uk> wrote:
>> I have a class with an attribute called 'gridsize' and I want
>> a derived class to force and keep it at 0.8 (representing 8mm).
>>
>> Is this a correct, or the most pythonic approach?
>>
>> ####################
>>
>>      def __getattr__(self,attrname):
>>          if attrname == 'gridsize':
>>              return 0.8
>>
>>      def __setattr__(self,attrname,value):
>>          if attrname == 'gridsize':
>>              pass
>>          else:
>>              self.__dict__[attrname]=value
>>
>> #####################
>>
>> Cheers,
>> Ken.
> 
> Why not make gridsize a property with no set method?
> 
> André

Thanks for the suggestion, André.

I admit I haven't used properties, and had to look them up.
Pretty cool indeed !  But an extra unnecessary level of
complexity for my needs here, I feel.

They are _certainly_ going to become part of my
Python toolkit.


Cheers,
Ken.



More information about the Python-list mailing list