zoukankan      html  css  js  c++  java
  • 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

    A class implementation with __new__ method overridden

    OutPut:

    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 objectThat 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__

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

     

    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

     

    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

    Output:

    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

    A class implementation with __new__ method overridden

    OutPut:

    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 objectThat 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__

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

     

    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

     

    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

    Output:

    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.

  • 相关阅读:
    设计手稿: 搜索引擎
    软件版本介绍
    VS2012中使用编译的Qt-5.1.1静态库开发程序
    POJ2236(并查集)
    Java关键字this的用法总结
    paip.提升用户体验-----c++ gcc 命令在notepad++扩展中的配置..
    MySQL基本查询语句练习
    [置顶] 提升代码内外部质量的22条经验
    mysql 数据库复制表 create table city1 like city;
    两个脚本
  • 原文地址:https://www.cnblogs.com/wdmx/p/10076007.html
Copyright © 2011-2022 走看看