[Baypiggies] What's the "Pythonic" way to access class attributes?

wescpy at gmail.com wescpy at gmail.com
Tue Oct 11 22:54:44 EDT 2016


   - Python is not Java
   <http://dirtsimple.org/2004/12/python-is-not-java.html>
   - use Foo.bar = 10
   - Yep, there's no private, protected, nor friend in Python
   - Use __slots__ if you want some to set some protection (although it
   wasn't created for security)
   - Use properties to implement custom set, get(, and del) behavior (h/t
   to Tony)
   - Use descriptors if properties don't do the job (properties ==
   descriptor shortcut)

Cheers,
--Wesley

On Tue, Oct 11, 2016 at 5:07 PM, Tony Cappellini <cappy2112 at gmail.com>
wrote:

> I'm surprise that properties wasn't brought up.
>
> https://docs.python.org/3/howto/descriptor.html#properties
>
> http://www.python-course.eu/python3_properties.php
>
>
> On Tue, Oct 11, 2016 at 2:29 AM, Braun Brelin <bbrelin at gmail.com> wrote:
> > Hello all,
> >
> > I just wanted to get the list's opinion on something.
> >
> > Since Python doesn't really have the concept of private and protected
> > attribute access protections a la Java or C++ (Yes, I know you can use
> > the __ and _ prefixes to declare something private or protected in
> > Python, but that really only does name-mangling on the attribute name
> > rather than explicitly disallow access to the attribute from outside the
> > class), is it better to still access the attribute directly a la
> > Foo.bar = 10 or use a setter/getter approach like Foo.setBar(10)?
> > It seems to me that option one is better, especially if I can use a
> > descriptor with the attribute.
> >
> > Any thoughts?
> >
> > Thanks,
> >
> > Braun Brelin
> >
> >
> > _______________________________________________
> > Baypiggies mailing list
> > Baypiggies at python.org
> > To change your subscription options or unsubscribe:
> > https://mail.python.org/mailman/listinfo/baypiggies
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> https://mail.python.org/mailman/listinfo/baypiggies
>



-- 
"A computer never does what you want... only what you tell it." ~Wesley Chun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20161011/5dce3525/attachment.html>


More information about the Baypiggies mailing list