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

  • Home
  • SEARCH
  • 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 1015927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:28:24+00:00 2026-05-16T10:28:24+00:00

So as part of problem 17.6 in Think Like a Computer Scientist, I’ve written

  • 0

So as part of problem 17.6 in “Think Like a Computer Scientist”, I’ve written a class called Kangaroo:

class Kangaroo(object):

    def __init__(self, pouch_contents = []):
        self.pouch_contents = pouch_contents

    def __str__(self):
        '''
        >>> kanga = Kangaroo()
        >>> kanga.put_in_pouch('olfactory')
        >>> kanga.put_in_pouch(7)
        >>> kanga.put_in_pouch(8)
        >>> kanga.put_in_pouch(9)
        >>> print kanga
        "In kanga's pouch there is: ['olfactory', 7, 8, 9]"
        '''

        return "In %s's pouch there is: %s" % (object.__str__(self), self.pouch_contents)

    def put_in_pouch(self, other):
        '''
        >>> kanga = Kangaroo()
        >>> kanga.put_in_pouch('olfactory')
        >>> kanga.put_in_pouch(7)
        >>> kanga.put_in_pouch(8)
        >>> kanga.put_in_pouch(9)
        >>> kanga.pouch_contents
        ['olfactory', 7, 8, 9]
        '''

        self.pouch_contents.append(other)

What’s driving me nuts is that I’d like to be able to write a string method that would pass the unit test underneath __str__ as written. What I’m getting now instead is:

In <__main__.Kangaroo object at 0x4dd870>'s pouch there is: ['olfactory', 7, 8, 9]

Bascially, what I’m wondering if there is some function that I can perform on kanga = Kangaroo such that the output of the function is those 5 characters, i.e. function(kanga) -> “kanga”.

Any ideas?

Edit:
Reading the first answer has made me realize that there is a more concise way to ask my original question. Is there a way to rewrite __init__ such that the following code is valid as written?

>>> somename = Kangaroo()
>>> somename.name
'somename'
  • 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-16T10:28:24+00:00Added an answer on May 16, 2026 at 10:28 am

    I wasn’t going to post this but if you only want this for debugging then here you go:

    import sys
    
    class Kangaroo(object):
        def __str__(self):
            flocals = sys._getframe(1).f_locals
            for ident in flocals:
                if flocals[ident] is self:
                    name = ident
                    break
            else:   
                name = 'roo'
            return "in {0}'s pouch, there is {1}".format(name, self.pouch_contents)
    
    kang = Kangaroo()
    print kang
    

    This is dependent on CPython (AFAIK) and isn’t suitable for production code. It wont work if the instance is in any sort of container and may fail for any reason at any time. It should do the trick for you though.

    It works by getting the f_locals dictionary out of the stack frame that represents the namespace where print kang is called. The keys of f_locals are the names of the variables in the frame so we just loop through it and test if each entry is self. If so, we break. If break is not executed, then we didn’t find an entry and the loops else clause assigns the value ‘roo’ as requested.

    If you want to get it out of a container of some sort, you need to extend this to look through any containers in f_locals. You could either return the key if it’s a dictlike container or the index if it’s something like a tuple or list.

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

Sidebar

Related Questions

Can't how to deal with my problem: part of view are hidden, when page
I've encountered a problem where part way through building a solution, VS 2010 becomes
I'll only try to present the main part of the problem, because the whole
Part of the solution to my problem I found here: How to calculate correlation
my problem is the following: This is a part of my HTML-Code: <form method='GET'
Strange problem happening where the end of these 2 blocks of code (this part:
The problem I'm experiencing is when I add the message Body part of the
I'm having a problem with a part of code. It should work,since it is
Here is part 1 of our problem (Loading a dynamically generated XML file as
I have a problem. Part of my app requires text to be shown in

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.