jeudi 22 juin 2017

Using "private" variable + method v.s. "public" instance variable

I often see this pattern:

class Foo:
    def __init__(self, bar):
        self._bar = bar

    def bar(self):
        return _bar

Why would that be preferable to this?

 class Foo:
     def __init__(self, bar)
         self.bar = bar

Aucun commentaire:

Enregistrer un commentaire