Question

I'm doing an exercise out of the Python book that involves creating a class and a subclass. I am getting the following error when I try to run the program: AttributeError: 'Customer' object has no attribute 'name', when it tries to go through this bit of code: self.name.append(name)

As this is my first time dealing with classes and objects in Python, I'm sure I am making some overt mistake somewhere, but I can't seem to figure it out. I've looked over the documentation for creating classes and writing member functions, and it looks correct, but it is obviously not. I want the Customer subclass to inherit the name, address, and telephone attributes from the Person superclass, but it doesn't seem to be doing so?

Here is my code:

class Person:

    def __init__(self):
        self.name = None
        self.address = None
        self.telephone = None

    def changeName(self, name):
        self.name.append(name)

    def changeAddress(self, address):
        self.address.append(address)

    def changeTelephone(self, telephone):
        self.telephone.append(telephone)

class Customer(Person):

    def __init__(self):
        self.customerNumber = None
        self.onMailingList = False

    def changeCustomerNumber(self, customerNumber):
        self.customerNumber.append(customerNumber)

    def changeOnMailingList():
        if onMailingList == False:
            onMailingList == True
        else:
            onMailingList == False

def main():

    customer1 = Customer()

    name = 'Bob Smith'
    address = '123 Somewhere Road'
    telephone = '111 222 3333'
    customerNumber = '12345'

    customer1.changeName(name)
    customer1.changeAddress(address)
    customer1.changeTelephone(telephone)
    customer1.changeCustomerNumber(customerNumber)

    print("Customer name: " + customer1.name)
    print("Customer address: " + customer1.address)
    print("Customer telephone number: " + customer1.telephone)
    print("Customer number: " + customer1.customerNumber)
    print("On mailing list: " + customer1.OnMailingList)

    customer1.changeOnMailingList()

    print("On mailing list: " + customer1.OnMailingList)

main()
Was it helpful?

Solution

  1. use super(Customer, self).__init__() as @Pedro Wemeck say

    But there is a problem to note: If you use python 2.x, please change as one of the two ways:

    class Person -> class Person(object)
    

    or

    class Customer(Person):
        def __init__(self):
            Person.__init__(self)
    

    super can be only used in new-style class

  2. There is a problem:

    def changeOnMailingList():
        if onMailingList == False:
            onMailingList == True
        else:
            onMailingList == False
    

    you should change to:

    def changeOnMailingList(self):
        if self.onMailingList == False:
            self.onMailingList == True
        else:
            self.onMailingList == False
    
  3. You get AttributeError: 'NoneType' object has no attribute 'append' ,because the self.name is None, and can't use append, which belongs to list object.

You can use self.name = name directly.

OTHER TIPS

Your subclass Customer is redefining the __init__ method without calling the superclass method. This means that your lines in Person.__init__ creating the name, address and telephone attributes are never executed.

You want your Customer.__init__ method to call Person.__init__ at some point. Use Python super().

class Customer(Person):

    def __init__(self):
        super(Customer, self).__init__()
        self.customerNumber = None
        self.onMailingList = False
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top