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 8172889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:55:49+00:00 2026-06-06T21:55:49+00:00

Purely a cosmetic thing, but when I create a class in python, how do

  • 0

Purely a cosmetic thing, but when I create a class in python, how do I control its instances’ titles? e.g. for

class Foo():
    def __init__(self, name):
         self.name = name

I can then create an instance:

>> f = Foo('bar')
>> f
<Foo instance at 0x10ab41a28>

If I wanted that to return bar, what would I do? (In Django, you use the __unicode__ method of the class, but I’ve tried setting both __unicode__ and __str__ functions to return self.name, and while these work on their own, they don’t affect the value of the class.)

  • 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-06-06T21:55:52+00:00Added an answer on June 6, 2026 at 9:55 pm

    I usually define __str__ to return a string representation, and then return the string in the __repr__ method, like this:

    >>> class Foo():
    ...     def __init__(self, name):
    ...          self.name = name
    ...     def __str__(self):
    ...          return '<Foo name=%s>' % self.name
    ...     def __repr__(self):
    ...          return str(self)
    ... 
    >>> f = Foo('bar')
    >>> f
    <Foo name=bar>
    

    For more info, see Difference between str and repr in Python

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code: object Link { //TYPE-1 (purely functional) def apply(name: String, target: Page, f:
I got the idea to create a purely code-driven striped image background when I
I have a purely virtual class defined as such: class BaseClass { protected: const
I understand this is purely a coding related forum, but i believe techies can
This is a purely theoretical question. Given three simple classes: class Base { }
A question purely for curiosity's sake. This is obviously invalid syntax: foo = {}
This is purely out of curiosity, but why does this occur? >>> a =
I am asking this purely to determine the worthwhile-ness of implementing the class in
This is a purely academic question, but what's the difference between using == and
So this question is purely for learning purposes and curiosity, but can anyone explain

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.