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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:18:37+00:00 2026-05-22T21:18:37+00:00

class Bar: pass class Foo: def __str__(self): return Foo instance >> aBar = Bar()

  • 0
class Bar:
     pass

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

>> aBar = Bar()
>> print aBar
<__main__.Bar instance at 0x100572a28>
>> aFoo = Foo()
>> print aFoo
Foo instance

is there a way to print out the address of aFoo after overriding the str method?

using

 >>repr(aFoo) 

solved my problem

  • 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-22T21:18:38+00:00Added an answer on May 22, 2026 at 9:18 pm

    At least in cpython, id provides the address. But the output is in decimal; you have to convert that to hex:

    >>> f = (x for x in [1,2,3])
    >>> print f
    <generator object <genexpr> at 0x1004d22d0>
    >>> '%x' % id(f)
    '1004d22d0'
    

    Actually, though, the __repr__ function isn’t altered when __str__ is overridden. So you can do this as well:

    >>> class Foo:
    ...     def __str__(self): return "Foo instance"
    ... 
    >>> a = Foo()
    >>> print a
    Foo instance
    >>> print repr(a)
    <__main__.Foo instance at 0x1004d1c68>
    

    I think id is preferable for this, if what you want is really the id. But id is not guaranteed to return the address; that’s just the cpython implementation. I don’t know whether it’s specified that the built-in __repr__ of objects has to return an address, or whether it has to return the id, or neither. So if you specifically want whatever it is that __repr__ provides, then this may be the way to go.

    Update: The answer is neither, at least according to the language reference, which dictates only that the __repr__ of an object be “information-rich and unambiguous.” And indeed, sometimes the __repr__ does not actually return the address of the specific object in question, as seen here:

    >>> a = Foo()
    >>> '%x' % id(a)
    '1004d1fc8'
    >>> '%x' % id(a.__str__)
    '1004745a0'
    >>> '%x' % id(Foo.__str__)
    '1004745a0'
    >>> repr(a.__str__)
    '<bound method Foo.__str__ of <__main__.Foo instance at 0x1004d1fc8>>'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Foo(object): pass foo = Foo() def bar(self): print 'bar' Foo.bar = bar foo.bar()
Consider the following Python (3.x) code: class Foo(object): def bar(self): pass foo = Foo()
Can one write something like: class Test(object): def _decorator(self, foo): foo() @self._decorator def bar(self):
def foo(): pass def bar(): print 'good bay' two function like blow and now
When inheriting from two objects like these class Foo(object): def __init__(self,a): self.a=a class Bar(object):
If I define: class Bar(object): @staticmethod def bar(): # code pass class Foo(Bar): #
I have class Foo(): function bar(): pass function foobar(): pass Rather than executing each
Here's an example of my problem : class bar() : spam = foo() def
One can do something like this: class master: @combomethod def foo(param): param.bar() # Param
class TheModel extends Backbone.Model foo: #bar Entity = new TheModel(#pass in attributes) Can I

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.