Why is the _ _ new__ (), for python source object object static?

class object:
    """ The most base type """
    ...

    @staticmethod -sharp known case of __new__
    def __new__(cls, *more): -sharp known special case of object.__new__
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass
    
    ...

the _ _ new__ method here is strange. Isn"t the first parameter bound to the class (that is, the source object object itself)?

I have several questions:

  1. Why is it not a class method?
  2. is this a special case of a static method decorator?
  3. is there any other place to use this besides object?

look forward to your answers

Dec.31,2021

having figured it out, I now list my personal opinions

as the source object of python, object is the ancestor of all class objects, (base), including another source object, type. In fact, the first parameter of the _ _ new__ method just happens to be set to cls, not to bind an instance object, so you can answer

like this.
  1. is a static method, because _ _ new__ () is just a common method for binding the source object object here, and in the general sense, _ _ new__ () is indeed a class method
  2. .
  3. No, all static methods are just ordinary methods that bind class objects or instance objects
  4. No, it actually confuses the ordinary position argument of cls with the cls position argument that represents the class object in the class method

it needs to be added that instances of source object types are also classes and can be further instantiated. They can often be used as the basic data type descriptors of some frameworks, and mandatory type checking can be performed. For example, the ModelBase, of django's ORM is inherited by all model as metaclass, which involves something about metaprogramming

.

with an explanation of the official documents and some discussions on this thing on stackoverflow, it is clear that it is more useful to read more official documents

.

1. python.org/3/reference/datamodel.html-sharpobject.__new__" rel=" nofollow noreferrer "> documentation

object.__new__ (cls [,.])

Called to create a new instance of class cls. _ _ new__ () is a static
method (special-cased so you need not declare it as such) that takes
the class of which an instance was requested as its first argument.
The remaining arguments are those passed to the object constructor
expression (the call to the class). The return value of _ new__ ()
should be the new object instance (usually an instance of cls).

Typical implementations create a new instance of the class by invoking
the superclass's _ new__ () method using super (). _ new__ (cls [,.])
with appropriate arguments and then modifying the newly-created
instance as necessary before returning it.

If _ new__ () returns an instance of cls, then the new instance's
_ init__ () method will be invoked like _ init__ (self [,.]), where self is the new instance and the remaining arguments are the same as
were passed to _ new__ ().

If _ new__ () does not return an instance of cls, then the new
instance's _ init__ () method will not be invoked.

_ _ new__ () is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also
commonly overridden in custom metaclasses in order to customize class
creation.

2. python" rel=" nofollow noreferrer "> StackOverflow


A static method does not receive an implicit first argument. To declare a static method, use this idiom:
A class method receives the class as implicit first argument, just like an instance method receives the instance.
is excerpted from builtins.py module

I think the reason for calling _ _ new__ static method is that each time this function is called, explicitly passes cls as the first argument

.
Menu