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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:55:12+00:00 2026-05-24T17:55:12+00:00

I am working in Python with an Email() class that I would like to

  • 0

I am working in Python with an Email() class that I would like to extend into a SerializeEmail() class, which simply adds two further methods, .write_email() and .read_email(). I would like this sort of behaviour:

# define email
my_email = SerializeEmail()
my_email.recipients = 'link@hyrule.com'
my_email.subject = 'RE: Master sword'
my_email.body = "Master using it and you can have this."
# write email to file system for hand inspection
my_email.write_email('my_email.txt')
...
# Another script reads in email
my_verified_email = SerializeEmail()
my_verified_email.read_email('my_email.txt')
my_verified_email.send()

I have navigated the json encode/decode process, and I can successfully write my SerializeEmail() object, and read it in, however, I can’t find a satisfactory way to recreate my object via a SerializeEmail.read_email() call.

class SerializeEmail(Email):

    def write_email(self,file_name):

        with open(file_name,"w") as f:
            json.dump(self,f,cls=SerializeEmailJSONEncoder,sort_keys=True,indent=4)

    def read_email(self,file_name):

        with open(file_name,"r") as f:
           json.load(f,cls=SerializeEmailJSONDecoder)

The problem here is that the json.load() call in my read_email() method returns an instance of my SerializeEmail object, but doesn’t assign that object to the current instance that I’m using to call it. So right now I’d have to do something like this,

another_email = my_verified_email.read_email('my_email.txt')

when what I want is for the call to my_veridied_email.read_email() to populate the current instance of my_verified_email with the data on the file. I’ve tried

self = json.load(f,cls=SerializeEmailJSONDecoder)

but that doesn’t work. I could just assign each individual element of my returned object to my “self” object, but that seems ad-hoc and inelegant, and I’m looking for the “right way” to do this, if it exists. Any suggestions? If you think that my whole approach is flawed and recommend a different way of accomplishing this task, please sketch it out for me.

  • 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-24T17:55:13+00:00Added an answer on May 24, 2026 at 5:55 pm

    While you could jump through a number of hoops to load serialized content into an existing instance, I wouldn’t recommend doing so. It’s an unnecessary complication which really gains you nothing; it means that the extra step of creating a dummy instance is required every time you want to load an e-mail from JSON. I’d recommend using either a factory class or a factory method which loads the e-mail from the serialized JSON and returns it as a new instance. My personal preference would be a factory method, which you’d accomplish as follows:

    class SerializeEmail(Email):
    
        def write_email(self,file_name):
    
            with open(file_name,"w") as f:
                json.dump(self,f,cls=SerializeEmailJSONEncoder,sort_keys=True,indent=4)
    
        @staticmethod
        def read_email(file_name):
    
            with open(file_name,"r") as f:
               return json.load(f,cls=SerializeEmailJSONDecoder)
    
    # You can now create a new instance by simply doing the following:
    new_email = SerializeEmail.read_email('my_email.txt')
    

    Note the @staticmethod decorator, which allows you to call the method on the class without any implicit first argument being passed in. Normally factory methods would be @classmethods, but since you’re loading the object from JSON, the implicit class argument is unnecessary.

    Notice how, with this modification, you don’t need to instantiate a SerializeEmail object before you can load another one from JSON. You simply call the method directly on the class and get the desired behavior.

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

Sidebar

Related Questions

I'm working on a Python library that interfaces with a web service API. Like
I'm working on a simple python based system that sends a verification email to
Working in python 2.7. I have an argument that takes a list, adds the
I have a working Python 2.6 code using matplotlib, and would like to get
Working with python interactively, it's sometimes necessary to display a result which is some
I need to have an at-home project now that I'm working on Python/Django at
I'm working on a Python package named lehmer that includes a bunch of extension
I'm working on a project that involves uploading an image to tumblr from Python.
I have a working Python based program that I want to run as a
I have a working Python script that executes an external command and calls Popen.communicate()

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.