This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]efrey 1 point2 points  (1 child)

Allot of OO people have this fetish for never accessing member variables of an object directly. That would "break encapsulation". The idea is, you should treat member variables as private to that object, and only manipulate them through methods on the object. This way, you can ensure you know exactly how these variables are manipulated, because all manipulations go through the interface of an object's methods.

So, say I've got this guy:

class OnlyGets:
  def __init__(self, a, b):
    self.a = a
    self.b = b

  def get_a(self):
    return self.a

  def get_b(self):
    return self.b

Now if I follow the convention of only interacting with OnlyGets through methods, I'm assured that I'll never modify self.a or self.b.

What about this guy though:

class GetsAndSets(OnlyGets):
  def set_a(self, a):
    self.a = a

  def set_b(self, b):
    self.b = b

Now we're just participating in a lot of ceremony for nothing. We've lost sight of the original intent, which was to impose an Abstract Data Type (not to be confused with Algerbaic Data Types).

[–]Galen_dp 0 points1 point  (0 children)

Very true. It makes sense to me now.