Accessors in Python (getters and setters)
Steve Holden
steve at holdenweb.com
Thu Jul 20 03:02:32 EDT 2006
Dennis Lee Bieber wrote:
> On 19 Jul 2006 22:38:17 -0700, "mystilleef" <mystilleef at gmail.com>
> declaimed the following in comp.lang.python:
>
>
>>permitted should be between an object and its mediator. Messages are
>>passed through the system via signals or events or established
>>protocols. What are the benefits? Well I can change the implementation
>>of any object at will without breaking the whole system. Or I can even
>
>
> Python permits that using properties... But one doesn't have to code
> the properties from the get-go... Development can be done with direct
> attribute access, and if it is determined that some sort of conditioning
> logic is needed, it can be implemented WITHOUT CHANGING THE API.
>
> This IS an "established protocol" in Python.
>
>>High coupling is bad. Exposing critical states of an object is bad. Any
>>decent computer science book will tell you that.
>>
>
> In Python, using, if needed, properties, this is transparent... The
> user of the object doesn't know, or need to know, if it is state or
> behavior -- all it sees is that it supplies a value for something,
> and/or retrieves a value for something.
>
>>Obviously if I want to control access to an attribute, it means I don't
>>want it altered or I want to perform pre/post conditional computations
>>before/after the attribute is accessed. There's a reason it is called
>>access control.
>>
>
> Python properties support this, no need to have the user of the
> object explicitly call a getter or setter...
>
>
>
>>changing states you never bothered to hide to begin with, then the
>>wisdom behind careful upfront planning begins to make sense.
>>
>
> If it isn't part of the documented API, it is the user's fault. That
> applies to all software...
I'm this thread represents proof tht you can lead a horse to water but
you can't make him drink - especially when he thinks the water is
Kool-Aid, and that attribute APIs can be abused in ways that
method-based ones can't.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
More information about the Python-list
mailing list