Python: __new__ magic method explained

https://howto.lintel.in/python-__new__-magic-method-explained/

Python: __new__ magic method explained

Python is Object oriented language, every thing is an object in python. Python is having special type of  methods called magic methods named with preceded and trailing double underscores.

When we talk about magic method __new__ we also need to talk about __init__

These methods will be called when you instantiate(The process of creating instance from class is called instantiation). That is when you create instance. The magic method __new__ will be called when instance is being created. Using this method you can customize the instance creation. This is only the method which will be called first then __init__ will be called to initialize instance when you are creating instance.

Method __new__ will take class reference as the first argument followed by arguments which are passed to constructor(Arguments passed to call of class to create instance). Method __new__ is responsible to create instance, so you can use this method to customize object creation. Typically method __new__ will return the created instance object reference. Method __init__ will be called once __new__ method completed execution.

You can create new instance of the class by invoking the superclass’s __new__ method using super. Something like super(currentclass, cls).__new__(cls, [,….])

Usual class declaration and instantiation

Python

1

2

3

4

5

6

7

8

9

classFoo(object):

def__init__(self,a,b):

self.a=a

self.b=b

defbar(self):

pass

i=Foo(2,3)

A class implementation with __new__ method overridden

Python

1

2

3

4

5

6

7

8

9

10

11

12

classFoo(object):

def__new__(cls,*args,**kwargs):

print"Creating Instance"

instance=super(Foo,cls).__new__(cls,*args,**kwargs)

returninstance

def__init__(self,a,b):

self.a=a

self.b=b

defbar(self):

pass

OutPut:

Python

1

2

>>>i=Foo(2,3)

Creating Instance

Note:

You can create instance inside __new__  method either by using superfunction or by directly calling __new__ method over object  Where if parent class is object. That is,

instance = super(MyClass, cls).__new__(cls, *args, **kwargs)

or

instance = object.__new__(cls, *args, **kwargs)

Things to remember

