Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7493427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:57:11+00:00 2026-05-29T16:57:11+00:00

I’m trying to print the list created by the functions in this class- what

  • 0

I’m trying to print the list created by the functions in this class- what do I need to fix? I’m getting output from the terminal along the lines of [<__main__.Person instance at 0x1004a0320>,.

class Person:
    def __init__(self,first,last,id,email):
        self.firstName=first
        self.lastName=last
        self.id=id
        self.email=email
        self.friends=[]
    def add_friend(self,friend):
        if len(self.friends)<5:
            self.friends.append(friend)
        if len(friend.friends)<5:
            friend.friends.append(self)

p1=Person("David","Waver","922-43-9873","dwaver@wsu.edu")
p2=Person("Bob","Jones","902-38-9973","bjones@odu.edu")
p3=Person("James","Smith","302-38-9103","jonsdfes@ou.edu")
p4=Person("Tim","Jack","902-38-0918","remmy@usc.edu")
p5=Person("Jim","Johnston","314-78-2343","jjohnston@fsu.edu")
p6=Person("Gina","Relent","102-38-1064","ginar@wvu.edu")
p7=Person("Greg","Morris","932-38-4473","jones@ttu.edu")

p1.add_friend(p2)
p1.add_friend(p3)
p1.add_friend(p4)
p1.add_friend(p5)
p1.add_friend(p6)
p1.add_friend(p7)

print p1.friends
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:57:11+00:00Added an answer on May 29, 2026 at 4:57 pm

    You need to define __repr__ or __str__ in your Person class.

    >>> class Person:
    ...     def __init__(self,first,last,id,email):
    ...         self.firstName=first
    ...         self.lastName=last
    ...         self.id=id
    ...         self.email=email
    ...         self.friends=[]
    ...     def add_friend(self,friend):
    ...         if len(self.friends)<5:
    ...             self.friends.append(friend)
    ...         if len(friend.friends)<5:
    ...             friend.friends.append(self)
    ...     def __repr__(self):
    ...         return self.firstName + ' ' + self.lastName
    

    Then initialize the list as above…

    >>> print p1.friends
    [Bob Jones, James Smith, Tim Jack, Jim Johnston, Gina Relent]
    

    This answer gives a good explanation of these functions.

    Given the above post’s point about the functions of __repr__ and __str__, probably the __repr__ should look more like this:

    def __repr__(self):
        template = "Person('{0}', '{1}', '{2}', '{3}')"
        return template.format(self.firstName, self.lastName, self.id, self.email)
    

    What’s nice about the above is that it generates a string that, when evaluated, creates an object that has the same properties (apart from friends) as the original. For example:

    >>> print p1
    Person('David', 'Waver', '922-43-9873', 'dwaver@wsu.edu')
    >>> Person('David', 'Waver', '922-43-9873', 'dwaver@wsu.edu')
    Person('David', 'Waver', '922-43-9873', 'dwaver@wsu.edu')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.