Let's say I want to implement some list class in python with extra structure, like a new constructor. I wrote:
import random
class Lis(list):
def __init__(self, n):
self = []
for i in range(n):
self.append(random.randint(0, 30))
Now doing Lis(3) gives me an empty list. I don't know where I did it wrong.
You are overriding the object with self = []
try the following
import random
class Lis(list):
def __init__(self, n):
for i in range(n):
self.append(random.randint(0, 30))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With