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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:19:19+00:00 2026-05-26T18:19:19+00:00

I am learning Python and so far I can tell the things below about

  • 0

I am learning Python and so far I can tell the things below about __new__ and __init__:

  1. __new__ is for object creation
  2. __init__ is for object initialization
  3. __new__ is invoked before __init__ as __new__ returns a new instance and __init__ invoked afterwards to initialize inner state.
  4. __new__ is good for immutable object as they cannot be changed once they are assigned. So we can return new instance which has new state.
  5. We can use __new__ and __init__ for both mutable object as its inner state can be changed.

But I have another questions now.

  1. When I create a new instance such as a = MyClass("hello","world"), how these arguments are passed? I mean how I should structure the class using __init__ and __new__ as they are different and both accepts arbitrary arguments besides default first argument.
  2. self keyword is in terms of name can be changed to something else? But I am wondering cls is in terms of name is subject to change to something else as it is just a parameter name?

I made a little experiments as such below:

>>> class MyClass(tuple):
    def __new__(tuple):
        return [1,2,3]

and I did below:

>>> a = MyClass()
>>> a
[1, 2, 3]

Albeit I said I want to return tuple, this code works fine and returned me [1,2,3]. I knew we were passing the first parameters as the type we wanted to receive once the __new__ function is invoked. We are talking about New function right? I don’t know other languages return type other than bound type?

And I did anther things as well:

>>> issubclass(MyClass,list)
False
>>> issubclass(MyClass,tuple)
True
>>> isinstance(a,MyClass)
False
>>> isinstance(a,tuple)
False
>>> isinstance(a,list)
True

I didn’t do more experiment because the further wasn’t bright and I decided to stop there and decided to ask StackOverflow.

The SO posts I read:

  1. Python object creation
  2. Python's use of __new__ and __init__?
  • 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-26T18:19:20+00:00Added an answer on May 26, 2026 at 6:19 pm

    how I should structure the class using __init__ and __new__ as they are different and both accepts arbitrary arguments besides default first argument.

    Only rarely will you have to worry about __new__. Usually, you’ll just define __init__ and let the default __new__ pass the constructor arguments to it.

    self keyword is in terms of name can be changed to something else? But I am wondering cls is in terms of name is subject to change to something else as it is just a parameter name?

    Both are just parameter names with no special meaning in the language. But their use is a very strong convention in the Python community; most Pythonistas will never change the names self and cls in these contexts and will be confused when someone else does.

    Note that your use of def __new__(tuple) re-binds the name tuple inside the constructor function. When actually implementing __new__, you’ll want to do it as

    def __new__(cls, *args, **kwargs):
        # do allocation to get an object, say, obj
        return obj
    

    Albeit I said I want to return tuple, this code works fine and returned me [1,2,3].

    MyClass() will have the value that __new__ returns. There’s no implicit type checking in Python; it’s the responsibility of the programmer to return the correct type (“we’re all consenting adults here”). Being able to return a different type than requested can be useful for implementing factories: you can return a subclass of the type requested.

    This also explains the issubclass/isinstance behavior you observe: the subclass relationship follows from your use of class MyClass(tuple), the isinstance reflects that you return the “wrong” type from __new__.

    For reference, check out the requirements for __new__ in the Python Language Reference.

    Edit: ok, here’s an example of potentially useful use of __new__. The class Eel keeps track of how many eels are alive in the process and refuses to allocate if this exceeds some maximum.

    class Eel(object):
        MAX_EELS = 20
        n_eels = 0
    
        def __new__(cls, *args, **kwargs):
            if cls.n_eels == cls.MAX_EELS:
                raise HovercraftFull()
    
            obj = super(Eel, cls).__new__(cls)
            cls.n_eels += 1
            return obj
    
        def __init__(self, voltage):
            self.voltage = voltage
    
        def __del__(self):
            type(self).n_eels -= 1
    
        def electric(self):
            """Is this an electric eel?"""
            return self.voltage > 0
    

    Mind you, there are smarter ways to accomplish this behavior.

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

Sidebar

Related Questions

I've started learning Python 3 - and now so far that I need some
I am learning Python (2.7) and to test what I have learned so far
I'm learning Python with the tutorial app from Flask and I can't figure how
I'm learning Python and have been trying to understand more about the details of
How well do Python machine learning algorithms scale for web applications? So far, I
I am learning Python for a class now, and we just covered tuples as
I've been learning python for a while now with some success. I even managed
I recently started learning Python and I was rather surprised to find a 1000
WARNING: I have been learning Python for all of 10 minutes so apologies for
I'm learning Python now because of the Django framework. I have been a Perl

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.