[Chicago] Get/Set/Accessors in Python?

Brantley Harris deadwisdom at gmail.com
Wed Dec 5 17:29:15 CET 2012


Interesting, you're essentially ensuring a sort of type check on the
attribute.

Generally in the python world you only do this sort of checking in a
function that takes the object, which strikes me as more positive
programming, but I'm not sure why.  Possibly because I try very hard to put
less logic on the objects themselves and more in the functions/methods that
use them, because the less logic on the objects there is, the more pure
they are and therefore the more modular.

On Wed, Dec 5, 2012 at 10:22 AM, Philip Doctor <diomedestydeus at gmail.com>wrote:

> Tim,
> I agree that they get misused a lot (especially in C#) but I see it's very
> common to put validation in a set in c#.  So maybe syntactically it looks
> like you're just setting myCar.Speed to 60, but in myCar it's validating
> that Speed is not negative and it's less than MaxSpeed, etc etc.  I
> actually see this happening more and more in C# with data attributes in
> msft MVC.  They have a public get/set and then an annotation that indicates
> things like not negative or required, etc etc.  I guess the caveat being
> that without a return value, your only way to really communicate that a
> value is out of bounds is with an exception throw, so your developers need
> to be comfortable with that kind of getter/setter feedback.
>
> My only point in this ramble being that public get/set, while often cargo
> culted in and bad style, can actually still be quite useful in c# and a
> little bit better than a public variable :)
>
>
>
>
> On Wed, Dec 5, 2012 at 10:15 AM, Brian Curtin <brian at python.org> wrote:
>
>> On Wed, Dec 5, 2012 at 9:38 AM, Malcolm Newsome
>> <malcolm.newsome at gmail.com> wrote:
>> > Additionally, a separate, but perhaps related question is that I have
>> not
>> > seen public/private classes in Python.  How might this factor into the
>> whole
>> > accessor scenario?  (Or, am I trying to relate two topics that have
>> nothing
>> > to do with each other?)
>>
>> There's no notion of private classes, but you could create private
>> attributes on a class using a leading double underscore. I and most
>> others don't recommend doing this.
>>
>> class MyClass:
>>     def __private(self):
>>         ...
>>
>> Python uses "name mangling" to hide the __private method, but nothing
>> is stopping you from unmangling the name and just calling mangled
>> names.
>>
>> Check out this first paragraph for more information:
>> http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Objects
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> http://mail.python.org/mailman/listinfo/chicago
>>
>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20121205/fa506cdb/attachment.html>


More information about the Chicago mailing list