Skip to content

TR1 Initial commit #667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions python-isinstance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# How Does `isinstance()` Work in Python?

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

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

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.


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 Ball, FootBall, PoolBall

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

isinstance(eight_ball, PoolBall)
isinstance(eight_ball, Ball)
isinstance(eight_ball, FootBall)

isinstance(eight_ball, object)
isinstance(football, object)
isinstance(ball, object)
isinstance(object, object)
28 changes: 28 additions & 0 deletions python-isinstance/balls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from abc import ABC, abstractmethod


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

@abstractmethod
def get_shape(self):
pass


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

def get_shape(self):
return self.shape


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

def get_shape(self):
return "Elongated Ellipsoid"
28 changes: 28 additions & 0 deletions python-isinstance/balls_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from abc import ABC, abstractmethod


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

@abstractmethod
def get_shape(self):
pass


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

def get_shape(self):
return self.shape


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

def get_shape(self):
return "Elongated Ellipsoid"
8 changes: 8 additions & 0 deletions python-isinstance/basic_iterable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
squares = [
1,
4,
9,
]

for _ in squares:
print(_)
19 changes: 19 additions & 0 deletions python-isinstance/bool_int_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
isinstance(True, int)

isinstance(True, bool)

isinstance(False, int)

isinstance(False, bool)

test_data = [10, True, False]

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

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

# Try and avoid this.
# for element in test_data:
# "bool" if type(element) is bool else "int"
21 changes: 21 additions & 0 deletions python-isinstance/consolidation1_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from balls import Ball, FootBall, PoolBall

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


isinstance(football, FootBall)

isinstance(football, PoolBall)

isinstance(ball, Ball)

isinstance(ball, FootBall)

isinstance(football, Ball)

isinstance(ball, PoolBall)

isinstance(1, bool)

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

from balls_v2 import AmericanFootBall, PoolBall

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

isinstance(eight_ball, ABC)

isinstance(football, ABC)

isinstance(isinstance, Callable)

isinstance(eight_ball.get_shape, Callable)

isinstance(PoolBall, Callable)
9 changes: 9 additions & 0 deletions python-isinstance/customiterable_one_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from collections.abc import Iterable

from customiterables import CustomIterableOne

for number in CustomIterableOne(4):
print(number)


isinstance(CustomIterableOne(4), Iterable)
9 changes: 9 additions & 0 deletions python-isinstance/customiterable_three_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from collections.abc import Iterable

from customiterables import CustomIterableThree

for number in CustomIterableThree(4):
print(number)


isinstance(CustomIterableThree(4), Iterable)
8 changes: 8 additions & 0 deletions python-isinstance/customiterable_two_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from collections.abc import Iterable

from customiterables import CustomIterableTwo

for number in CustomIterableTwo(4):
print(number)

isinstance(CustomIterableTwo(4), Iterable)
48 changes: 48 additions & 0 deletions python-isinstance/customiterables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from collections.abc import Iterable


class CustomIterableOne(Iterable):

def __init__(self, last_value):
self.last_value = last_value
self.current = 1

def __iter__(self):
return self

def __next__(self):
if self.current <= self.last_value:
value = self.current
self.current += 1
return value
else:
raise StopIteration()


class CustomIterableTwo:

def __init__(self, last_value):
self.last_value = last_value
self.current = 1

def __iter__(self):
return self

def __next__(self):
if self.current <= self.last_value:
value = self.current
self.current += 1
return value
else:
raise StopIteration()


class CustomIterableThree:

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

def __getitem__(self, index):
if index >= self.last_value:
raise IndexError
return index + 1
7 changes: 7 additions & 0 deletions python-isinstance/invalid_types.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/isinstance_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)
11 changes: 11 additions & 0 deletions python-isinstance/iter_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from customiterables import (CustomIterableOne, CustomIterableThree,
CustomIterableTwo)

iter(CustomIterableOne(4))

iter(CustomIterableTwo(4))

iter(CustomIterableThree(4))

iter(100)

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 @@
"Number" if isinstance(3.14, (int, float)) else "Not a number"

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

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

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

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

"Number" if isinstance("3.14", int | float) else "Not a number"
8 changes: 8 additions & 0 deletions python-isinstance/type_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
shape = "sphere"
number = 8

isinstance(shape, str)

isinstance(number, int)

isinstance(number, float)
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
11 changes: 11 additions & 0 deletions python-isinstance/validating_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def calculate_area(length, breadth):
if isinstance(length, int) and isinstance(breadth, int):
return length * breadth
return "Invalid argument"


calculate_area(5, 3)

calculate_area(5, "3")

calculate_area("5", "3")
Loading