简体   繁体   English

Python:如何区分同一类的两个不同对象的两个变量?

[英]Python: how can I differentiate between two variables of two different objects of the same class?

I have a problem with a task for school. 我上学的任务有问题。 I want my last method to test if two rectangles are the same. 我想要我的最后一个方法来测试两个矩形是否相同。 The only problem there is that I can't seem to differentiate between the two different heights, widiths and the different point(this is the left-low corner point of the rectangle) of the two different rectangles, any advice? 唯一的问题是,我似乎无法区分两个不同的高度,宽度和两个不同的矩形的不同点(这是矩形的左下角点),有什么建议吗?

Thanks a lot 非常感谢

class Point():
    def __init__(self,x,y):
        self.x=x
        self.y=y

class Rectangle():
    def __init__(self,Point,w,h):
        self.Point=Point
        self.widith=w**strong text**
        self.height=h

    def same(self,Rectangle):
        if Rectangle.self.Point==self.Point and Rectangle.self.widith==self.widith and Rectangle.self.height==self.height:
            return True
        else:
            return False

First of all don't use the same name for function params and classes. 首先,对于函数参数和类不要使用相同的名称。 It makes the code confusing and error prone. 它使代码混乱并且容易出错。 Try this: 尝试这个:

class Rectangle:
    def __init__(self, point, width, height):
        self.point = point
        self.widith = width
        self.height = height

Now I assume that point variable is an instance of Point class. 现在,我假设point变量是Point类的实例。 In that case comparing one point to another via == will fail, because by default == checks if two objects are the same in the sense of being the same object in memory. 在那种情况下,通过==将一个点与另一个点进行比较将失败,因为默认情况下==检查两个对象是否在内存中是相同的对象。

Thus your implemenatation of same method may look like this: 因此,您对same方法的实现可能如下所示:

def same(self, other):
    return (
        self.point.x == other.point.x
        and self.point.y == other.point.y
        and self.width == other.width
        and self.height == other.height
    )

If you overwrite builtin __eq__ method (which is responsible for the behaviour of == operator) on Point class like this: 如果您在Point类上覆盖内置的__eq__方法(负责==运算符的行为),如下所示:

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

then same method can be simplified to: 然后可以将same方法简化为:

def same(self, other):
    return (
        self.point == other.point  # now __eq__ will be called here
        and self.width == other.width
        and self.height == other.height
    )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 python 中,相同 class 的两个不同对象可以有不同的变量数 - In python can two different objects of same class have different no of variables 是否可以区分 Python 中具有相同变量的两个对象? - Is it possible to differentiate between two objects that have the same variable in Python? 如何在python 2.7中区分两种不同类型的文件? - How to differentiate between two different type of files in python 2.7? Python - 如何区分指向同一对象的两个列表元素? - Python - How do I differentiate between two list elements that point to the same object? 如何将抓取的数据分配给具有相同类的两个不同变量? - How can I assign scraped data two different variables with same class? 如何区分两个不同文件夹中具有不同扩展名的同名文件? Python - How to differentiate files with same name with different extension from two different folders? Python 如何防止在Python中从两个不同的变量生成两个相同的随机数? - How do I prevent two of the same random numbers being generated from two different variables in Python? 如何同时在 Python 中运行两个不同的代码? - How can I run two different code in Python at the same time? 如何在 Python 中为两个或多个变量分配相同的类值,使它们不在同一个 ID 下? - How do I assign the same class value to two or more variables in Python, so they are not under the same ID? Pymongo或Mongodb将两个相同的python词典视为不同的对象。我可以强迫他们一起对待吗? - Pymongo or Mongodb is treating two equal python dictionaries as different objects. Can I force them to be treated the same?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM