Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何添加方法以使用‘;在’;在python 3中使用我的类obj?_Python - Fatal编程技术网

如何添加方法以使用‘;在’;在python 3中使用我的类obj?

如何添加方法以使用‘;在’;在python 3中使用我的类obj?,python,Python,如何添加允许在中使用aa类的方法 class aa: def __init__(self,x): self.x=x def __str__(self): return str(self.x) def __add__(self,other): x=self.x+other return aa(x) a=aa(2) print(2 in a) # error: “...arg not iterable” 您需

如何添加允许在中使用aa类的
方法

class aa:
    def __init__(self,x):
        self.x=x
    def __str__(self):
        return str(self.x)
    def __add__(self,other):
        x=self.x+other
        return aa(x)

a=aa(2)
print(2 in a) # error: “...arg not iterable”

您需要为您的类实现
\uuuuuu contains\uuuuu

看更多


您需要为您的类实现
\uuuuuu contains\uuuuu

看更多


@jpp,关于迭代器的一个不是重复的,因为它不是OP所要求的(你不能在
中使用迭代器使用
)@jpp,关于迭代器的一个不是重复的,因为它不是OP所要求的(你不能在
中使用迭代器使用
),尽管它不是必需的,最好将打算用作容器的类作为
collections.abc.container
的子类。您的读者和IDE将更好地理解您的意图。此外,如果您忘记定义
\uuuuuu包含\uuuuu
或不小心,Python将给您一个错误。拼写错误。虽然它不是必需的,但对于打算用作容器的类来说,最好将其作为
collections.abc.container
的子类。您的读者和IDE将更好地理解您的意图。此外,如果您忘记定义
\uuuuuu包含\uuuuu
或不小心,Python将给您一个错误。拼错了。
class aa:
    def __init__(self,x):
        self.x=x
    def __str__(self):
        return str(self.x)
    def __add__(self,other):
        x=self.x+other
        return aa(x)
    def __contains__(self,x):
        # TODO implement  
        pass