Last active
March 24, 2025 16:19
Interesting Python Problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseMeta(type): | |
def __init__(cls, *args): | |
cls.f = cls.f # comment this line and run again! | |
pass | |
def f(cls): | |
print(cls) | |
class Derived(metaclass=BaseMeta): | |
pass | |
Derived().f() # <class '__main__.Derived'> # gives AttributeError for the other case! | |
# Derived.f() # works for both cases | |
# https://chatgpt.com/share/67e17ede-f60c-800f-b4b6-60a086c6f9bf | |
# Without the statment `cls.f = cls.f`, the `Derived` class has the classmethod `f()` as an atttribute that is called using the | |
# `__getattribute__()` function; otherwise, it explicitly copies the method to the `Derived` class `__dict__`, so it available | |
# to its instances as well. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
a = np.array([1]), np.array([1]) | |
assert np.array_equal(a, a) # True | |
a = np.array([1, 1]), np.array([1]) | |
assert np.array_equal(a, a) # False! | |
# https://chatgpt.com/share/67e184e0-a2c4-800f-b8fe-566d4f603b6d | |
# `np.array_equal` forms a regular multi‐dimensional array when the subarrays are of the same size, allowing element‐wise comparison. | |
# With different sizes, it falls back to an object array where equality isn’t defined element‐wise, so the comparison becomes ambiguous. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment