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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:39:15+00:00 2026-05-16T21:39:15+00:00

Having a object x which is an instance of some class how to create

  • 0

Having a object x which is an instance of some class how to create a new instance of the same class as the x object, without importing that all possible classes in the same namespace in which we want to create a new object of the same type and using isinstance to figure out the correct type.

For example if x is a decimal number:

>>> from decimal import Decimal
>>> x = Decimal('3')
>>> x
Decimal('3')

how to create new instance of Decimal. I think the obvious thing to do would be either of these:

>>> type(x)('22')
Decimal('22')
>>> x.__class__('22')
Decimal('22')

Since __class__ will not work on int for example:

>>> 1.__class__
  File "<stdin>", line 1
    1.__class__

Is it good practice to use type to achieve this, or is there some other ways or more caveats when using this approach for creating new objects?

Note:
There was an answer that is now deleted that gave a right way to get __class__ of int.

>>> (1).__class__
<type 'int'>

Use case

The question is mostly theoretical, but I am using this approach right now with Qt to create new instances of QEvent. For example, since the QEvent objects are consumed by the application event handler in order to post the event to QStateMachine you need to create a new instance of the event otherwise you get runtime error because the underlying C++ object get deleted.

And since I am using custom QEvent subclasses that all share the same base class thus objects accept same predefined set of arguments.

  • 1 1 Answer
  • 2 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-16T21:39:15+00:00Added an answer on May 16, 2026 at 9:39 pm

    Calling type(x) is definitely the canonical way to create a new instance of exactly the same type as x. However, what arguments to pass to that call is not a given, because the “signature” (number and types of arguments to pass in the call) changes with every different type; so, if you have no idea of what the type might be, you need more introspection for this purpose (as well as some rule or heuristic about what all arguments you want to pass once you’ve determined, for example, that you may pass any number from 0 to 3, and that the optional/keyword argument names are ‘y’, ‘z’, ‘t’… obviously it’s impossible to establish a general rule here!).

    So, can you please clarify (once the type to instantiate is, easily, determined;-) how you’re going to be tackling the hard parts of your problem, that you don’t even mention? What constraints can you assume on the type’s signature? Do you need to perform some sanity checks on that or is it OK to just cause a TypeError by calling with the wrong kind or number of arguments? Etc, etc… without more info of this nature, your question just doesn’t lend itself to proving an answer that can actually be used in the real world!-)

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

Sidebar

Related Questions

Is it possible to find which method was called on an object without having
I am trying to build some classes that inherit from a parent class, which
I'm having some trouble creating a thread object in another class (to which it
I am trying to create some classes which only contain data members (no functions)
I am having a user object which has one-to-many relation with Address object. My
I have created an empty json object having an array itemlist(which further contains itemid
I am having trouble with Subsonic 3.0 generating an object for a table which
In UML, if there's a class A having an object of type B as
I've got a generic class that manages resources of all kinds of types, but
I'm relatively new to .NET and MVC3. I'm having some trouble with the above

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.