According to what I've read, Python does not strictly enforce Encapsulation from
here. As shown below, slots are commonly utilised for quicker attribute access and memory savings. However, encapsulation may be strongly maintained using slots and decorators, as seen in the following code:
class GetSet(object):
__slots__ = ["attrval"]
def __init__(self,value):
self.attrval = value
@property
def var(self):
#print 'getting the "var" attribute'
return self.attrval
@var.setter
def var(self,value):
#print 'setting the "var" attribute'
self.attrval = value
@var.deleter
def var(self):
#print 'deleting the "var" attribute'
self.attrval = None
An instance of GetSet will not have dynamic variable settings (due to slots), and the setter and getter methods will call the class's method definitions. Isn't encapsulation completely invoked?
Hi, I’m Piyush. I’m a Computer Science and Engineering graduate who is passionate about programming and technology. I found this forum in hopes of learning something valuable in programming.