There's a situation involving sub-classing I can't figure out.
I'm sub-classing Random (the reason is besides the point). Here's a basic example of what I have:
import random
class MyRandom(random.Random):
def __init__(self, x): # x isn't used here, but it's necessary to show the problem.
print("Before")
super().__init__() # Nothing passed to parent
print("After")
MyRandom([])
The above code, when run, gives the following error (and doesn't print "Before"):
>>> import test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\_\PycharmProjects\first\test.py", line 11, in <module>
MyRandom([])
TypeError: unhashable type: 'list'
To me, this doesn't make any sense. Somehow, the argument to MyRandom is apparently being passed directly to Random.__init__ even though I'm not passing it along, and the list is being treated as a seed. "Before" never prints, so apparently my initializer is never even being called.
I thought maybe this was somehow due to the parent of Random being implemented in C and this was causing weirdness, but a similar case with list sub-classing doesn't yield an error saying that ints aren't iterable:
class MyList(list):
def __init__(self, y):
print("Before")
super().__init__()
print("After")
r = MyList(2) # Prints "Before", "After"
I have no clue how to even approach this. I rarely ever sub-class, and even rarer is it that I sub-class a built-in, so I must have developed a hole in my knowledge. This is not how I expect sub-classing to work. If anyone can explain what's going on here, I'd appreciate it.
Python 3.9