If __new__ return instance of  it’s own class, then the __init__ method of newly created instance will be invoked with instance as first (like __init__(self, [, ….]) argument following by arguments passed to __new__ or call of class.  So, __init__ will called implicitly.

If __new__ method return something else other than instance of class,  then instances __init__ method will not be invoked. In this case you have to call __init__ method yourself.

Applications

Usually it’s uncommon to override __new__ method, but some times it is required if you are writing APIs or customizing class or instance creation or abstracting something using classes.

SINGLETON USING __NEW__

You can implement the singleton design pattern using __new__ method. Where singleton class is a class that can only have one object. That is, instance of class.

Here is how you can restrict creating more than one instance by overriding __new__

Singleton implementation using __new__

Python

1

2

3

4

5

6

7

classSingleton(object):

_instance=None  # Keep instance reference

def__new__(cls,*args,**kwargs):

ifnotcls._instance:

cls._instance=object.__new__(cls,*args,**kwargs)

returncls._instance

It is not limited to singleton. You can also impose limit on total number created instances

Limiting total number of instances can be created using __new__

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

classLimitedInstances(object):

_instances=[]  # Keep track of instance reference

limit=5

def__new__(cls,*args,**kwargs):

ifnotlen(cls._instances)<=cls.limit:

raiseRuntimeError,"Count not create instance. Limit %s reached"%cls.limit

instance=object.__new__(cls,*args,**kwargs)

cls._instances.append(instance)

returninstance

def__del__(self):

# Remove instance from _instances

self._instance.remove(self)

CUSTOMIZE INSTANCE OBJECT

You can customize the instance created and make some operations over it before initializer __init__  being called.Also you can impose restriction on instance creation based on some constraints

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

defcreateInstance():

# Do what ever you want to determie if instance can be created

returnTrue

classCustomizeInstance(object):

def__new__(cls,a,b):

ifnotcreateInstance():

raiseRuntimeError,"Count not create instance"

instance=super(CustomizeInstance,cls).__new__(cls,a,b)

instance.a=a

returninstance

def__init__(self,a,b):

pass

Customize Returned Object

Usually when you instantiate class it will return the instance of that class.You can customize this behaviour and you can return some random object you want.

Following  one is simple example to demonstrate that returning random object other than class instance

Python

1

2

3

4

5

6

7

8

9

classAbstractClass(object):

def__new__(cls,a,b):

instance=super(AbstractClass,cls).__new__(cls)

instance.__init__(a,b)

return3

def__init__(self,a,b):

print"Initializing Instance",a,b

Output:

Python

1

2

3

4

>>>a=AbstractClass(2,3)

Initializing Instance23

>>>a

3

Here you can see when we instantiate class it returns  3 instead of instance reference. Because we are returning 3 instead of created instance from __new__ method. We are calling __init__ explicitly.  As I mentioned above, we have to call __init__ explicitly if we are not returning instance object from __new__ method.

The __new__ method is also used in conjunction with meta classes to customize class creation

Conclusion

There are many possibilities on how you can use this feature.  Mostly it is not always required to override __new__ method unless you are doing something regarding instance creation.

Simplicity is better than complexity. Try to make life easier use this method only if it is necessary to use.

Python: __new__ magic method explained

Python is Object oriented language, every thing is an object in python. Python is having special type of  methods called magic methods named with preceded and trailing double underscores.

When we talk about magic method __new__ we also need to talk about __init__

These methods will be called when you instantiate(The process of creating instance from class is called instantiation). That is when you create instance. The magic method __new__ will be called when instance is being created. Using this method you can customize the instance creation. This is only the method which will be called first then __init__ will be called to initialize instance when you are creating instance.

Method __new__ will take class reference as the first argument followed by arguments which are passed to constructor(Arguments passed to call of class to create instance). Method __new__ is responsible to create instance, so you can use this method to customize object creation. Typically method __new__ will return the created instance object reference. Method __init__ will be called once __new__ method completed execution.

You can create new instance of the class by invoking the superclass’s __new__ method using super. Something like super(currentclass, cls).__new__(cls, [,….])

Usual class declaration and instantiation

Python

1

2

3

4

5

6

7

8

9

classFoo(object):

def__init__(self,a,b):

self.a=a

self.b=b

defbar(self):

pass

i=Foo(2,3)

A class implementation with __new__ method overridden

Python

1

2

3

4

5

6

7

8

9

10

11

12

classFoo(object):

def__new__(cls,*args,**kwargs):

print"Creating Instance"

instance=super(Foo,cls).__new__(cls,*args,**kwargs)

returninstance

def__init__(self,a,b):

self.a=a

self.b=b

defbar(self):

pass

OutPut:

Python

1

2

>>>i=Foo(2,3)

Creating Instance

Note:

You can create instance inside __new__  method either by using superfunction or by directly calling __new__ method over object  Where if parent class is object. That is,

instance = super(MyClass, cls).__new__(cls, *args, **kwargs)

or

instance = object.__new__(cls, *args, **kwargs)

Things to remember

If __new__ return instance of  it’s own class, then the __init__ method of newly created instance will be invoked with instance as first (like __init__(self, [, ….]) argument following by arguments passed to __new__ or call of class.  So, __init__ will called implicitly.

If __new__ method return something else other than instance of class,  then instances __init__ method will not be invoked. In this case you have to call __init__ method yourself.

Applications

Usually it’s uncommon to override __new__ method, but some times it is required if you are writing APIs or customizing class or instance creation or abstracting something using classes.

SINGLETON USING __NEW__

You can implement the singleton design pattern using __new__ method. Where singleton class is a class that can only have one object. That is, instance of class.

Here is how you can restrict creating more than one instance by overriding __new__

Singleton implementation using __new__

Python

1

2

3

4

5

6

7

classSingleton(object):

_instance=None  # Keep instance reference

def__new__(cls,*args,**kwargs):

ifnotcls._instance:

cls._instance=object.__new__(cls,*args,**kwargs)

returncls._instance

It is not limited to singleton. You can also impose limit on total number created instances

Limiting total number of instances can be created using __new__

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

classLimitedInstances(object):

_instances=[]  # Keep track of instance reference

limit=5

def__new__(cls,*args,**kwargs):

ifnotlen(cls._instances)<=cls.limit:

raiseRuntimeError,"Count not create instance. Limit %s reached"%cls.limit

instance=object.__new__(cls,*args,**kwargs)

cls._instances.append(instance)

returninstance

def__del__(self):

# Remove instance from _instances

self._instance.remove(self)

CUSTOMIZE INSTANCE OBJECT

You can customize the instance created and make some operations over it before initializer __init__  being called.Also you can impose restriction on instance creation based on some constraints

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

defcreateInstance():

# Do what ever you want to determie if instance can be created

returnTrue

classCustomizeInstance(object):

def__new__(cls,a,b):

ifnotcreateInstance():

raiseRuntimeError,"Count not create instance"

instance=super(CustomizeInstance,cls).__new__(cls,a,b)

instance.a=a

returninstance

def__init__(self,a,b):

pass

Customize Returned Object

Usually when you instantiate class it will return the instance of that class.You can customize this behaviour and you can return some random object you want.

Following  one is simple example to demonstrate that returning random object other than class instance

Python

1

2

3

4

5

6

7

8

9

classAbstractClass(object):

def__new__(cls,a,b):

instance=super(AbstractClass,cls).__new__(cls)

instance.__init__(a,b)

return3

def__init__(self,a,b):

print"Initializing Instance",a,b

Output:

Python

1

2

3

4

>>>a=AbstractClass(2,3)

Initializing Instance23

>>>a

3

Here you can see when we instantiate class it returns  3 instead of instance reference. Because we are returning 3 instead of created instance from __new__ method. We are calling __init__ explicitly.  As I mentioned above, we have to call __init__ explicitly if we are not returning instance object from __new__ method.

The __new__ method is also used in conjunction with meta classes to customize class creation

Conclusion

There are many possibilities on how you can use this feature.  Mostly it is not always required to override __new__ method unless you are doing something regarding instance creation.

Simplicity is better than complexity. Try to make life easier use this method only if it is necessary to use.

原文地址:https://www.cnblogs.com/wdmx/p/10076007.html

时间: 2024-10-03 23:03:53

Python: __new__ magic method explained的相关文章

magic method细解python一直让我疑惑的几个常用魔法方法(上)

这里只分析几个可能会常用到的魔法方法,像__new__这种不常用的,用来做元类初始化的或者是__init__这种初始化使用的 每个人都会用的就不介绍了. 其实每个魔法方法都是在对内建方法的重写,和做像装饰器一样的行为.理解这个道理 再尝试去理解每个细节装饰器会比较方便. 关于__str__和__repr__: 直接上例子: class Test(object): def __init__(self, world): self.world = world def __str__(self): re

python __new__以及__init__

@[深入Python]__new__和__init__ 1 2 3 4 5 6 7 8 class A(object):     def __init__(self):         print "init"     def __new__(cls,*args, **kwargs):         print "new %s"%cls         return object.__new__(cls, *args, **kwargs) A() 输出: new

跟黄哥学python序列文章之python方法链(method chaining)

# 跟黄哥学python序列文章之python方法链(method chaining) ## 写这篇文章来由,有朋友说下面这样的代码看不懂. choice = raw_input("please input:\n").strip()[0].lower() 很多对于有经验的程序员来说,这些都不是事, 但对于初学者来说,看到这样的语法头有点大. ## 这个其实是面向对象中方法链的概念. ## 请看维基百科上Method chaining的定义 Method chaining, also k

PHP Magic Method Setter and Getter

<?php /* Magic method __set() and __get() 1.The definition of a magic function is provided by the programmer – meaning you, as the programmer, will actually write the definition. This is important to remember – PHP does not provide the definitions of

Python — magic method

Python 中内置的特定的方法, 这些方法在进行特定的操作时,会自动被调用. __init__(self, [ ])  # 类的构造函数, 用于初始化实例变量 __new__(cls, [ ]) # 类的内置方法 __del__(self)       # 当对象的引用为 0 时调用 该方法, 并不是del 时调用该方法 算术运算的 内置方法.对象可以进行算术运算,可通过修改这些方法,改变对象的行为 定义一个定时器的类,并通过重载其内部方法实现,定时器开始,结束,结果显示,及定时器对象的 加

流动python - 什么是魔术方法(magic method)

我们经常看到各种各样的方法已经被包围了由双下划线,例如__init__,他们是魔术方法. 魔术方法python语言预订好"协议",在不同情况下不同的魔术方法,是隐式调用.我们重写这些方法,因此,操纵各种行为. class A(object): def __str__(self): return "I am A,2333" def __len__(self): return 42 a = A() print a#输出 "I am A,2333" p

飘逸的python - 什么是魔术方法(magic method)

我们经常看到各种被双下划线环绕的方法,如__init__,它们就是魔术方法. 魔术方法是python语言预定好的"协议",不同魔术方法在不同场景下,会被隐式调用.我们通过重载这些方法,从而操控各种行为. class A(object): def __str__(self): return "I am A,2333" def __len__(self): return 42 a = A() print a#输出 "I am A,2333" prin

python __new__()和__init__()哪个更早?

通过代码验证是最靠谱的: class Foo(object): def __init__(self): print 'foo init' def __new__(cls,*args,**kwargs): print 'foo new' return object.__new__(cls,*args,**kwargs) foo = Foo() print type(foo) 结果: >>> foo new foo init <class '__main__.Foo'> >

飘逸的python - __new__、__init__、__call__傻傻分不清

__new__: 对象的创建,是一个静态方法.第一个參数是cls.(想想也是,不可能是self,对象还没创建,哪来的self) __init__ : 对象的初始化, 是一个实例方法,第一个參数是self. __call__ : 对象可call.注意不是类,是对象. 先有创建,才有初始化.即先__new__,而后__init__. 上面说的不好理解,看样例. 对于__new__ class Bar(object): pass class Foo(object): def __new__(cls,