Encapsulation in Python

Dan Strohl D.Strohl at F5.com
Thu Mar 10 09:57:59 EST 2016


> I've been studying Object Oriented Theory using Java. Theoretically, all
> attributes should be private, meaning no one except the methods itself can
> access the attribute;
> 
> public class Foo {
>     private int bar;
>     ...

Why?  I mean sure, lots of them should be, but if I am doing something like:

class person:
     age = 21
     name = 'Cool Dude'

And if I am not doing anything with the information, why make it private... I am only going to crete a getter/setter that directly accesses it anyway.

Sure, if I think I am going to do something with it later, I will often "hide" it using one method or another...

class person():
    _name = ('Cool','Dude')
    
def fname(self):
        return self._name[0]

> 
> Normally in Java, we would write getters and setters to set/get the attribute
> bar. However, in Python, we normally create a class like so;
> 
> class Foo(object):
>     bar = 0
>     ...
> 
> And we usually don't write any getters/setters (though they exist in Python, I
> have not seen much projects making use of it).
> 

Lots of projects do use these, but mostly (in my experience) these are libraries that are designed to provide easy to use classes / methods to developers so that they don’t have to figure things out.  Implementing getters/setters is more complex and takes more code (and is more to troubleshoot / go wrong).  So, if you don’t need it, why not stick with something simple?

> We can easily encapsulate (data hiding) Foo's class using the '_'
> (underscore) when creating a new attribute, however, this would require all
> attributes to have a underscore.

Keep in mind that this doesn’t really hide the data, I can still access it (foo._bar = 0), even using the double underscore doesn’t actually "hide" the data, it just makes it harder to accidently override in instances.  The underscore is simply a convention that most people choose to use to suggest that _this is an internal var and should be used with caution.

> According to this answer [1], it's acceptable to to expose your attribute
> directly (Foo.bar = 0), so I wonder where the encapsulation happens in
> Python? If I can access the attribute whenever I want (with the except of
> using a underscore), what's the best way to encapsulate a class in Python?

Encapsulation can happen if the developer wants it by using any of a number of approaches (__getattr__/__setattr__, __getattribute__, __get__/__set__, property(), @property, etc...), python allows the developer to define where it makes sense to use it, and where not to.

> Why aren't most of the projects not using getters/setters and instead they
> access the variable directly?

I don’t know about "most" projects... but getters/setters (in one form or another) are used often in lots of projects... but like I mentioned above, if the project doesn’t need to encapsulate the info, why do it?  (keep in mind that I can always rewrite the class and add encapsulation later if I need to).

One note here, be careful of getting too caught up in using any single language (no matter which one) to define Object Oriented Theory, each approaches it in its own way, and each has its own benefits and challenges.  This is a perfectly valid question (and a good one), but don’t let yourself get into the trap of feeling that Java is the definitive/best/only approach to OO (or Python for that matter, or C++, or whatever!).

Dan Strohl

> 
> Regards,
> 
> Ben Mezger
> 
> [1] - http://stackoverflow.com/q/4555932



More information about the Python-list mailing list