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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:17:30+00:00 2026-06-16T04:17:30+00:00

How to make format(self) work in this case? class Commit: number = None sha

  • 0

How to make format(self) work in this case?

class Commit:
    number = None
    sha = None
    message = None
    identity = None

    def __init__(self, raw, number):
        r = raw.commits[number]

        self.number = number
        self.sha = r['sha']
        self.message = r['message']
        self.identity = raw.identities[r['identity']]

    def __str__(self):
        return """
Commit {number} {sha}
Message {message}
Identity {identity}
""".format(self)

    def __getitem__(self, attr):
        return getattr(self, attr)
    def __contains__(self, attr):
        return hasattr(self, attr)

If I then try to access individual attributes as

c = Commit(raw, 170)
print(c['sha'])

for instance, it works. However, if I print(c) directly, it says:

KeyError: ‘number’

I would have expected format() to pull the attributes it needs via __getitem__().

How to make it work?

  • 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-16T04:17:32+00:00Added an answer on June 16, 2026 at 4:17 am

    No, you’d have to use **self and support more mapping methods.

    However, you’d be much better off using the format support for attribute access instead:

        def __str__(self):
            return """
    Commit {0.number} {0.sha}
    Message {0.message}
    Identity {0.identity}
    """.format(self)
    

    Now it’ll use attribute access to find number, message, etc. from the first positional argument to .format(), which is self.

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

Sidebar

Related Questions

I have a Person class like this: class Person: def __init__(self, name, age): self.name
This is my class: class Account(models.Model): name = models.CharField(max_length=255) def __unicode__(self): return '{0}'.format(self.name) class
# file1.py class _Producer(self): def __init__(self): self.chunksize = 6220800 with open('/dev/zero') as f: self.thing
I want to make a program that will read some number in string format
I have a class that takes a string in this format: 000067000000000012620060324b38e2cab3353 , encrypts
I need to make a file format for my software. The data is basic
I am trying to make a new file format for music. It needs to
How can i make an rtf file and tiff file format to be viewed
i'd like to make an export into CSV format, but the mutualised host i
I tried to make my textbox type in currency format. I did it, but

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.