zoukankan      html  css  js  c++  java
  • Python之美[从菜鸟到高手]--NotImplemented小析

    今天写代码时无意碰到NotImplemented,我一愣。难道是NotImplementedError的胞弟,所以略微研究了一下。

    NotImplemented故名思议。就是“未实现”。通常是用在一些比較算法中的,如class的__eq__,__lt__等,注意NotImplemented并非异常,所以不能

    使用raise,当没有实现时应该是return NotImplemented。

    我们能够看看django中的Field的实现,

    @total_ordering
    class Field(object):
        """Base class for all field types"""
        def __eq__(self, other):
            # Needed for @total_ordering
            if isinstance(other, Field):
                return self.creation_counter == other.creation_counter
            return NotImplemented
    
        def __lt__(self, other):
            # This is needed because bisect does not take a comparison function.
            if isinstance(other, Field):
                return self.creation_counter < other.creation_counter
            return NotImplemented
    

    那提供NotImplemented有什么优点?优点就在于假设A == B NotImplemented,会去调用B的__eq__方法,假设B也没有会调用cmp方法。

    我们看以下一个样例:

    class Person:
        def __init__(self, age):
            self.age = age
    
        def __eq__(self, other):
            if not isinstance(other, Person):
                return NotImplemented
            return self.age == other.age

    假设你们稳定库中有这么一段代码。并且Person可能包括了非常多字段。可是你想实现Person和整数比較。

    person=Person(10)
    print person == 10 #False
    非常遗憾,上面结果是False,不符合我们要求,至于为什么是10,稍后再说,我们先看看怎样解决问题?

    事实上这时你能够简单封装一个age对象,

    class Age:
        def __init__(self, age):
            self.age = age
    
        def __eq__(self, other):
            return self.age == other.age 
    
    person=Person(10)
    age=Age(10)
    print person == age #True
    ok,非常完美,那我们从上面能得到什么结论呢?

    我们在写一些基础代码时,即使是没实现。也不要raise NotImplementedError,而是return NotImplemented,相当于提供给其他不同对象的比較接口,这对

    代码扩展非常有优点。


    我们再来看看上面那么直接和10比,为什么是False?

    先看以下这段代码:

    class A:
        def __lt__(self, a):
            return NotImplemented
    
        def __add__(self ,a):
            return NotImplemented
    
    print A() < A() # True
    print A() < 1  # False
    非常奇怪吧,明明已经直接是NotImplemented,为什么还有结果?
    大胆推測,前面说明最后会使用cmp比較,非常明显当都未定义时会比較id值。也就是内存地址。后创建的对象内存地址大,就是这个道理。

    至于A() < 1,由于python的小整数对象在初始化时创建的。内存地址肯定小。假设你还不信。


    只是千万别搞没有意思的操作。


    这也是这位兄弟不解的地方,http://stackoverflow.com/questions/1062096/python-notimplemented-constant



  • 相关阅读:
    集合容器概述
    enum枚举类型
    this关键字、this()、super()
    重载与重写
    nginx报404的可能错误
    nginx常用命令
    vbs系统监控
    VBS windows监控
    Oracle SQL优化[转]
    shell /bin/bash^M: bad interpreter错误解决
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6724376.html
Copyright © 2011-2022 走看看