Skip to content

Instantly share code, notes, and snippets.

@calthoff
Created December 3, 2019 02:08
Show Gist options
  • Save calthoff/104bb758e001df4b4a5eb643bc5d0a20 to your computer and use it in GitHub Desktop.
Save calthoff/104bb758e001df4b4a5eb643bc5d0a20 to your computer and use it in GitHub Desktop.
# Exercise 1 Solution
class Square:
square_list = []
def __init__(self):
self.square_list.append(self)
# Exercise 2 Solution
class Square:
def __init__(self, s1):
self.s1 = s1
def calculate_perimeter(self):
return self.s1 * 4
def __repr__(self):
return "{} by {} by {} by {}".format(self.s1, self.s1, self.s1, self.s1)
# Exercise 3 Solution
def same(obj1, obj2):
return obj1 is obj2
class Square:
def __init__(self):
pass
@aaronhalstonlee
Copy link

i don't feel like they are complaining, but even if so, it's warranted. these are the instructions for exercise 3:

Write a function that takes two objects as parameters and returns True if they are the same object, and False if not

it will not pass unless you name the function 'same'

@dersokc
Copy link

dersokc commented May 19, 2021

class Person():
    def __init__(self):
        pass

def check_if_same(obj1, obj2):
    if obj1 is obj2:
        return obj1 is obj2
    else:
        return obj1 is obj2
    
p1 = Person()
same_p = p1

p2 = Person()
another_p = p2


print(check_if_same(p1, same_p))
print(check_if_same(same_p, another_p))

Output:

True
False

The Udemy course will not evaluate this exercise, but hopefully this helps someone else understand the problem.

@MustafaKhurram2k4
Copy link

MustafaKhurram2k4 commented Jan 18, 2024

def are_same_object(obj1, obj2):
return obj1 is obj2

obj_a = [1, 2, 3]
obj_b = [1, 2, 3]
obj_c = obj_a

print(are_same_object(obj_a, obj_b)) # Output: False
print(are_same_object(obj_a, obj_c)) # Output: True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment