如何比较对象Python的两个实例?

时间:2018-09-01 19:38:24

标签: python equality

试图比较两个对象的数据成员;但是,该错误消息没有特定的详细信息,这使我对如何更正该错误的信息不多

class Person: 
  def __init__(self, name, age, id):
    self.name = name
    self.age = age
    self.id = id

  def same_person(Person lhs, Person rhs):
    return lhs.id == rhs.id

person1 = Person("David Joyner", 30, 901234567)
person2 = Person("D. Joyner", 29, 901234567)
person3 = Person("David Joyner", 30, 903987654)
# print calls provided as part of an exercise: not my implementation
print(same_person(person1, person2))
print(same_person(person1, person3))
  • Python 3.6.5
  • 命令:python person.py
  • 错误消息
  • SyntaxError
  • 如果是缩进级别,则会显示以下错误
  • IndentationError

5 个答案:

答案 0 :(得分:3)

same_person是from django.core.exceptions import ObjectDoesNotExist class Homepage(TemplateView): template_name = 'home.html' def get_context_data(self, **kwargs): context = super(Homepage, self).get_context_data(**kwargs) context['event_list'] = Event.objects.all() if self.request.user.is_authenticated() try: my_location = self.request.user.userlocation except ObjectDoesNotExist: # ... hande case where the location does not exists else: print("The location is {}".format(my_location)) return context类的方法,应仅将一个参数作为输入。它应该定义为:

Person

并称为

def same_person(self, other):
    return self.id == other.id

或者您可以覆盖person1.same_person(person2) 方法(即__eq__)。

==

以便能够以def __eq__(self, other): return self.id == other.id

的身份进行操作

答案 1 :(得分:1)

Vector(Students(1,Math,40),Students(1,English,60), Students(1,Science,55), Students(2,Math,80),Students(2,English,60), Students(2,Science,55),Students(3,Math,40),Students(3,English,60), Students(3,Science,30))

除非使用类型输入,否则不必在python中定义lhs和rhs类型。

答案 2 :(得分:1)

很多错误:

  1. 方法中的参数不能以Person类名开头
  2. 您尚未定义实例person1person2person3
  3. 如果定义实例方法(same_person),则应在实例上使用它。

这就是我要做的:

class Person:
    def __init__(self, name, age, id):
        self.name = name
        self.age = age
        self.id = id

    def same_person(self, other):
        return self.id == other.id

person1 = Person("Bob", 25, 1)
person2 = Person("Mike", 33, 1)
person3 = Person("Maria", 28, 2)

print(person1.same_person(person2))
print(person1.same_person(person3))

输出:

True
False

答案 3 :(得分:1)

其他答案是正确的,并提供了最佳方法,但我意识到您写过:

  

打印练习中提供的电话:不是我的实现

print(same_person(person1, person2))
print(same_person(person1, person3))

该练习可能希望您在类之外定义一个函数。您可以通过从类中删除该函数并在类外以不缩进的方式编写该函数来实现(也无需提供类类型)。例如:

class Person: 
    def __init__(self, name, age, id):
        self.name = name
        self.age = age
        self.id = id

def same_person(lhs, rhs):
    return lhs.id == rhs.id

person1 = Person("David Joyner", 30, 901234567)
person2 = Person("D. Joyner", 29, 901234567)
person3 = Person("David Joyner", 30, 903987654)

print(same_person(person1, person2))
print(same_person(person1, person3))

答案 4 :(得分:0)

您最好重写 eq 以比较对象:

class Person: 
    def __init__(self, name, age, id):
        self.name = name
        self.age = age
        self.id = id
    def __eq__(self, other):
        return self.id == other.id
person1 = Person("Bob", 25, 1)
person2 = Person("Mike", 33, 1)
person3 = Person("Maria", 28, 2)

print(person1 == person2)
print(person1 == person3)
>>> True
>>> False

https://devinpractice.com/2016/11/29/python-objects-comparison/