JavaScript
x
12
12
1
class Point:
2
def __init__(self, x_or_obj = 0, y = 0):
3
if isinstance(x_or_obj, Point):
4
self.x = x_or_obj.x
5
self.y = x_or_obj.y
6
else:
7
self.x = x_or_obj
8
self.y = y
9
10
m = Point(1,2)
11
k = Point(m)
12
So I have difficulties with understanding why isinstance
evaluating True
in this code. I see it as int
is checking against class
, which makes no sense to me.
Advertisement
Answer
Looking at this article about isinstance
:
The isinstance() function returns True if the specified object is of the specified type, otherwise False.
In m
‘s definition:
JavaScript
1
2
1
m = Point(1,2)
2
You’re passing 1
as the value of x_or_obj
. 1
is an integer, not a Point
, therefore it evaluates to False
.
However, in k
‘s definition:
JavaScript
1
2
1
k = Point(m)
2
you’re passing m
as the value of x_or_obj
. You earlier defined m
as type Point
, so isinstance
evaluates to True.