[Tutor] Creating a pojo in python

Alan Gauld alan.gauld at btinternet.com
Mon Feb 9 10:20:55 CET 2015


On 09/02/15 07:20, rakesh sharma wrote:
> How can one create a POJO in python.I mean a class like this
> class A {   private a;   private b;   public getA() {       return a;   }   public getB() {      return b   }}
> I tried creating class in python but the variables were accessible as public data members.

First you have to answer the question "why do you want
to hide your data attributes, only to expose them via
getters?"

If you have a good and legitimate reason for doing that
then Python has mechanisms for doing it, but they are
quite rarely used, since there are few cases where
you really need to do that.

Python's style says that we are all adults and can be trusted
to behave responsibly so we don't need to make all attributes
private. Of course you should still design your classes after
good OOP principles so data attributes are mainly there to
support object behaviour. As such, most object access will
be via methods. But that should be true in Java too.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list