首页 \ 问答 \ __name__上的抽象属性未强制执行(Abstract Property on __name__ not enforced)

__name__上的抽象属性未强制执行(Abstract Property on __name__ not enforced)

请考虑以下示例代码:

from abc import ABC, abstractmethod, abstractproperty

class Base(ABC):

    @abstractmethod
    def foo(self) -> str:
        print("abstract")

    @property
    @abstractmethod
    def __name__(self) -> str:
        return "abstract"

    @abstractmethod
    def __str__(self) -> str:
        return "abstract"

    @property
    @abstractmethod
    def __add__(self, other) -> str:
        return "abstract"


class Sub(Base):

    def foo(self):
        print("concrete")

    def __str__(self):
        return "concrete"

    def __add__(self, other) -> str:
        return "concrete"


sub = Sub()
sub.foo()
sub.__name__
print(str(sub))

请注意,子类没有实现抽象属性__name__ ,实际上当引用__name__时,它从父节点打印为“abstract”:

>>> sub.foo()
concrete
>>> sub.__name__
'abstract'
>>> print(str(sub))
concrete

但是,这不是因为__name__是一个dunder方法,也不是因为__name__@abstractmethod装饰器的某些问题不能很好地协同工作,因为如果我从Sub删除__add__的实现,它不会让我实例化它。 (我知道__add__通常不是属性,但我想使用'真正的'dunder方法)如果删除__str__foo的实现,会发生同样的预期行为。 只有__name__这样。

什么是__name__引起这种行为? 有没有办法解决这个问题,或者我是否需要手动为父(抽象)实现引发TypeError


Consider the following sample code:

from abc import ABC, abstractmethod, abstractproperty

class Base(ABC):

    @abstractmethod
    def foo(self) -> str:
        print("abstract")

    @property
    @abstractmethod
    def __name__(self) -> str:
        return "abstract"

    @abstractmethod
    def __str__(self) -> str:
        return "abstract"

    @property
    @abstractmethod
    def __add__(self, other) -> str:
        return "abstract"


class Sub(Base):

    def foo(self):
        print("concrete")

    def __str__(self):
        return "concrete"

    def __add__(self, other) -> str:
        return "concrete"


sub = Sub()
sub.foo()
sub.__name__
print(str(sub))

Note that the subclass does not implement the abstract property __name__, and indeed when __name__ is referenced, it prints as "abstract" from its parent:

>>> sub.foo()
concrete
>>> sub.__name__
'abstract'
>>> print(str(sub))
concrete

However, it is not because __name__ is a dunder method, nor because of some issue with @property and @abstractmethod decorators not working well together, because if I remove the implementation of __add__ from Sub, it does not let me instantiate it. (I know __add__ is not normally a property, but I wanted to use a 'real' dunder method) The same expected behavior occurs if I remove the implementation of __str__ and foo. Only __name__ behaves this way.

What is it about __name__ that causes this behavior? Is there some way around this, or do I need to have the parent (abstract) implementation manually raise the TypeError for me?


原文:https://stackoverflow.com/questions/41173879
更新时间:2022-04-30 22:04

最满意答案

我们来看看生成的字节码:

fun <T> getValue(): T {
    return 1000 as T
}

// becomes

public final getValue()Ljava/lang/Object;
   L0
    LINENUMBER 17 L0
    SIPUSH 1000
    INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
    CHECKCAST java/lang/Object
    ARETURN
   L1
    LOCALVARIABLE this LSimpleClass; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1

正如您所看到的,此方法不会将 1000转换为Long它只是确保对象的类型为java/lang/Object (嗯,它是)并返回1000作为Integer对象。

因此,您可以使用任何类型调用(注意:仅调用)此方法,这不会引发异常。 但是,将结果存储在变量中会调用实际强制转换,这可能会导致ClassCastException

fun f3() {
    val simpleObject = SimpleClass()
    // L0
    //   LINENUMBER 16 L0
    //   NEW SimpleClass
    //   DUP
    //   INVOKESPECIAL SimpleClass.<init> ()V
    //   ASTORE 0

    simpleObject.getValue<SimpleClass>() // no problems
    // L1
    //   LINENUMBER 17 L1
    //   ALOAD 0
    //   INVOKEVIRTUAL SimpleClass.getValue ()Ljava/lang/Object;
    //   POP

    val number = simpleObject.getValue<SimpleClass>() // throws ClassCastException1
    // L2
    //   LINENUMBER 18 L2
    //   ALOAD 0
    //   INVOKEVIRTUAL SimpleClass.getValue ()Ljava/lang/Object;
    //   CHECKCAST SimpleClass
    //   ASTORE 1


    // L3
    //   LINENUMBER 19 L3
    //   RETURN
    // L4
    //   LOCALVARIABLE number LSimpleClass; L3 L4 1
    //   LOCALVARIABLE simpleObject LSimpleClass; L1 L4 0
    //   MAXSTACK = 2
    //   MAXLOCALS = 2
}

但为什么将结果存储为Long? 抛出异常? 再说一次,我们来看看字节码的差异:

