Skip to content

Commit 863607f

Browse files
authored
Merge pull request #681 from realpython/python-isinstance-fix-commit
python-isinstance (Fix)
2 parents 0b936c8 + 0725d56 commit 863607f

19 files changed

+290
-0
lines changed

python-isinstance/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# What Does isinstance() Do in Python?
2+
3+
This folder provides the code examples for the Real Python tutorial [What Does isinstance() Do in Python?](https://realpython.com/what-does-isinstance-do-in-python/).
4+
5+
The `.py` files contain the code found in the tutorial.
6+
7+
When setting up your tutorial environment, make sure `balls.py`, `balls_v2.py`, and `player_iterables.py` are in your program folder. You'll need to import content from these to replicate the tutorial examples.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from balls_v2 import Ball
2+
3+
test_ball = Ball("white", "sphere")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from balls import AmericanFootBall, Ball, PoolBall
2+
3+
eight_ball = PoolBall("black", 8)
4+
football = AmericanFootBall("brown")
5+
ball = Ball("green", "sphere")
6+
7+
isinstance(eight_ball, PoolBall)
8+
isinstance(eight_ball, Ball)
9+
isinstance(eight_ball, AmericanFootBall)
10+
11+
isinstance(eight_ball, object)
12+
isinstance(football, object)
13+
isinstance(ball, object)
14+
isinstance(object, object)

python-isinstance/balls.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Ball:
2+
def __init__(self, color, shape):
3+
self.color = color
4+
self.shape = shape
5+
6+
7+
class PoolBall(Ball):
8+
def __init__(self, color, number):
9+
super().__init__(color, shape="sphere")
10+
self.number = number
11+
12+
13+
class AmericanFootBall(Ball):
14+
def __init__(self, color):
15+
super().__init__(color, shape="prolate spheroid")

python-isinstance/balls_v2.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Ball(ABC):
5+
def __init__(self, color, shape):
6+
self.color = color
7+
self.shape = shape
8+
9+
@abstractmethod
10+
def get_state(self):
11+
pass
12+
13+
14+
class PoolBall(Ball):
15+
def __init__(self, color, number):
16+
super().__init__(color, shape="sphere")
17+
self.number = number
18+
19+
def get_state(self):
20+
print(
21+
f"Color = {self.color}, Number = {self.number}, Shape = {self.shape}"
22+
)
23+
24+
25+
class AmericanFootBall(Ball):
26+
def __init__(self, color):
27+
super().__init__(color, shape="prolate spheroid")
28+
29+
def get_state(self):
30+
print(f"Color = {self.color}, Shape = {self.shape}")

python-isinstance/basic_types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
shape = "Sphere"
2+
number = 8
3+
4+
isinstance(shape, str)
5+
isinstance(number, int)
6+
isinstance(number, float)

python-isinstance/bool_int_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
isinstance(True, int)
2+
3+
isinstance(True, bool)
4+
5+
isinstance(False, int)
6+
7+
isinstance(False, bool)
8+
9+
test_data = [10, True, False]
10+
11+
for element in test_data:
12+
print("int") if isinstance(element, int) else print("bool")
13+
14+
for element in test_data:
15+
print("bool") if isinstance(element, bool) else print("int")
16+
17+
for element in test_data:
18+
print("bool") if type(element) is bool else print("int") # noqa
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def calculate_area(length, breadth):
2+
return length * breadth
3+
4+
5+
calculate_area(5, 3)
6+
calculate_area(5, "3")
7+
calculate_area("5", "3")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def calculate_area(length, breadth):
2+
if isinstance(length, int) and isinstance(breadth, int):
3+
return length * breadth
4+
raise TypeError("Both arguments must be integers")
5+
6+
7+
calculate_area(5, 3)
8+
calculate_area(5, "3")
9+
calculate_area("5", "3")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from balls import AmericanFootBall, Ball, PoolBall
2+
3+
ball = Ball("green", "sphere")
4+
football = AmericanFootBall("brown")
5+
6+
isinstance(football, AmericanFootBall)
7+
8+
isinstance(football, PoolBall)
9+
10+
isinstance(ball, Ball)
11+
12+
isinstance(ball, AmericanFootBall)
13+
14+
isinstance(football, Ball)
15+
16+
isinstance(ball, PoolBall)
17+
18+
isinstance(1, bool)
19+
20+
isinstance(0, bool)

0 commit comments

Comments
 (0)