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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:35:01+00:00 2026-06-12T02:35:01+00:00

In learning about Python’s data model, I am playing with creating objects from existing

  • 0

In learning about Python’s data model, I am playing with creating objects from existing objects using the __new__ method. Here are some examples which create new objects of various types:

x = 2;     print type(x).__new__(x.__class__)
x = {};    print type(x).__new__(x.__class__)
x = [1,2]; print type(x).__new__(x.__class__)
x = 2.34;  print type(x).__new__(x.__class__)
x = '13';  print type(x).__new__(x.__class__)
x = 1.0j;  print type(x).__new__(x.__class__)
x = True;  print type(x).__new__(x.__class__)
x = (1,2); print type(x).__new__(x.__class__)

However, the following three experiments give me errors:

x = None;           print type(x).__new__(x.__class__)
x = lambda z: z**2; print type(x).__new__(x.__class__)
x = object;         print type(x).__new__(x.__class__)

The errors are (respectively):

TypeError: object.__new__(NoneType) is not safe, use NoneType.__new__()
TypeError: Required argument 'code' (pos 1) not found
TypeError: type() takes 1 or 3 arguments

Why don’t these three examples work? (Note: for the lambda example it appears that I have to pass in a code fragment when I invoke the __new__ method but I don’t know how to do that.) I am using Python 2.6.

Please note that I realize this is not necessarily the way you would want to create new objects in real code, but my purpose is not practical, rather, it is to understand how the low-level object methods work.

  • 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-12T02:35:03+00:00Added an answer on June 12, 2026 at 2:35 am

    It’s nothing too special, it’s just that for some types there is a default “empty” object of that type, while for others there is not. Your working examples are basically equivalent to:

    int()
    dict()
    list()
    float()
    str()
    complex()
    tuple()
    

    . . . all of which work. Your last three examples are basically trying to create new instances of NoneType, function, and type.

    1. NoneType fails for a unique reason, because None is a singleton — the NoneType type can only ever have one instance, namely the None object. (The specific error message you get is a little strange, but if you do types.NoneType() you’ll get a more direct message saying “Can’t create NoneType instances”.)
    2. function fails because, as you saw, it requires an argument, and you don’t provide one. What you’d need is a code object, which you could get from an existing function or from the compile function. (It also requires a globals argument, which can just be a dict.)
    3. type fails because you didn’t give enough arguments. You can either do type(foo) to get the type of foo, or type(name, bases, dict) to create a new type (i.e., a class).

    Notice incidentally that in your last example you are taking the type of object, which is itself a type. If you do x = object() instead (making x be an individual object rather than the object type) then it will work and create a “blank” object.

    The thing to remember is that calling __new__ is not really that magical. It is just what happens when you directly try to instantiate the type by doing someType(). If that type requires arguments, calling __new__ will fail, just like any other function call will fail if you don’t give it the right arguments, because type(x).__new__ is just a function like any other function. You can see this with a user-defined class:

    >>> class Foo(object):
    ...     pass
    >>> x = Foo();           print type(x).__new__(x.__class__)
    <__main__.Foo object at 0x00EB0370>
    >>> class Foo(object):
    ...     def __init__(self, someArg):
    ...         pass
    >>> class Foo(object):
    ...     def __new__(cls, someArg):
    ...         return super(Foo, cls).__new__(cls)
    >>> x = Foo(2);           print type(x).__new__(x.__class__)
    Traceback (most recent call last):
      File "<pyshell#32>", line 1, in <module>
        x = Foo(2);           print type(x).__new__(x.__class__)
    TypeError: __new__() takes exactly 2 arguments (1 given)
    

    It succeeded in the first case, because my class didn’t require arguments; it failed in the second class, because the second class does require arguments.

    The __new__ does not fail for any secret reason; it just fails because the type you are trying to instantiate requires arguments in order to construct an instance. (The None case is the only one that’s different here; that one does fail for a special reason, because None is a special object in Python.)

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

Sidebar

Related Questions

I've just started learning about Gimp scripting using Python, and was wondering, how do
I am about to embark on learning Python (largely for the purposes of using
I'm learning about Python Packages from Learn Python the Hard Way and one of
I'm just learning about python. I'm fairly new. I have the following code that
I began learning Python a few days ago, and i was wondering about a
I am learning about raw sockets. I heard that ping utility is using raw
I have been learning about Delegation and Data Sources for iOS programming and need
I'm learning about metaclasses in Python. I think it is a very powerful technique,
I recently began learning Python, and I am a bit confused about how packages
Now, I'm learning python but I'm PHP web developer. I don't interest about terminal

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.