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

  • Home
  • SEARCH
  • 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 535079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:40:44+00:00 2026-05-13T09:40:44+00:00

i think you can defined either ‘ __init__ ‘ or ‘ __new__ ‘ in

  • 0

i think you can defined either ‘__init__‘ or ‘__new__‘ in a class,but why all defined in django.utils.datastructures.py.

my code:

class a(object):
    def __init__(self):
        print  'aaa'
    def __new__(self):
        print 'sss'

a()#print 'sss'

class b:
    def __init__(self):
        print  'aaa'
    def __new__(self):
        print 'sss'
b()#print 'aaa'

datastructures.py:

class SortedDict(dict):
    """
    A dictionary that keeps its keys in the order in which they're inserted.
    """
    def __new__(cls, *args, **kwargs):
        instance = super(SortedDict, cls).__new__(cls, *args, **kwargs)
        instance.keyOrder = []
        return instance

    def __init__(self, data=None):
        if data is None:
            data = {}
        super(SortedDict, self).__init__(data)
        if isinstance(data, dict):
            self.keyOrder = data.keys()
        else:
            self.keyOrder = []
            for key, value in data:
                if key not in self.keyOrder:
                    self.keyOrder.append(key)

and what circumstances the SortedDict.__init__ will be call.

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-05-13T09:40:45+00:00Added an answer on May 13, 2026 at 9:40 am

    You can define either or both of __new__ and __init__.

    __new__ must return an object — which can be a new one (typically that task is delegated to type.__new__), an existing one (to implement singletons, “recycle” instances from a pool, and so on), or even one that’s not an instance of the class. If __new__ returns an instance of the class (new or existing), __init__ then gets called on it; if __new__ returns an object that’s not an instance of the class, then __init__ is not called.

    __init__ is passed a class instance as its first item (in the same state __new__ returned it, i.e., typically “empty”) and must alter it as needed to make it ready for use (most often by adding attributes).

    In general it’s best to use __init__ for all it can do — and __new__, if something is left that __init__ can’t do, for that “extra something”.

    So you’ll typically define both if there’s something useful you can do in __init__, but not everything you want to happen when the class gets instantiated.

    For example, consider a class that subclasses int but also has a foo slot — and you want it to be instantiated with an initializer for the int and one for the .foo. As int is immutable, that part has to happen in __new__, so pedantically one could code:

    >>> class x(int):
    ...   def __new__(cls, i, foo):
    ...     self = int.__new__(cls, i)
    ...     return self
    ...   def __init__(self, i, foo):
    ...     self.foo = foo
    ...   __slots__ = 'foo',
    ... 
    >>> a = x(23, 'bah')
    >>> print a
    23
    >>> print a.foo
    bah
    >>> 
    

    In practice, for a case this simple, nobody would mind if you lost the __init__ and just moved the self.foo = foo to __new__. But if initialization is rich and complex enough to be best placed in __init__, this idea is worth keeping in mind.

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

Sidebar

Ask A Question

Stats

  • Questions 387k
  • Answers 387k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use isset to prevent an any warnings or notices from… May 14, 2026 at 11:57 pm
  • Editorial Team
    Editorial Team added an answer My Domain/Business Model class contains a "Mode" property which is… May 14, 2026 at 11:57 pm
  • Editorial Team
    Editorial Team added an answer Query ReportDatas .Where( reportData => reportData.StartsWith( "Something%" ) && reportData.Report.Id… May 14, 2026 at 11:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.