Skip to content
UPEEC

Encapsulation

Keep an object's internal state behind methods; Python uses naming conventions rather than enforced privacy.

Easy
Comments 0
class Battery:
    def __init__(self, mah):
        self._mah = mah          # "internal" by convention
        self.__serial = "X1"     # name-mangled to _Battery__serial
    def capacity(self):
        return self._mah

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.