AttributeError in my Python program regarding classes
Posted
by
Axel Finkel
on Stack Overflow
See other posts from Stack Overflow
or by Axel Finkel
Published on 2014-06-05T02:33:42Z
Indexed on
2014/06/05
3:25 UTC
Read the original article
Hit count: 184
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()
© Stack Overflow or respective owner