Skip to content

Commit a6c1e21

Browse files
committed
TR1 Initial commit
1 parent a7f6e16 commit a6c1e21

20 files changed

+267
-0
lines changed

python-isinstance/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# How Does `isinstance()` Work in Python?
2+
3+
This folder provides the code examples for the Real Python tutorial [How Does `isinstance()` Work in Python?](https://realpython.com/how-does-python-isinstance-work/).
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 `customiterables.py` are in your program folder. You'll need to import content from these during the tutorial.
8+
9+
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
eight_ball = PoolBall("black", 8)
2+
football = FootBall("brown")
3+
ball = Ball("green", "sphere")
4+
5+
isinstance(eight_ball, PoolBall)
6+
isinstance(eight_ball, Ball)
7+
isinstance(eight_ball, FootBall)
8+
9+
isinstance(eight_ball, object)
10+
isinstance(football, object)
11+
isinstance(ball, object)
12+
isinstance(object, object)

python-isinstance/balls.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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_shape(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_shape(self):
20+
return self.shape
21+
22+
23+
class AmericanFootBall(Ball):
24+
def __init__(self, color):
25+
super().__init__(color, shape="elongated ellipsoid")
26+
27+
def get_shape(self):
28+
return "Elongated Ellipsoid"

python-isinstance/balls_v2.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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_shape(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_shape(self):
20+
return self.shape
21+
22+
23+
class AmericanFootBall(Ball):
24+
def __init__(self, color):
25+
super().__init__(color, shape="elongated ellipsoid")
26+
27+
def get_shape(self):
28+
return "Elongated Ellipsoid"

python-isinstance/basic_iterable.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
squares = [
2+
1,
3+
4,
4+
9,
5+
]
6+
7+
for _ in squares:
8+
print(_)

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+
"int" if isinstance(element, int) else "bool"
13+
14+
for element in test_data:
15+
"bool" if isinstance(element, bool) else "int"
16+
17+
for element in test_data:
18+
"bool" if type(element) is bool else "int"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
isinstance(football, FootBall)
2+
3+
isinstance(football, PoolBall)
4+
5+
isinstance(ball, Ball)
6+
7+
isinstance(ball, FootBall)
8+
9+
isinstance(football, Ball)
10+
11+
isinstance(ball, PoolBall)
12+
13+
isinstance(1, bool)
14+
15+
isinstance(0, bool)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from abc import ABC
2+
3+
isinstance(eight_ball, ABC)
4+
5+
isinstance(football, ABC)
6+
7+
from collections.abc import Callable
8+
9+
isinstance(isinstance, Callable)
10+
11+
from balls import PoolBall
12+
13+
eight_ball = PoolBall("black", 8)
14+
15+
isinstance(eight_ball.get_shape, Callable)
16+
17+
isinstance(PoolBall, Callable)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from customiterables import CustomIterableOne
2+
3+
for number in CustomIterableOne(4):
4+
print(number)
5+
6+
from collections.abc import Iterable
7+
8+
isinstance(CustomIterableOne(4), Iterable)

0 commit comments

Comments
 (0)