Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python-isinstance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# What Does isinstance() Do in Python?

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

The `.py` files contain the code found in the tutorial.

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.
3 changes: 3 additions & 0 deletions python-isinstance/abstract_class_failure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from balls_v2 import Ball

test_ball = Ball("white", "sphere")
14 changes: 14 additions & 0 deletions python-isinstance/ball_instance_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from balls import AmericanFootBall, Ball, PoolBall

eight_ball = PoolBall("black", 8)
football = AmericanFootBall("brown")
ball = Ball("green", "sphere")

isinstance(eight_ball, PoolBall)
isinstance(eight_ball, Ball)
isinstance(eight_ball, AmericanFootBall)

isinstance(eight_ball, object)
isinstance(football, object)
isinstance(ball, object)
isinstance(object, object)
15 changes: 15 additions & 0 deletions python-isinstance/balls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Ball:
def __init__(self, color, shape):
self.color = color
self.shape = shape


class PoolBall(Ball):
def __init__(self, color, number):
super().__init__(color, shape="sphere")
self.number = number


class AmericanFootBall(Ball):
def __init__(self, color):
super().__init__(color, shape="prolate spheroid")
30 changes: 30 additions & 0 deletions python-isinstance/balls_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from abc import ABC, abstractmethod


class Ball(ABC):
def __init__(self, color, shape):
self.color = color
self.shape = shape

@abstractmethod
def get_state(self):
pass


class PoolBall(Ball):
def __init__(self, color, number):
super().__init__(color, shape="sphere")
self.number = number

def get_state(self):
print(
f"Color = {self.color}, Number = {self.number}, Shape = {self.shape}"
)


class AmericanFootBall(Ball):
def __init__(self, color):
super().__init__(color, shape="prolate spheroid")

def get_state(self):
print(f"Color = {self.color}, Shape = {self.shape}")
6 changes: 6 additions & 0 deletions python-isinstance/basic_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
shape = "Sphere"
number = 8

isinstance(shape, str)
isinstance(number, int)
isinstance(number, float)
18 changes: 18 additions & 0 deletions python-isinstance/bool_int_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
isinstance(True, int)

isinstance(True, bool)

isinstance(False, int)

isinstance(False, bool)

test_data = [10, True, False]

for element in test_data:
print("int") if isinstance(element, int) else print("bool")

for element in test_data:
print("bool") if isinstance(element, bool) else print("int")

for element in test_data:
print("bool") if type(element) is bool else print("int") # noqa
7 changes: 7 additions & 0 deletions python-isinstance/calculate_area_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def calculate_area(length, breadth):
return length * breadth


calculate_area(5, 3)
calculate_area(5, "3")
calculate_area("5", "3")
9 changes: 9 additions & 0 deletions python-isinstance/calculate_area_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def calculate_area(length, breadth):
if isinstance(length, int) and isinstance(breadth, int):
return length * breadth
raise TypeError("Both arguments must be integers")


calculate_area(5, 3)
calculate_area(5, "3")
calculate_area("5", "3")
20 changes: 20 additions & 0 deletions python-isinstance/consolidation1_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from balls import AmericanFootBall, Ball, PoolBall

ball = Ball("green", "sphere")
football = AmericanFootBall("brown")

isinstance(football, AmericanFootBall)

isinstance(football, PoolBall)

isinstance(ball, Ball)

isinstance(ball, AmericanFootBall)

isinstance(football, Ball)

isinstance(ball, PoolBall)

isinstance(1, bool)

isinstance(0, bool)
23 changes: 23 additions & 0 deletions python-isinstance/consolidation2_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from abc import ABC
from collections.abc import Callable

from balls_v2 import AmericanFootBall, PoolBall

eight_ball = PoolBall("black", 8)
football = AmericanFootBall("brown")

# a)

isinstance(eight_ball, ABC)
isinstance(football, ABC)

# b)

isinstance(isinstance, Callable)
isinstance(PoolBall, Callable)

# c)

isinstance(eight_ball.get_state, Callable)

