Property Abuse

Jean-Michel Pichavant jeanmichel at sequans.com
Wed Dec 14 11:37:15 EST 2011


Ian Kelly wrote:
> On Wed, Dec 14, 2011 at 1:28 AM, Felipe O <pip.261 at gmail.com> wrote:
>   
>> Hi All,
>> I was wondering what everyone's thought process was regarding properties.
>> Lately I find I've been binging on them and have classes with > 10
>> properties. While pylint doesn't complain (yet), it tends to be picky about
>> keeping instance attribute counts low, so I figure there's something against
>> that. How do you guys decide between using properties versus getter methods,
>> or how do you refactor them if neither?
>>     
>
> I prefer direct instance attribute access where possible*, properties
> where necessary, and methods where an argument is needed or the
> relationship is more complex than get/set/delete.
>
> * One of the strengths of Python's property system** is that you can
> switch between plain attributes and mutable properties as needed
> without breaking dependent code.  Often I see people doing this, which
> drives me nuts with its useless verbosity, when a plain instance
> attribute would have sufficed:
>
>     @property
>     def foo(self):
>         return self._foo
>
>     @foo.setter
>     def foo(self, value):
>         self._foo = value
>
> ** As opposed, for instance, to the .NET property system.  You can't
> arbitrarily switch between public member variables and public
> properties in .NET, because it breaks ABI.
>
> Cheers,
> Ian
>   
I second this opinion, plain attributes are what's required most of the 
time.

JM



More information about the Python-list mailing list