Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Getters and setters: pythonic?

Options
  • 05-03-2010 4:04pm
    #1
    Registered Users 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 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 Posts: 7,468 ✭✭✭Evil Phil


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


Advertisement