- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在实现一个简单的类来表示二维向量。以下是相关部分:
class Vector:
def __init__( self, x, y ):
self.vec_repr = x, y
def __add__( self, other ):
new_x = self.x + other.x
new_y = self.y + other.y
return Vector( new_x, new_y )
def __getattr__( self, name ):
if name == "x":
return self.vec_repr[0]
elif name == "y":
return self.vec_repr[1]
稍后,我有类似的东西:
a = Vector( 1, 1 )
b = Vector( 2, 2 )
a + b
我收到 TypeError: 'NoneType' object is not callable
。这特别奇怪,因为错误没有被标记为在任何特定的行上,所以我不知道去哪里找!
很奇怪,于是做了一些实验,发现它出现在a+b
这一行。另外,当我按如下方式重新上课时:
class Vector:
def __init__( self, x, y ):
self.x, self.y = x, y
def __add__( self, other ):
new_x = self.x + other.x
new_y = self.y + other.y
return Vector( new_x, new_y )
错误消失了!
我看到有很多关于与此类似的错误的问题 - 所有问题似乎都涉及某个函数名称在某处被变量覆盖,但我看不出这是在哪里发生的!
作为另一个线索,当我将 __getattr__()
的默认返回类型更改为其他类型时 - 例如 str - 错误会变成 TypeError: 'str' object is not callable
关于发生了什么的任何想法? __getattr__()
有什么我不明白的行为吗?
最佳答案
问题是您的 __getattr__
不会为 x
和 y
以外的属性返回任何内容,也不会引发 AttributeError。因此,当查找 __add__
方法时,__getattr__
返回 None
并因此返回错误。
您可以通过使 __getattr__
返回其他属性的值来解决此问题。事实上,您必须确保 __getattr__
从其父类(super class)中为所有未处理的属性调用该方法。但实际上 __getattr__
在这里使用是错误的。它应该谨慎使用,并且在没有更明显、更高级别的解决方案可用时使用。例如,__getattr__
对于动态调度是必不可少的。但在您的情况下,x
和 y
值在代码运行之前是众所周知且定义明确的。
正确的解决方案是创建 x
和 y
属性,并且根本不实现 __getattr__
。
@property
def x(self):
return self.vec_repr[0]
@property
def y(self):
return self.vec_repr[1]
关于Python 奇怪错误 : "TypeError: ' NoneType' object is not callable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6891477/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我想知道如果我们有一个引用 const 的函数参数会发生什么功能如下图。 版本 1 int anotherFunc() { std::cout<<"inside anotherFunc"<
我正在尝试运行YOLOv8重新训练的对象检测测试,但我一直收到此错误。。我已经试过安装gnu-cobol和更新西芹了,还有其他想法吗?谢谢
我做了一个静态类方法(这里称为“函数”)f需要一个列表 Callable小号: import java.util.concurrent.*; import java.util.*; class A {
以下是我的类定义: class logline: def __init__(self,t,cmp,msg): self.t = t self.cmp = cmp
我正在用 dart 做一个小实验,我找不到一种方法来确定一个变量是否是“可调用的”,而无需显式检查每种类型(字符串、整数、 bool 等),并猜测它是否是可调用的,如果没有的话那些。我还尝试了一个 t
我大量使用来自 python 3 的 python 类型支持。 最近我试图将一个函数作为参数传递,但我没有找到使用 kwargs 的任何帮助。在 typing.Callable签名。 请检查下面的代码
每当我运行实现可调用的程序时,我都会以顺序形式获得输出。 就像,这是我的程序: package com.handson; import java.util.concurrent.ArrayBlocki
我编写了一个注释(在Spring Boot应用程序中)并尝试将其应用到Callable的call()方法上,但它不起作用,但另一方面,当应用于普通方法时(请参阅下面的代码),它有效,这一直困扰着我,你
我试图将对象列表拆分为较小的子列表,并在不同的线程上单独处理它们。所以我有以下代码: List instances = xmlInstance.readInstancesFromXml
我想我可以用 std.traits.functionAttributes 来做到这一点,但它不支持 static。对于任何类型的可调用对象(包含 opCall 的结构),我如何判断该可调用对象是否使用
我是新来的,如果我把这个放到错误的主题中,很抱歉? 我的问题: class TaskEol implements Callable> { ArrayList coordinates =
我有一些代码可以从目录中的多个 JAR 动态加载类。因为 JAR 中的代码是不受信任的,所以我希望通过对它们进行“沙盒处理”来保护主应用程序免受编码不良或恶意动态类的影响。我想做的一件事是“时间盒”不
这个问题在这里已经有了答案: Overriding special methods on an instance (5 个答案) 关闭 9 年前。 我有以下代码 import numpy as np
我正在尝试制作一个类似于 https://github.com/ElgarL/TownyChat/blob/master/src/com/palmergames/bukkit/TownyChat/To
有人可以解释为什么要继承未参数化和参数化的Callable: from typing import Callable from typing import NoReturn from typing i
我有一个任务要以固定速率运行。但是,我还需要每次执行后的任务结果。这是我尝试过的: 任务 class ScheduledWork implements Callable { public St
给定一个实例 x的 Callable , 我怎样才能运行 x在一个单独的进程中,以便我可以重定向进程的标准输入和输出?例如,有没有办法构建一个 Process来自Callable ?有没有标准Exec
我有以下可调用对象: public class Worker implements Callable{ @Override public Boolean call(){ boo
现在很清楚了what a metaclass is ,有一个相关的概念,我一直在使用,但不知道它的真正含义。 我想每个人都犯了一次括号错误,导致“对象不可调用”异常。更何况,使用 __init__ 和
我是一名优秀的程序员,十分优秀!