1

I need to inherit list class and override the init() method to take parameters a,b . a should be the lenght of the list I initialise and b should the step between Items in the list. I just don't know where to start with overriding the init method.

def myclass(list):
    def __init__(a,b,*args, **kwargs):
         pass

I have no idea what to do past this.

I have seen I can do this:

class MyClass(list):
    def __init__(a,b):
        data =[x for x in range(0,a*b,b)]
        list.__init__(self,data)

But the I am not familiar with how python implements the list class, for instance how do I get to use the list comprehension I just passed.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
ArdentLearner
  • 712
  • 7
  • 20
  • Edited the question, and I would appreciate it if you dived into pariculars of how the list class is implemented in pyhon. – ArdentLearner Nov 01 '16 at 03:03
  • Is this homework, or are you trying to solve a specific problem? The reason I'm asking is in Python we don't normally "pre-initialize" lists. – Burhan Khalid Nov 01 '16 at 03:03
  • @donkopotamus: You only override `__new__` for immutable types; for `list`, `__init__` is fine. – ShadowRanger Nov 01 '16 at 03:20

3 Answers3

1

Thanks to everyone who responded. Realised I could achieve what I wanted to this way:

class myclass(list):
    def __init__(self,a,b):
        data =[x for x in range(0,a*b,b)]
        self.length = len(data)
        super(myclass, self).__init__()
        self.extend(data)
ArdentLearner
  • 712
  • 7
  • 20
0

You should use super to call the list methods, in this case it will look something like this:

class myclass(list):
    def __init__(self, a, b, *args, **kwargs):
        super(myclass, self).__init__() # this will call the list init
        # do whatever you need with a and b

l = myclass(10, 0)
l.append(10) # this will calls list append, since it wasn't overriden.
print l
Fernando Coelho
  • 237
  • 1
  • 8
  • Thank you for the responding, I would appreciate it if you checked the edited question. – ArdentLearner Nov 01 '16 at 03:05
  • I don't think you can do this because I believe the [] reserved for lists. There are a few internal operators you can override, for instance the access method (l[1], for instance), by implementing the \_\_getitem\_\_ method. The list.__init__() is the old class style way of doing the same thing I did with super in my example. – Fernando Coelho Nov 01 '16 at 03:27
0
#!/usr/bin/python


class myclass:

    # use the init to pass the arguments to self/the class
    def __init__(self, list, num):
        self.list = list
        self.num = num


    # use this to use the variables
    def use_the_stuff(self):
        #prints the item in the given place
        # i.e in a list of ["A","B","C"] 
        # if self.num is 0, then A will be printed.
        print self.list[self.num]


list_abc = ["A", "B", "C"]
myclass(list_abc, 2).use_the_stuff()

Basically uses a class with init to take the list in and do stuff with it.

Jasmit Tarang
  • 135
  • 1
  • 7