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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:56:25+00:00 2026-06-13T23:56:25+00:00

Say I have a class, which has a number of subclasses. I can instantiate

  • 0

Say I have a class, which has a number of subclasses.

I can instantiate the class. I can then set its __class__ attribute to one of the subclasses. I have effectively changed the class type to the type of its subclass, on a live object. I can call methods on it which invoke the subclass’s version of those methods.

So, how dangerous is doing this? It seems weird, but is it wrong to do such a thing? Despite the ability to change type at run-time, is this a feature of the language that should completely be avoided? Why or why not?

(Depending on responses, I’ll post a more-specific question about what I would like to do, and if there are better alternatives).

  • 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-13T23:56:26+00:00Added an answer on June 13, 2026 at 11:56 pm

    Here’s a list of things I can think of that make this dangerous, in rough order from worst to least bad:

    • It’s likely to be confusing to someone reading or debugging your code.
    • You won’t have gotten the right __init__ method, so you probably won’t have all of the instance variables initialized properly (or even at all).
    • The differences between 2.x and 3.x are significant enough that it may be painful to port.
    • There are some edge cases with classmethods, hand-coded descriptors, hooks to the method resolution order, etc., and they’re different between classic and new-style classes (and, again, between 2.x and 3.x).
    • If you use __slots__, all of the classes must have identical slots. (And if you have the compatible but different slots, it may appear to work at first but do horrible things…)
    • Special method definitions in new-style classes may not change. (In fact, this will work in practice with all current Python implementations, but it’s not documented to work, so…)
    • If you use __new__, things will not work the way you naively expected.
    • If the classes have different metaclasses, things will get even more confusing.

    Meanwhile, in many cases where you’d think this is necessary, there are better options:

    • Use a factory to create an instance of the appropriate class dynamically, instead of creating a base instance and then munging it into a derived one.
    • Use __new__ or other mechanisms to hook the construction.
    • Redesign things so you have a single class with some data-driven behavior, instead of abusing inheritance.

    As a very most common specific case of the last one, just put all of the “variable methods” into classes whose instances are kept as a data member of the “parent”, rather than into subclasses. Instead of changing self.__class__ = OtherSubclass, just do self.member = OtherSubclass(self). If you really need methods to magically change, automatic forwarding (e.g., via __getattr__) is a much more common and pythonic idiom than changing classes on the fly.

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

Sidebar

Related Questions

Say I have a class Customer which has a property FirstName . Then I
Let's say I have an class called Star which has an attribute color .
I have a ViewModel class which has large number of properties(Say 50). Once the
Let's say we have a class foo which has a private instance variable bar
Let's say I have a Size class which has height and width properties (in
Say I have a Date, or a Phone number, or some Class which I
I have a class which has a large number of static fields of a
Say I have a class which represents a person, a variable within that class
Possible Duplicate: Overload ++ operator Say i have a class in which i overloads
I have FrameChangeAnimation class which takes multiple spritesheets and duration. So lets say I

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.