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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:16:45+00:00 2026-05-29T08:16:45+00:00

I am running an interactive python session which builds big python data-structures (5+ GB)

  • 0

I am running an interactive python session which builds big python data-structures (5+ GB) which take a long time to load, and so I want to exploit Python on-the-fly code change abilities at its maximum (though sometimes, without having to plan too much for that).

My current problem is the following: I have an old instance of a class that I have later modified the code and reloaded the module — I would like the old instance to be able to use the new function definitions. How do I do that without just manually copying all the information from the old instance to a new fresh instance?

Here is what I have tried. Suppose I have the module M.py:

class A():
    def f(self):
        print "old class"

Here is an interactive session:

import M
old_a = M.a()

# [suppose now I change the definition of M.A.f in the source file]

reload(M)
# I attempt to use the new class definition with the old instance:
M.A.f(old_a)

at which point I get the following type error from Python:

TypeError: unbound method f() must be called with A instance as first argument (got A instance instead)

Python is obviously not happy to receive an old instance of A even though they are basically functionally equivalent types (in my code) — is there any way I could ‘type cast’ it to the new instance type so that Python wouldn’t complain? Something morally like: M.A.f( (M.A) old_a ) ?

  • 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-29T08:16:45+00:00Added an answer on May 29, 2026 at 8:16 am

    There is no casting in Python but you can change the class of an existing object: It is perfectly legal and does the job:

    old_a.__class__=M.A
    old_a.f()
    

    As long as you haven’t changed the relation between class methods and instance variables, changed what __init__ does or something like that this is perfectly fine.

    EDIT: As jsbueno points out: The __init__ or __new__ methods are not called at the point of changing __class__. Further, the new __del__ will be called at destruction.

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

Sidebar

Related Questions

I'm running Python 2.6 on Unix and when I run the interactive prompt (
I'm looking for something that provides interactive meta-data about a running .NET WinForms application.
I have a Python server which is not running as root, which fronts an
Running python on Snow Leopard, and I can't import the 'time' module. Works in
I have some long running scripts with breaks requiring input/interaction to continue but when
I am trying to install Python 2.6.5 on my web server running Debian 4.3.2.1-1.
I am using a interactive command line program in a Linux terminal running the
I am running python 3.1.4 from macports and I am attempting to use the
I'm running pywin32 for python 2.5. I'm following the instructions for python ADO given
I am trying to use ZODB 3.10.2 on my web server which is running

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.