Skip to content

Commit

Permalink
Add examples for weird things about python
Browse files Browse the repository at this point in the history
  • Loading branch information
ABDreos committed Sep 10, 2024
1 parent 38d3189 commit 273d382
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 0 deletions.
12 changes: 12 additions & 0 deletions 2024/weird_things/booleans_subclassing_integers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def main() -> None:
if 1:
print("1 is True")

print(1 == True)
print(0 == False)
print(1 + True)
print(True + True)


if __name__ == "__main__":
main()
17 changes: 17 additions & 0 deletions 2024/weird_things/default_empty_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def append_to_list[T](value: T, my_list: list[T] = []) -> list[T]:
my_list.append(value)
return my_list


def main() -> None:
# First call
result1 = append_to_list(1)
print(result1) # Output: [1]

# Second call
result2 = append_to_list(2)
print(result2) # Output: [1, 2]


if __name__ == "__main__":
main()
10 changes: 10 additions & 0 deletions 2024/weird_things/delayed_printing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import time


def main() -> None:
print("Hello world", end="_")
time.sleep(3)


if __name__ == "__main__":
main()
17 changes: 17 additions & 0 deletions 2024/weird_things/for_else_clause.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def check_element_existence[T](data: list[T], to_find: T) -> None:
for num in data:
if num == to_find:
print("Exists!")
break
else:
print("Does not exist")


def main() -> None:
data = [1, 2, 3, 4, 5]
check_element_existence(data, 4)
check_element_existence(data, -1)


if __name__ == "__main__":
main()
12 changes: 12 additions & 0 deletions 2024/weird_things/function_attribute_mutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def timer():
pass


def main() -> None:
timer.counter = 0
timer.counter += 1
print(timer.counter) # Output: 1


if __name__ == "__main__":
main()
12 changes: 12 additions & 0 deletions 2024/weird_things/integer_caching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def main() -> None:
a = 256
b = 256
print(a is b) # Output: True

c = 257
d = 257
print(c is d) # Output: False


if __name__ == "__main__":
main()
10 changes: 10 additions & 0 deletions 2024/weird_things/mutating_immutable_struct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Run the following in the Python console
# Expected bevahior! data is (1,2)
data = (1, 2)
data[0] += 1 # Returns an error
data # Is (1,2)

# Not expected! data is ([1, 3], 2)
data = ([1],2)
data[0] += [3]
data
14 changes: 14 additions & 0 deletions 2024/weird_things/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[tool.poetry]
name = "weird-things"
version = "0.1.0"
description = ""
authors = ["Andreas Bergman <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.11"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
14 changes: 14 additions & 0 deletions 2024/weird_things/reference_inconsistency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def main() -> None:
a = [1, 2, 3]
b = a # b is now a reference to a
a += [4]
print(b) # Output: [1, 2, 3, 4]

s = "hello"
t = s # t is now a reference to s
s += " world"
print(t) # Output: "hello"


if __name__ == "__main__":
main()
8 changes: 8 additions & 0 deletions 2024/weird_things/space_invader_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def main() -> None:
a = 420
a -=- 1
print(a)


if __name__ == "__main__":
main()
9 changes: 9 additions & 0 deletions 2024/weird_things/variable_global_scope_a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def main() -> None:
for x in range(7):
if x == 6:
print(x, ": for x inside loop")
print(x, ": x in global/scope")


if __name__ == "__main__":
main()
7 changes: 7 additions & 0 deletions 2024/weird_things/variable_global_scope_b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def main() -> None:
[x for x in range(7) if x == 6 and print(f"{x}: for x inside loop")]
print(x, ": x in global") # will return an error


if __name__ == "__main__":
main()

0 comments on commit 273d382

Please sign in to comment.