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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:47:08+00:00 2026-05-24T22:47:08+00:00

While trying to figure out how BeautifulSoup works, I incidentally learnt the __str__ method

  • 0

While trying to figure out how BeautifulSoup works, I incidentally learnt the __str__ method (I’m new to python). So if I did not misperceived then the __str__ method helps to shape how the class will be represented if printed out. For instance:

class Foo:
    def __str__(self):
        return "bar"

>>> x = Foo()
>>> print x
bar

Right? So asserting that I’m right, is it possible to override the __str__ method of a list of dictionaries? I mean say that in class Foo you have:

class Foo:
   def __init__(self):
      self.l = [{"Susan": ("Boyle", 50, "alive")}, {"Albert": ("Speer", 106, "dead")}]

Now is it possible to have the following outcome?

>>> x = Foo()
>>> print x.l
"Susan Boyle is 50 and alive. Albert Speer is 106 and dead."

EDIT

Considering agf’s solution, how can I access the dictionary once again? I mean if I define __str__ method then apparently I should define something else to retrieve the dictionary as it is. Please consider the following example:

class PClass(dict):
    def __str__(self):
        # code to return the result that I want 

class Foo:
    def __init__(self):
        self.l = PClass({"Susan": ["Boyle", ........ })

>>> x = Foo()
>>> print x.l 
# result that works great
>>> y = x.l["Susan"] # this would not work. How can I achieve it? 
  • 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-24T22:47:09+00:00Added an answer on May 24, 2026 at 10:47 pm

    You need to subclass the item you’re pretty-printing.

    from itertools import chain
    
    class PrintableList(list): # for a list of dicts
        def __str__(self):
            return '. '.join(' '.join(str(x) for x in
                chain.from_iterable(zip((item[0], 'is', 'and'), item[1])))
                    for item in (item.items()[0] for item in self)) + '.'
    
    class PrintableDict(dict): # for a dict
        def __str__(self):
            return '. '.join(' '.join(str(x) for x in
                chain.from_iterable(zip((item[0], 'is', 'and'), item[1])))
                    for item in self.iteritems()) + '.'
    
    class Foo:
       def __init__(self):
          self.d = PrintableDict({"Susan": ("Boyle", 50, "alive"), 
                                  "Albert": ("Speer", 106, "dead")})
    
    class Bar:
       def __init__(self):
          self.l = PrintableList([{"Susan": ("Boyle", 50, "alive")}, 
                                  {"Albert": ("Speer", 106, "dead")}])
    
    foo = Foo()
    print self.d
    bar = Bar()
    print self.l
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying to figure out the best method to ping (ICMP) something from python,
I learned something new while trying to figure out why my readwrite property declared
I've been trying to figure out how TTY drivers works for a while now
I was hanging out in my profiler for a while trying to figure out
A while ago I was trying to figure out a way of doing this
While trying to figure out terminology for rendering, I came down to this problem
While trying to figure out how to round a float like 1.255 to the
I am working on my first NASM program, and while trying to figure out
I've been struggling for a while now trying to figure this out and I'm
Okay I've been trying to figure this out for a while now. I have

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.