-2

I created this class:

class Family:
    def __init__(self):
        self.__name = ""
        self.__address = ""
        self.__age = ""
        self.__phone = ""

    # Mutator Methods
    def setName (self,mName):
        self.__name = mName
    def setAddress (self,mAddress):
        self.__address = mAddress
    def setAge (self,mAge):
        self.__age = mAge
    def setPhone (self,mPhone):
        self.__phone = mPhone

    # Accessor Methods
    def getName():
        return __name
    def getAddress():
        return __address
    def getAge():
        return __age
    def getPhone():
        return __phone

Using the main module, I created three instances of the family class and put them in a list.

def main():
    objectOne = familyClass.Family()
    objectTwo = familyClass.Family()
    objectThree = familyClass.Family()

    membersList = [objectOne,objectTwo,objectThree]

I, then, created a loop that asks the user to input the information for each object then another loop to print out the information:

    for index in range(0,3):
        membersList[index].setName = input("Enter the name: ")
        membersList[index].setAddress = input("Enter the address: ")
        membersList[index].setAge = input("Enter the age: ")
        membersList[index].setPhone = input("Enter the phone: ")
        print()

    for index in range(0,3):
        print("Here's the family members entered:")
        print('Name: {}'.format(membersList[index].getName))
        print('Address: {}'.format(membersList[index].getAddress))
        print('Age: {}'.format(membersList[index].getAge))
        print('Phone: {}'.format(membersList[index].getPhone))
        print()

but I kept getting this:

Here's the family members entered:
Name: <bound method Family.getName of <familyClass.Family object at 0x106264970>>
Address: <bound method Family.getAddress of <familyClass.Family object at 0x106264970>>
Age: <bound method Family.getAge of <familyClass.Family object at 0x106264970>>
Phone: <bound method Family.getPhone of <familyClass.Family object at 0x106264970>>
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 2
    As the output says, you got the *method* objects. It seems you expected to get their return values, so you actually have to *call* them using `()`. – mkrieger1 May 03 '20 at 09:36
  • You may want to read a basic Python OOP tutorial. You generally don't need getter /setter in Python, and should use property to implement them. Also, `__init__` should generally take the parameters with which to initialise the fields, not assign bogus values. – MisterMiyagi May 03 '20 at 10:05
  • Its a homework problem and the instructor wanted us to use accessor and mutator functions instead of the constructor. – Youssef Hegazy May 04 '20 at 04:13

1 Answers1

0

you have 2 problem:
1 - in your getter method you omit to use self

 # Accessor Methods 
 def getName(self): 
     return self.__name
 def getAddress(self):
     return self.__add
 def getAge(self): 
     return self.__age 
 def getPhone(): 
     return self.__phone 

2 - you need to call your set with () like this a.setName( input("Enter the name: ")) because is method and not attribute
read here What's the pythonic way to use getters and setters?

example :

In [63]: a = Family()                                                                                                                                                                                      

In [64]: a.setName( input("Enter the name: "))                                                                                                                                                             
Enter the name: blah

In [65]: a.getName()                                                                                                                                                                                       
Out[65]: 'blah'

In [66]:  
Beny Gj
  • 607
  • 4
  • 16