-1

I am reading python tutorial.

What relation does variable name pinA has with method name getPinA() , is it similar to automated getter setter in scala.

class BinaryGate(LogicGate):

    def __init__(self,n):
        LogicGate.__init__(self,n)

        self.pinA = None
        self.pinB = None

    def getPinA(self):
        return int(input("Enter Pin A input for gate "+ self.getLabel()+"-->"))

    def getPinB(self):
        return int(input("Enter Pin B input for gate "+ self.getLabel()+"-->"))
McGrady
  • 10,869
  • 13
  • 47
  • 69
user2230605
  • 2,390
  • 6
  • 27
  • 45

1 Answers1

0

is it similar to automated getter setter in scala.

Not really. self.pinA = None will set pinA to always be None. And any call to getPinA() will always use the input() function and never lookup the self.pinA value.

Python doesn't exactly have "encapsulation" like those JVM languages. In other words, you can always access attributes, so the concept of "getter & setter functions" isn't needed.

You can use @property, though, which acts like a way to implement a getter with an underlying value.

class BinaryGate(LogicGate):

    def __init__(self,n):
        LogicGate.__init__(self,n)
        self._pinA = None

    @property
    def pinA(self):
        if self._pinA is None:
            self._pinA = int(input("Enter Pin A input for gate "+ self.getLabel()+"-->"))
        return self._pinA

You can read more here about some Python conventions

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245