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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:48:48+00:00 2026-06-06T11:48:48+00:00

I want to define a class that is able to populate itself reading from

  • 0

I want to define a class that is able to populate itself reading from the serialized data of another instance. Here’s the simplified code:

class MyClass(list):

    def __init__(self,**kwargs):
        if kwargs.has_key('fdata'):
            f = open(kwargs['fdata'],'r')
            self = pickle.load(f)
            print len(self)    #prints 320          
            f.close()              

    ...

a = MyClass(fdata='data.dat')
print len(a)    #prints 0

This is the output I obtain:

320
0

The problem I have is that the instance returned is always empty, even though I am able to read all the elements inside __init__() What can be causing this?

  • 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-06T11:48:50+00:00Added an answer on June 6, 2026 at 11:48 am

    Assigning to self inside a method simply rebinds the local name self to a different object. Assignments to bare names in Python can never modify any object – they just rebind names.

    Why don’t you use the straight-forward

    with open("data.dat") as f:
        a = pickle.load(f)
    

    instead of constructing a new class? If you don’t like this, wrap this in a function, but it’s not that useful to put this code into __init__().

    There are other ways to achieve the same effect. Probably the best way to achieve exactly what you are trying is to overwrite __new__() instead of __init__() – __new__() is called before the new instance is constructed, so you can simply return the unpickled instance instead of having to modify an already constructed one.

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

Sidebar

Related Questions

I want to define a class that supports __getitem__ , but does not allow
I want to define a class method that has access to a local variable.
What I mean is to define an instance of a type class that applies
I want to define a class, ClassA , that can be passed as a
I want to define some properties on a class using the [Indexable()] attribute in
I want to define a constant that should be available in all of the
I have written one MapActivity class that is able to display a set of
I have a class that reads a pop3 email server. I want to write
I want to define an interface IFile that includes an array of key/value pairs
We have a situation where we want to define a relationship where a class

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.