Skip to content

Instantly share code, notes, and snippets.

@Kilo59
Last active August 19, 2022 18:11
Show Gist options
  • Save Kilo59/f055b2863351675e7f8d89ddd040c9ff to your computer and use it in GitHub Desktop.
Save Kilo59/f055b2863351675e7f8d89ddd040c9ff to your computer and use it in GitHub Desktop.
Fun with Python id()
# ##############
# at what number does this error?
# 0? 100,000? never?
# ##############
try:
for i, n in enumerate(range(0, 100000)):
assert i is n, f"{i} is {n}"
except AssertionError as e:
print(f"Error on -> {e}")
# ###
# str
# ###
foo1 = "foo"
foo2 = "foo"
print(f"foo1\t id:{id(foo1)}")
print(f"foo2\t id:{id(foo2)}")
assert foo1 is foo2
bar = "bar"
print(f"bar\t id:{id(bar)}")
assert foo1 is not bar
# ############
# id shadowing
# ############
def fake_id(x):
print(f"fake_id called with {x}")
return "fake_id"
def my_func(id):
print(f"id = {id.__name__}()")
assert foo1 is not bar
print("end of my_func")
my_func(fake_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment