-1

I have experience with classes but i am a little confused here. The problem is that it says store reference of the other Objects of class also in the class object. I am putting the code i wrote below. Kindly guide me on how to solve the problem? This is the image of question

class Person:
friends=list()

def __init__(self, first_name, last_name):
    self.first_name=first_name
    self.last_name=last_name

def add_friend(self, friend_person):
    self.friends.append(friend_person)

def get_name(self):
    name=self.first_name+' '+self.last_name
    return name

def get_friends(self):
    return self.friends

1 Answers1

2

Attribute friends belong to a person and is different for each person.

This means friends should be an instance variable rather than class variable.

class Person: 

  def __init__(self, first_name, last_name):
      self.first_name = first_name
      self.last_name = last_name
      self.friends = []  # person can have a list of friends

  def add_friend(self, friend):
      " Add a friend for this person "
      self.friends.append(friend)

  def get_name(self):
    return self.first_name + " " + self.last_name

  def get_friends(self):
    " List of friends as a string "
    return self.friends  # returns list of friends

  def __str__(self):
    """ Converts attributes of person to string 
        (useful for print) """
    return self.get_name()

Example Usage

# Example
p = Person('Bob', "Johnson")

# Add some friends (uses Person constructor to create persons)
p.add_friend(Person("Mary", "Sullivan"))  
p.add_friend(Person("Jodie", "Oliver"))
p.add_friend(Person("Danny", "Washington"))

# Show p's friends
print(f'Person: {p}')   # uses __str__ Person method to convert p to string
                        # for printing
print('Friends')
for friend in p.get_friends():
  print('\t', friend)  # since friend is a Person, Person method __str__
                       # knows how to convert to string for printing

Output

Person: Bob Johnson
Friends
     Mary Sullivan
     Jodie Oliver
     Danny Washington
DarrylG
  • 16,732
  • 2
  • 17
  • 23
  • @juanpa.arrivillaga Thanks alot for noticing that mistake, i clearly forgot that one. However the actual thing that is confusing me is that what's the best way to store the reference of other object of class (u can get more clear explanation from image in question) – Haris Harris Jun 05 '20 at 18:11
  • @HarisHarris--added an example usage. Does this help clarify how other objects (friends in this case) are added to a person instance? Other persons are added to the `self.friends` list. – DarrylG Jun 05 '20 at 18:30
  • @DarryIG first of all apologies i tagged wrong person. I am new and trying to get used to this farmework. Secondly yeah it's much clear now. Thanks alot. – Haris Harris Jun 05 '20 at 18:43
  • @HarisHarris--feel free to ask away if you have any follow up questions. – DarrylG Jun 05 '20 at 19:00
  • @HarisHarris--no problem. People build up reputation points this way (in case you wondered about the numbers associated with users). – DarrylG Jun 06 '20 at 15:40
  • Ok. Thanks for being kind. – Haris Harris Jun 06 '20 at 16:32