var number: Long? = null              |    var number: Long = 0
                                      |
      ACONST_NULL                     |        LCONST_0
      CHECKCAST java/lang/Long        |        LSTORE 0                
      ASTORE 0                        |

                number = simpleObject.getValue<Long>() [both]

      ALOAD 1                         |
              INVOKEVIRTUAL SimpleClass.getValue ()Ljava/lang/Object; [both]
      CHECKCAST java/lang/Long        |        CHECKCAST java/lang/Number
      ASTORE 0                        |        INVOKEVIRTUAL java/lang/Number.longValue ()J
                                      |        LSTORE 0

如您所见, number: Long的字节码将函数结果转换为Number ,然后调用Number.longValue以将值转换为Long (Java中的long

但是,数字的字节码number: Long? 将函数结果直接转换为Long? (在Java中很Long )导致ClassCastException

不确定,如果这种行为记录在某处。 但是, as运算符执行不安全的转换,编译器会警告它:

Warning:(21, 16) Kotlin: Unchecked cast: kotlin.Int to T

Let's take a look into generated bytecode:

fun <T> getValue(): T {
    return 1000 as T
}

// becomes

public final getValue()Ljava/lang/Object;
   L0
    LINENUMBER 17 L0
    SIPUSH 1000
    INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
    CHECKCAST java/lang/Object
    ARETURN
   L1
    LOCALVARIABLE this LSimpleClass; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1

As you can see, this method does not cast 1000 to a Long it simply ensures, that the object is of type java/lang/Object (well, it is) and returns the 1000 as an Integer object.

Therefore, you may call (note: call only) this method with any type and this will not throw exception. However, storing the result in a variable invokes real cast which may lead to ClassCastException

fun f3() {
    val simpleObject = SimpleClass()
    // L0
    //   LINENUMBER 16 L0
    //   NEW SimpleClass
    //   DUP
    //   INVOKESPECIAL SimpleClass.<init> ()V
    //   ASTORE 0

    simpleObject.getValue<SimpleClass>() // no problems
    // L1
    //   LINENUMBER 17 L1
    //   ALOAD 0
    //   INVOKEVIRTUAL SimpleClass.getValue ()Ljava/lang/Object;
    //   POP

    val number = simpleObject.getValue<SimpleClass>() // throws ClassCastException1
    // L2
    //   LINENUMBER 18 L2
    //   ALOAD 0
    //   INVOKEVIRTUAL SimpleClass.getValue ()Ljava/lang/Object;
    //   CHECKCAST SimpleClass
    //   ASTORE 1


    // L3
    //   LINENUMBER 19 L3
    //   RETURN
    // L4
    //   LOCALVARIABLE number LSimpleClass; L3 L4 1
    //   LOCALVARIABLE simpleObject LSimpleClass; L1 L4 0
    //   MAXSTACK = 2
    //   MAXLOCALS = 2
}

But why storing the result as a Long? throws an exception? Again, let's take a look at the differences in bytecode:

var number: Long? = null              |    var number: Long = 0
                                      |
      ACONST_NULL                     |        LCONST_0
      CHECKCAST java/lang/Long        |        LSTORE 0                
      ASTORE 0                        |

                number = simpleObject.getValue<Long>() [both]

      ALOAD 1                         |
              INVOKEVIRTUAL SimpleClass.getValue ()Ljava/lang/Object; [both]
      CHECKCAST java/lang/Long        |        CHECKCAST java/lang/Number
      ASTORE 0                        |        INVOKEVIRTUAL java/lang/Number.longValue ()J
                                      |        LSTORE 0

As you can see, the bytecode for number: Long casts the function result to a Number and then calls Number.longValue in order to convert the value to a Long (long in Java)

However, the bytecode for number: Long? casts the function result directly into the Long? (Long in Java) which leads to ClassCastException.

Not sure, if this behavior documented somewhere. However, the as operator performs unsafe cast and the compiler warns about it:

Warning:(21, 16) Kotlin: Unchecked cast: kotlin.Int to T

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)
  • maven中snapshot快照库和release发布库的区别和作用
  • arraylist中的搜索元素(Search element in arraylist)
  • 从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)
  • Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)
  • 如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • 从iframe访问父页面的id元素(accessing id element of parent page from iframe)
  • linux的常用命令干什么用的
  • Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)
  • 怎么删除禁用RHEL/CentOS 7上不需要的服务
  • 为什么Gradle运行测试两次?(Why does Gradle run tests twice?)
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在android中的活动之间切换?(Switching between activities in android?)
  • Perforce:如何从Depot到Workspace丢失文件?(Perforce: how to get missing file from Depot to Workspace?)
  • Webform页面避免运行服务器(Webform page avoiding runat server)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 内存布局破解(memory layout hack)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • 我们可以有一个调度程序,你可以异步添加东西,但会同步按顺序执行吗?(Can we have a dispatcher that you can add things todo asynchronously but will be executed in that order synchronously?)
  • “FROM a,b”和“FROM a FULL OUTER JOIN b”之间有什么区别?(What is the difference between “FROM a, b” and “FROM a FULL OUTER JOIN b”?)
  • Java中的不可变类(Immutable class in Java)
  • bat批处理文件结果导出到txt
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • 德州新起点计算机培训学校主要课程有什么?
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • “latin1_german1_ci”整理来自哪里?(Where is “latin1_german1_ci” collation coming from?)