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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:51:59+00:00 2026-06-17T08:51:59+00:00

Please can someone explain why I get the following error when using %r with

  • 0

Please can someone explain why I get the following error when using %r with tuples?

>>> repr((1,2))
'(1, 2)'
>>> class Foo(object):
...     def __init__(self,vals):
...             self.vals=vals
...     def __repr__(self):
...             return "Foo(%r)" % self.vals
... 
>>> foo = Foo((1,2))
>>> foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in __repr__
TypeError: not all arguments converted during string formatting

What is the appropriate way for printing out __repr__? Should I be using %s and repr(self.vals) instead?

  • 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-17T08:52:00+00:00Added an answer on June 17, 2026 at 8:52 am

    The % operator takes a tuple itself, so you are basically doing this:

    'Foo(%r)' % (1, 2)
    

    Wrap self.vals in a one-element tuple:

    'Foo(%r)' % (self.vals,)
    

    In principle you can use a string defined in a variable too:

    REPRFORMAT % (self.vals,)
    

    in which case you want to leave it up to that variable (perhaps taken from a configuration file?) how to format self.vals, be it %r or %s.

    You could also use the .format() method instead:

    return 'Foo({0!r})'.format(self.vals)
    

    This format gives you more flexibility with the input given; you could address individual items in the vals tuple, for example:

    return 'Foo(({0[0]:04d}, {0[1]:02d}))'.format(self.vals)
    

    which would result in Foo((0001, 04)) for your example input.

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

Sidebar

Related Questions

Can someone please explain to me why in the following code I get an
Please can someone could explain why I get this error and what to do
I don't get it - can someone please explain to me why I should
Could someone please help explain why I can't get this to work? I properly
Can someone please explain this phenomenon? I am using Mojarra 2.1.6 - Glassfish 3.1.2.
Can someone please explain this to me? I have the following code: <form action=<?php
Can someone please explain to me the following behaviour: function getLatLong() { var geocoder
Is there someone who can please explain why the following is not working??: the
Can someone please explain why the following code causes my app to bomb? NSData
Can please someone explain to me or bring me on the way how to

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.