1

I'm rather new to programming, especially in python, so I was following a tutorial. This is what I made from that tutorial. it has some bits cut out, because Stack Overflow requires that most of a post isn't code

class Person:
    __fname = ""
    __lname = ""

def __init__(self, fname, lname):
    self.__fname = fname
    self.__lname = lname

    def toString(self):
        return "First name is {}, last name is {}".format(self.__fname, self.__lname)

MrBrown = Person("James", "Brownson")

class Martian(Person):
    __mname = ""

    def __init__(self, fname, lname, mname):
        self.__mname = mname
        super(Martian, self).__init__(fname, lname)

    def toString(self):
        return "First name {}, middle name {}, last name {}".format(self.__fname, self.__mname, self.__lname)

MrGreen = Martian("Gack", "Snobble", "Ronk")

print(MrGreen.toString())

the error message says that the attribute '_Martian__fname' isn't there when I do This def toString(self): return "First name {}, middle name {}, last name {}".format(self.__fname, self.__mname, self.__lname). I inherited the fname and lname attributes from the Person class, but only the fname attribute has the error massage, so I'm not sure what I did wrong. Thanks in advance

  • 3
    Double underscores mangle the names and make them pseudo-private. See [What is the meaning of a single- and a double-underscore before an object name?](https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-a-single-and-a-double-underscore-before-an-object-name) – Peter Wood Oct 08 '17 at 20:18
  • Don't use double underscore names unless you really _need_ name mangling. And if you're not sure whether you need name mangling or not, you don't need it. – PM 2Ring Oct 08 '17 at 20:21

0 Answers0