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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:45:22+00:00 2026-06-14T11:45:22+00:00

I need to create a class that mimics this behavior (in mathematics, we say

  • 0

I need to create a class that mimics this behavior (in mathematics, we say list, dict, are “idempotent”):

>>> list(list([3,4]))
[3, 4]
>>> dict({'a':1,'b':2})
{'a':1,'b':2}

So, if A is my class, I want to write

>>> a = A(1)
>>> b = A(a)
>>> b == a
True

I imagine my class A has to look like this :

class A(object):
   def __init__(self,x):
       if isinstance(x, A) : 
           self = x
       else : 
           self.x = x
           self.y = 'hello'

I try it

>>> A(1).x
1
>>> A(A(1)).x
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'A' object has no attribute 'x'

It does not work !

I don’t want to copy x attributes in self, i just want self to BE x or “point” x

Some idea ?
Thanks

  • 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-14T11:45:23+00:00Added an answer on June 14, 2026 at 11:45 am

    What you are looking for is the __new__() method, which takes is run before the class is constructed, as opposed to __init__(), which takes place after. With __new__() you can hook in and replace the object being created.

    def __new__(cls, x):
        if isinstance(x, A):
            return x
        else:
            return object.__new__(cls, x)
    

    You can’t do this in __init__() as the object has already been created. Changing self simply changes the value of the local variable, it doesn’t affect the object.

    It’s also worth noting that type-checking is almost always the wrong thing to do in Python. Instead, check to see if the class has the information/attributes you need. This way, someone can create a class that acts like yours and works with your code.

    As a final word of warning, this is pretty confusing behaviour – people won’t expect your class to act like this and it’s generally not a great idea. Your example of list() and dict() isn’t accurate to what you are doing here, as list(some_list) does not give some_list, it gives a new list which is a copy of some_list – the same is true for dict():

    >>> x = [1, 2, 3]
    >>> list(x) is x
    False 
    

    When you call a constructor, it’s natural to expect a new object, rather than a reference to the existing one. I would recommend making A(some_a) copy some_a, and restructure your calling code not to rely on A(some_a) is some_a).

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

Sidebar

Related Questions

I've been using this pattern whenever I need to create a class that might
I need to create a class that will store a unique object elements. I
I need to create class Dog and PurebredDog extending Dog. Problem is that Dog
I need to create a class to handle audio AudioTrack. In this class must
Why do i need to create an instance of Random class, if i want
I've got a need to create a dynamic proxy in C#. I want this
Need to create a class that will do all things as the merge function.
I've got class A that implements IA. Now I need to create class B
I need to create a class that represent SVN inside a module called SCM.
I need to create a class that will present a UIVIew and has a

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.