isinstance(PoolBall, Callable)
11 changes: 11 additions & 0 deletions python-isinstance/isinstance_vs_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from balls import Ball, PoolBall

eight_ball = PoolBall("black", 8)

type(eight_ball)

type(eight_ball) is PoolBall

isinstance(eight_ball, Ball)

type(eight_ball) is Ball
9 changes: 9 additions & 0 deletions python-isinstance/isinstance_with_abc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from balls_v2 import AmericanFootBall, Ball, PoolBall

eight_ball = PoolBall("black", 8)

football = AmericanFootBall("brown")

isinstance(eight_ball, Ball)

isinstance(football, Ball)
14 changes: 14 additions & 0 deletions python-isinstance/iter_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from player_iterables import (
PlayersVersionFour,
PlayersVersionOne,
PlayersVersionThree,
PlayersVersionTwo,
)

iter(PlayersVersionOne(["Fast Ed", "Slow Jo", "Still Su"]))

iter(PlayersVersionTwo(["Fast Ed", "Slow Jo", "Still Su"]))

iter(PlayersVersionThree(["Fast Ed", "Slow Jo", "Still Su"]))

iter(PlayersVersionFour(["Fast Ed", "Slow Jo", "Still Su"]))
14 changes: 14 additions & 0 deletions python-isinstance/mro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Top:
pass


class Middle(Top):
pass


class Bottom(Middle):
pass


Bottom.mro()
isinstance(Bottom(), Top)
11 changes: 11 additions & 0 deletions python-isinstance/multiple_type_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
print("Number" if isinstance(3.14, (int, float)) else "Not a number")

print("Number" if isinstance("3.14", (int, float)) else "Not a number")

print("Number" if isinstance(3.14, ((int,), (float,))) else "Not a number")

print("Number" if isinstance(3.14, (int, float)) else "Not a number")

print("Number" if isinstance(3.14, int | float) else "Not a number")

print("Number" if isinstance("3.14", int | float) else "Not a number")
38 changes: 38 additions & 0 deletions python-isinstance/player_iterables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from collections.abc import Iterable


class PlayersVersionOne(Iterable):
def __init__(self, players):
self.players = players

def __iter__(self):
return iter(self.players)


class PlayersVersionTwo:

def __init__(self, players):
self.players = players

def __iter__(self):
return iter(self.players)


class PlayersVersionThree:

def __init__(self, players):
self.players = players

def __getitem__(self, index):
if index >= len(self.players):
raise IndexError
return self.players[index]


class PlayersVersionFour:

def __init__(self, players):
self.players = players

def __iter__(self):
pass
29 changes: 29 additions & 0 deletions python-isinstance/players_iterable_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from collections.abc import Iterable

from player_iterables import (
PlayersVersionFour,
PlayersVersionOne,
PlayersVersionThree,
PlayersVersionTwo,
)

for player in PlayersVersionOne(["Fast Ed", "Slow Jo", "Still Su"]):
print(player)

isinstance(PlayersVersionOne(["Fast Ed", "Slow Jo", "Still Su"]), Iterable)

for player in PlayersVersionTwo(["Fast Ed", "Slow Jo", "Still Su"]):
print(player)

isinstance(PlayersVersionTwo(["Fast Ed", "Slow Jo", "Still Su"]), Iterable)

for player in PlayersVersionThree(["Fast Ed", "Slow Jo", "Still Su"]):
print(player)

isinstance(PlayersVersionThree(["Fast Ed", "Slow Jo", "Still Su"]), Iterable)

# This will fail.
for player in PlayersVersionFour(["Fast Ed", "Slow Jo", "Still Su"]):
print(player)

isinstance(PlayersVersionFour(["Fast Ed", "Slow Jo", "Still Su"]), Iterable)
12 changes: 12 additions & 0 deletions python-isinstance/type_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from balls import Ball, PoolBall

eight_ball = PoolBall("black", 8)

type(eight_ball)

type(eight_ball) is PoolBall


isinstance(eight_ball, Ball)

type(eight_ball) is Ball