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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:01:38+00:00 2026-05-25T10:01:38+00:00

Edit: There was some confusion, but I want to ask a general question about

  • 0

Edit: There was some confusion, but I want to ask a general question about object oriented design in Python.

Consider a class that lets you map data values to counts or frequencies:

class DataMap(dict):
    pass

Now consider a subclass that allows you to construct a histogram from a list of data:

class Histogram(DataMap):
    def __init__(self, list_of_values):
        # 1. Put appropriate super(...) call here if necessary
        # 2. Build the map of values to counts in self
        pass

Now consider a class that lets you make a smoothed probability mass table rather than a Histogram.

class ProbabilityMass(DataMap):
    pass

What is the best way to allow a ProbabilityMass to be constructed from either a Histogram or a list of values?

I “grew up” programming in C++, and in this case I would use an overloaded constructor. In Python I’ve thought of doing this with:

  • The constructor takes multiple arguments (all but one of these should == None)
  • I define from_Histogram and from_list methods

In the second case (which I believe is better), what is the best way to allow the from_list method to use the shared code from the Histogram constructor? A ProbabilityMass table is nearly identical to a Histogram table, but it is scaled so that the sum of all value is 1.0.

If you have come across a similar problem, please share your expertise!

  • 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-25T10:01:39+00:00Added an answer on May 25, 2026 at 10:01 am

    To start with, if you think you want @staticmethod, you almost always don’t. Either the function is not part of the class, in which case it should just be a free function, or it is part of the class, but not tied to an instance, and it should be a @classmethod. Your named constructor is a good candidate for a @classmethod.

    Also note that you should invoke A.__init__ from B via super(), otherwise multiple inheritance can bite you bad.

    class A:
        def __init__(self, data):
            self.values_to_counts = {}
            for val in data:
                if val in self.values_to_counts:
                    self.values_to_counts[val] += 1
                else:
                    self.values_to_counts[val] = 1
        @classmethod
        def from_values_to_counts(cls, values_to_counts):
            self = cls([])
            self.values_to_counts = values_to_counts
            return self
    
    class B(A):
        def __init__(self, data, parameter):
            super(B, self).__init__(data)
            self.parameter = parameter
        def print_parameter(self):
            print self.parameter
    

    In this case, you don’t need a B.from_values_to_counts, it inherits from A, and it will return an instance of B, since that’s how it was called.

    If you need to do more complex initialization in B, you can, using super(), which looks very similar to the way it would when you use it with instances. after all, a classmethod really isn’t anything more complex than an instancemethod where the im_self attribute is assigned to the class itself.

    class A:
        def __init__(self, data):
            self.values_to_counts = {}
            for val in data:
                if val in self.values_to_counts:
                    self.values_to_counts[val] += 1
                else:
                    self.values_to_counts[val] = 1
        @classmethod
        def from_values_to_counts(cls, values_to_counts):
            self = cls([])
            self.values_to_counts = values_to_counts
            return self
    
    class B(A):
        def __init__(self, data, parameter):
            super(B, self).__init__(data)
            self.parameter = parameter
        def print_parameter(self):
            print self.parameter
        @classmethod
        def from_values_to_counts(cls, values_to_counts):
            self = super(B, cls).from_values_to_counts(values_to_counts)
            do_more_initialization(self)
            return self
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to learn some JVM language, but there are so many EDIT :
Edit I think there is some confusion, I am not using both of the
EDIT: There must be some way I can approach this without writing a whole
Is there a way to make a click-to-edit control in silverlight? I've got some
I'm using Eclipse in Ubuntu to edit PHP files. But, unfortunately, some of these
EDIT: There's now a doc page on this so this question is irrelevant, also
EDIT: There is the almost same question at Removing whitespace between HTML elements when
I have a question about association multiplicity. I understand it, but for example if
**EDIT: There are several options below that would work. Please vote/comment according to your
EDIT - there are no blank lines. EDIT 2 - my bad, it seems

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.