Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 如何在类内执行值检查_Python_Python 3.x_Class - Fatal编程技术网

Python 如何在类内执行值检查

Python 如何在类内执行值检查,python,python-3.x,class,Python,Python 3.x,Class,因此,我们的任务是创建一个通用的向量类来执行add方法,不管x,y值是什么(str还是int)。 下面是我试图执行的代码,只是为了检查try是否在类中工作 class Vector(): def __init__(self,x,y): self.x = x self.y = y def __valuecheck__(self): try: self.x + "a" except TypeErr

因此,我们的任务是创建一个通用的向量类来执行add方法,不管x,y值是什么(str还是int)。 下面是我试图执行的代码,只是为了检查try是否在类中工作

class Vector():
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def __valuecheck__(self):
        try:
            self.x + "a"
        except TypeError:
            return str(self.x)
    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)
    def __repr__(self):
        return "Vector({},{})".format(self.x,self.y)

a = Vector(1,"a")
b = Vector("a",2)
c = a.__add__(b)
print(c)
预期产量为

Vector(1a,a2)
我尝试了不同的变体,定义了经典函数,例如def valuecheck(),还尝试了添加try,除了添加到addinit方法之外,但似乎都不起作用。需要你们的帮助,任何提示都非常感谢!
干杯

我想我已经找到了答案

class Vector():
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def __valuecheck__(self):
        try:
            self.x + "a"
        except TypeError:
            return str(self.x)
    def __repr__(self):
        return "Vector({},{})".format(self.x,self.y)
    def __add__(self, other):
        mvbh = str(self.x), str(self.y) # My Vector Before Hand
        myVector = ''.join(mvbh)

        ovbh = str(other.x), str(other.y) # Other Vector Before Hand
        otherVector = ''.join(ovbh)

        final = "Vector({}, {})".format(myVector, otherVector) # Change this to create a new vector

        print(final)


a = Vector(1,"a")
b = Vector("a",2)
a.__add__(b)

它输出什么一个类的
\uuuuuuuuuuuuuuuuuuuuuuuuuuuu
方法告诉python如何将该类的两个实例添加到一起(即
向量(1,“a”)+向量(“a”,2)
)。你应该研究[重载一个类的加法运算符](重载加法、减法和乘法运算符)。@Dontbe3预期的输出是向量(1a,a2)@b_c thx作为答案,但我无法想象它是如何解决这个问题的?这个问题已经解决了。我认为在我们开始长期帮助之前,我们实际上需要知道你们在这里想要完成什么。你的问题涉及多个方面,没有定义一个明确的问题。您的问题是关于
错误处理
、关于
类结构
还是关于
变量类型
?或者问题只是“为什么我的代码不能工作”?谢谢你的回答!我刚刚对它进行了测试,并将代码更改为a=Vector(1,2)b=Vector(“a”,“b”)给出了输出向量(12,ab),这是您一直在寻找的,还是您正在寻找预防措施?如果是这样的话,只需做一些If语句,感谢您的回答,但这是不对的,因为我根本不执行值检查,如果我有两个数字,它们最终会是strings@ChrisPython那么,你的问题很含糊。重新编辑你的问题。你想说什么do@ChrisPython这到底是什么意思?如果您不检查值,并且总是希望它将新值附加到
xy
对的末尾,那么为什么不在
\uuuuu init\uuuu
方法中创建字符串呢?@hampusrason是的,他的问题非常令人困惑
class Vector():
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def __valuecheck__(self):
        try:
            self.x + "a"
        except TypeError:
            return str(self.x)
    def __add__(self, other):
        return Vector(str(self.x) + str(other.x), str(self.y) + str(other.y))
    def __repr__(self):
        return "Vector({},{})".format(self.x,self.y)

a = Vector(1,"a")
b = Vector("a",2)
c = a.__add__(b)
print(c)