如何添加方法以使用‘;在’;在python 3中使用我的类obj?
如何添加允许在中使用aa类的如何添加方法以使用‘;在’;在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” 您需
方法
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