2

Write a class named Person with data attributes for a person's name, address, telephone and email. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be one the mailing list. Demonstrate an instance of the Customer class in a simple program.

I am getting an attibute error.

, line 6, in main
    '555-987-1549','adsf@asd.com','Y','1153')
TypeError: __init__() takes 5 positional arguments but 7 were given

I don't completely follow the point of using the boolean expression so maybe someone could explain why it is being used and help me with why I am getting that error.

Heres my code

class Person:

class Person:
    def __init__(self, name, address, telephone, email):
        self.__name = name
        self.__address = address
        self.__telephone = telephone
        self.__email = email

    def set_name(self, name):
        self.__name = name

    def set_address(self, address):
        self.__address = address

    def set_telephone(self, telephone):
        self.__telephone = telephone

    def set_email(self, email):
        self.__email = email

    def get_name(self):
        return self.__name

    def get_address(self):
        return self.__address

    def get_telephone(self):
        return self.__telephone

    def get_email(self):
        return self.__email

Class Customer:

import Person

class Customer(Person.Person):
    def __init__(self, name, address, telephone, email):
        Person.Person.__init__(self, mail, number)
        self.mailing = 'Y'== True
        self.__mail = mail
        self.__number = number

    def set_mail(self, mail):
        self.__mail = mail

    def set_number(self, number):
        self.__number = number

    def get_mail(self):
        return self.__mail

    def get_number(self):
        return self.__number

    def mailList(self, mail):
        if mailing == True:
            return"On the mailing list"
        else:
            return"Not on the mailing list"

    def __str__(self):
        return "\nName: {}\nAddress: {}\ntelephone: {}\nEmail: {}\nMail: {}\nNumber: {}".\
            format(self.get_name(), self.get_address(),\
                   self.get_telephone(), self.get_email()\
                   , self.mailList(self.get_mail()),self.get_number())

customerTest:

import Customer

def main():

    customer = Customer.Customer('Josh', 'Long st, Dallas TX',\
                                 '555-987-1549','adsf@asd.com','Y','1153')
    print(customer)

main()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
keggy
  • 53
  • 2
  • 7
  • 1
    You invoke `Customer` with 6 parameters, which then invokes `Person` with two parameters (both are not defined, by the way), but both those constructors require 4 parameters, next to `self`. Also, `self.mailing = 'Y'== True` does not make much sense. – tobias_k Jun 21 '15 at 14:02

3 Answers3

1

Looks like instead of

def __init__(self, name, address, telephone, email):

You actually meant:

def __init__(self, name, address, telephone, email, mail, number):

By the way, what is going on here?

    self.mailing = 'Y'== True

You probably mean

    self.mailing = mail == True

Edit: Yeah, what Tobias said. Please fix this as well including missing parameters -

    Person.Person.__init__(self, mail, number)

Also while it is not necessary, you might want to use super().(...) syntax instead of calling Person.__init__(self, ...) directly.

Community
  • 1
  • 1
KalEl
  • 8,978
  • 13
  • 47
  • 56
0

__init__ constructor in `Customer is not correct, numbers of arguments is incorrect.

In self.mailing you probably want True or False based on argument.

def __init__(self, name, address, telephone, mail, mailing, number):
        Person.Person.__init__(self, name, address, telephone, mail)
        self.mailing = True if mailing == 'Y' else False
        self.__mail = mail
        self.__number = number

Also mailList need to use self to refer to current object.

def mailList(self, mail):
        if self.mailing == True:
            return"On the mailing list"
        else:
            return"Not on the mailing list"
ThePavolC
  • 1,693
  • 1
  • 16
  • 26
  • Thanks I almost got it to work. But the mailing always returns Not on the mailing List do you know what it could be I used the code you gave me. – keggy Jun 21 '15 at 14:22
0

For Person Class and if you are using Python2.7, define it as inheriting from Object class to be able to use super expression properly later when you inherent from this class otherwise if you deal with Python3+, you can just say super().__init__(), so:

Person Class will be:

class Person(object):#only change
    def __init__(self, name, address, telephone, email):
        self.__name = name
        self.__address = address
        self.__telephone = telephone
        self.__email = email

    def set_name(self, name):
        self.__name = name

    def set_address(self, address):
        self.__address = address

    def set_telephone(self, telephone):
        self.__telephone = telephone

    def set_email(self, email):
        self.__email = email

    def get_name(self):
        return self.__name

    def get_address(self):
        return self.__address

    def get_telephone(self):
        return self.__telephone

    def get_email(self):
        return self.__email

As for Customer Class, read code comments where I've made changes

import Person

class Customer(Person.Person):
    def __init__(self, name, address, telephone, email, mail, number): #fixed number of arguments to pass for class instantiation 
        super(type(self), self).__init__(name, address, telephone, email) #Also here and not the use of `super`
        #super().__init__(name, address, telephone, email) with Python 3+
        self.mailing = True if mail == 'Y' else False #Fixed self.mailing expression
        self.__mail = mail
        self.__number = number

    def set_mail(self, mail):
       self.__mail = mail

    def set_number(self, number):
        self.__number = number

    def get_mail(self):
        return self.__mail

    def get_number(self):
        return self.__number

    def mailList(self): #No need for mail argument
        if self.mailing == True: #Just Check here self.mailing
            return "On the mailing list"
        else:
            return "Not on the mailing list"

    def __str__(self):
        return "\nName: {}\nAddress: {}\ntelephone: {}\nEmail: {}\nMail: {}\nNumber: {}".\
                format(self.get_name(), self.get_address(),\
                       self.get_telephone(), self.get_email()\
                       , self.mailList(),self.get_number())
Community
  • 1
  • 1
Iron Fist
  • 10,739
  • 2
  • 18
  • 34