3

Attempting to compare two objects' data members; however, the error message has no specific details, which leaves me with little information on how to go about correcting it

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
  • Command: python person.py
  • Error message
  • SyntaxError
  • If it were an indentation level the following error is displayed
  • IndentationError
1
  • 1
    Among other things, you are probably mixing tabs and spaces in your indentation somewhere. Commented Sep 1, 2018 at 19:40

5 Answers 5

3

same_person is a method of the class Person and should take just an argument as input. It should be defined as:

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

and called as

person1.same_person(person2)

or you could override the __eq__ method (i.e., ==).

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

in order to be able to do it as person1 == person2

Sign up to request clarification or add additional context in comments.

4 Comments

return self is other is a bit more elegant.
@DYZ: but then Person(1, 2, 3) != Person(1, 2, 3), since they're not the same object.
If you suggest to add self.id is other.id I don't really understand why, we are checking equality not object identity.
I suggest to replace self.id == other.id with self is other (both expressions are fully equivalent).
1

The other answers are correct and provide the best way to do it, but I realized that you wrote:

print calls provided as part of an exercise: not my implementation

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

The exercise probably wants you to define a function outside the class. You can do that by removing that function from the class and writing it un-indented outside the class (without providing class type too). For example:

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))

3 Comments

Much appreciated
Glad to help :-)
@aguilar keep in mind that the best way to compare two objects would be overriding __eq__() method of the class, have a look at this
1
class Person: 
   def __init__(self, name, age, id):
      self.name = name
      self.age = age
      self.id = id

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

you dont have to define lhs and rhs type in python unless you are using typings.

Comments

1

Quite a few mistakes:

  1. The arguments in the method cannot be preceded by the Person classname
  2. You have not defined instances person1, person2 and person3
  3. If you define an instance method (same_person), it should be used ON an instance.

This is what I would do:

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))

Output:

True
False

Comments

0

You'd better rewrite eq to compare objects:

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.