Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Getters and setters: pythonic?

  • 05-03-2010 04:04PM
    #1
    Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭


    From what I've been reading online they're consider wrong in the older articles. However, with the property() function getters and setters can be hidden behind an attribute. This imho is a good thing however I'm new to python and have had to adopt new ways of looking at code.

    The reason I think they're good is that any additional work that may be required on a attribute can be done under the bonnet so to speak which leads to good encapsulation. But what do other's think? Is it still not considered pythonic?


Comments

  • Registered Users, Registered Users 2 Posts: 134 ✭✭anton


    I suppose it depends on the task, personally I wouldn't use them too often, just for a sake of redability - seeing something like x.a = b hints it's (usually) a simple assignment withn no side effects.

    There are however, cases where customized atribute access is essential, all sorts of proxy objects - in ORMs, remote object wrappers and so on.

    So use them if you need to, but not simply because they are there.
    Evil Phil wrote: »
    From what I've been reading online they're consider wrong in the older articles. However, with the property() function getters and setters can be hidden behind an attribute. This imho is a good thing however I'm new to python and have had to adopt new ways of looking at code.

    The reason I think they're good is that any additional work that may be required on a attribute can be done under the bonnet so to speak which leads to good encapsulation. But what do other's think? Is it still not considered pythonic?


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    I'm using them in a very specific case, everything else I'm doing normally.


Advertisement