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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:43:30+00:00 2026-06-18T02:43:30+00:00

Scenario I am looking for an object oriented approach in python that makes it

  • 0

Scenario

I am looking for an object oriented approach in python that makes it possible to save an instance of a class in a data file and also load it again in at a later point in time. My current approach looks like this:

class A(object):
    def __init__(self, ComplexParam1, ComplexParam2):
        self.ComplexParam1 = ComplexParam1
        self.ComplexParam2 = ComplexParam2

    @staticmethod
    def Create(EasyParam1, EasyParam2):
        #do some complex calculation to get ComplexParam1 and ComplexParam2 from EasyParam1 and EasyParam2
        return A(ComplexParam1, ComplexParam2)        

    def Save(self, Filename):
        #write ComplexParam1 and ComplexParam2 to disc

    @staticmethod
    def Load(Filename):
        #read ComplexParam1 and ComplexParam2 and call constructor
        return A(ComplexParam1, ComplexParam2)

As you can see ComplexParam1 and ComplexParam2 are to be calculated parameters and are not used for a first creation of the object A since they are very complex to get, whereas EasyParam1 and EasyParam2 are "known" parameters. Think of it as if the EasyParameters are integers and the ComplexParameters are large matricies that are constructed based on EasyParameters

So I am using the setup above to Save and Load objects to and from file, where Create uses the constructor since ComplexParam1 and ComplexParam2 are stored in file and do not need to be calculated again.

Problem

Up until now, the approach shown above worked just fine for me. Problems however arise, when this scheme is also used with class inheritances. So I am looking for a nicer and cleaner solution to my problem.

In C++ I would overload the constructor and make two possible creations of the class available, but this is not supported in python.

Any help, link and suggestion is appreciated.

  • 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-18T02:43:32+00:00Added an answer on June 18, 2026 at 2:43 am

    I think this is a case for the @classmethod decorator. For example, if you change the Load method to the following:

        @classmethod
        def Load(cls, Filename):
            # Do stuff
            return cls(ComplexA, ComplexB)
    

    Then you could override the constructor:

    class B(A):
        def __init__(self, complexA, complexB):
            # Whatever you want, including calling the parent constructor
    

    And, finally, you could call B.Load(some_file) which would invoke B.__init__.

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

Sidebar

Related Questions

I'm looking for a data structure that can possibly outperform Dictionary<string, object> . I
Python version 2.7. Scenario class A(object): x = 0 class B(A): pass ai =
I'm looking for possible solutions for the following scenario: I have a service that
I'm looking for a queuing system that could support the following scenario: A client
I'm looking around for a RegEx that can help me parse an nquad file.
Please consider the following scenario. I have a Task, that returns an object, lets
Is there any way to save an entire class definition for a C# object
I am looking for some help with an Ektron problem. The scenario is that
Recently I have been writing some serializable object that also deals with specific logic
I'm looking for example of how I would solve the scenario below: Imagine my

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.