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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:13:36+00:00 2026-06-05T16:13:36+00:00

I have a container class which I would like to use to print all

  • 0

I have a container class which I would like to use to print all the elements in it.

I’d like to print them to file or console.

I’ve laid out the element (Patch) and container class as below and __repr__(self).
I’m not sure I understood the purpose of __repr__() and am wondering whether the usage here is OK.

class Patch:
    def __init__(self, folder_name, file_name):
        self.folder_name = folder_name
        self.file_name = file_name
        self.full_path = os.path.join(self.folder_name, self.file_name)
        self.file_hash = md5_for_file(open(self.full_path, 'r'))
        self.file_size = os.path.getsize(self.full_path)

    def __repr__(self):
        return "%s %s %s" % (self.file_name, self.file_hash, self.file_size)

class PatchContainer:
    def __init__(self):
        self.patch_folder_dict = collections.OrderedDict()
        self.patch_file_set = set()
    def addPatch(self, patch):
        if patch.file_name in self.patch_file_set:
            print '*** Delete the file ', patch.full_path, ' ***'
            return
        self.patch_file_set.add(patch.file_name)
        if not patch.folder_name in self.patch_folder_dict:
            self.patch_folder_dict[patch.folder_name] = [patch]
        else:
            self.patch_folder_dict[patch.folder_name].append(patch)

    def prettyPrint(self, writeable_object=PATCH_META_FILE):
        sys.stdout = writeable_object
        for patch_folder in self.patch_folder_dict.keys():
            print patch_folder
            patch_list = self.patch_folder_dict[patch_folder]
            for patch in patch_list:
                print patch
        sys.stdout = sys.__stdout__

It works as intended but please comment on whether the style/usage are fine.

  • 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-05T16:13:38+00:00Added an answer on June 5, 2026 at 4:13 pm

    This looks like you probably want __str__. They are similar, but __str__ returns something intended for human consumption; __repr__ should return something intended for Python consumption – it should either be a Python expression that will evaluate to an equal object, or something in angle brackets like the default <classname object at id>. See the documentation for the builtin repr(object), which ends up calling object.__repr__():

    For many types, this function makes an attempt to return a string that
    would yield an object with the same value when passed to eval(),
    otherwise the representation is a string enclosed in angle brackets
    that contains the name of the type of the object together with
    additional information

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

Sidebar

Related Questions

I have a custom container class for which I'd like to write the iterator
I have a Moose class that i would like to store using Apache::Session::File. However,
I have a container class which uses boost::optional to hold the value. Here is
I have a DLL which contains a class with static members . I use
I have a class LINE which contains two properties of type POINT. I would
I have a class which contains a static collection that can be used across
I have a class which contains 5 properties. If any value is assingned to
I have a parent class which contains a child object. I am using set
I have an abstract class which contains method for setting header text. It looks
Hi i have a class which contains different methods for checking database values. Each

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.