Re: Is it correct this way to inherit from a list?

Discussion in 'Python' started by Rick Johnson, Mar 2, 2013.

  1. Rick Johnson

    Rick Johnson Guest

    On Saturday, March 2, 2013 11:02:14 AM UTC-6, gialloporpora wrote:

    > I would like to inherit from the list native class. really
    > I expected that was possible to use native list method
    > without redefining them, for example the __repr__ method.
    >
    > [...]
    >
    > class vector(list):
    > def __init__(self, *args):
    > self._list = list(args)
    > self._index = 0


    Here is where you go wrong.

    First of all why would you inherit from "list" and then create a new list as attribute, that seems a bit silly huh? If you want your custom list to "inherit" all the pre-defined methods of the python list type, then do so.

    >>> class MyList(list):

    pass

    >>> ml = MyList()
    >>> dir(ml)

    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
    >>> ml

    []
    >>> ml.append('cat')
    >>> ml

    ['cat']
    >>> ml.extend(range(5))
    >>> ml

    ['cat', 0, 1, 2, 3, 4]
    >>> ml.sort()
    >>> ml

    [0, 1, 2, 3, 4, 'cat']
    >>> isinstance(ml, list)

    True

    Quacks like a list to me. In this case you did not need to call the superclass constructor explicitly; for example.

    class MyList(list):
    def __init__(self):
    list.__init__(self)

    ....is really a waste of time because you will not have any options to pass to the super.

    >>> ml2 = MyList2()
    >>> dir(ml2)

    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
    >>> ml2+[10,20,30]

    [10, 20, 30]
    >>> ml2

    []
    >>> ml2.append('salt')
    >>> ml2

    ['salt']
    >>> isinstance(ml2, list)

    True

    If however you wanted to create a custom Tkinter widget, you would then need to pass the options from the derived class __init__ method into the superclass __init__ method, like this:

    class MyButton(tk.Button):
    def __init__(self, master, **kw):
    tk.Button.__init__(self, master, **kw)

    mb = MyButton(rootWindow, text='PushMe', command=helloButton)

    What are you trying to achieve exactly?
    Rick Johnson, Mar 2, 2013
    #1
    1. Advertising

  2. On Sun, Mar 3, 2013 at 1:30 PM, gialloporpora <> wrote:
    > Risposta al messaggio di Rick Johnson :
    >
    >
    >> What are you trying to achieve exactly?

    >
    >
    >
    > I would like to implement a class (vector) to works with vectors, for
    > example using scalar multiplication:
    > a*v = [a*v1, a*vn]
    > and a dual class for dual vector (the only method that I'll change is the
    > __str__ method to print it as colun.


    Have you looked at NumPy? I haven't used it myself, but I understand
    it's good for this sort of thing.

    ChrisA
    Chris Angelico, Mar 3, 2013
    #2
    1. Advertising

  3. On 02/03/2013 9:30 PM, gialloporpora wrote:
    > Risposta al messaggio di Rick Johnson :
    >
    >> What are you trying to achieve exactly?

    >
    >
    > I would like to implement a class (vector) to works with vectors, for
    > example using scalar multiplication:
    > a*v = [a*v1, a*vn]
    > and a dual class for dual vector (the only method that I'll change is
    > the __str__ method to print it as colun.
    > Sandro

    Numpy facilitates this sort of thing more efficiently than using a List.

    Colin W.
    Colin J. Williams, Mar 3, 2013
    #3
  4. Rick Johnson

    Jason Swails Guest

    On Sun, Mar 3, 2013 at 9:21 AM, Colin J. Williams <> wrote:

    > On 02/03/2013 9:30 PM, gialloporpora wrote:
    >
    >> Risposta al messaggio di Rick Johnson :
    >>
    >> What are you trying to achieve exactly?
    >>>

    >>
    >>
    >> I would like to implement a class (vector) to works with vectors, for
    >> example using scalar multiplication:
    >> a*v = [a*v1, a*vn]
    >> and a dual class for dual vector (the only method that I'll change is
    >> the __str__ method to print it as colun.
    >> Sandro
    >>

    > Numpy facilitates this sort of thing more efficiently than using a List.
    >


    As a couple people have already pointed out, numpy is the way to go for
    most scientific applications. You have also been given good advice
    regarding 'properly' inheriting from 'list' by calling the list.__init__
    function.

    The only thing I'll add here is that you can inherit from array.array
    instead of list if you want a 'truly' numeric-vector without introducing
    numpy as a dependency. The advantage array.array has over list in this
    instance is that it is type-restrictive for member data (it has to be
    pre-declared and throws TypeError if you try to pass it a bad variable
    type).

    You can then proceed to override the __add__, __sub__, __mul__, and __div__
    methods (and the in-place versions of these operators) to mimic vector
    operations. (By default, __add__ appends the rhs to the lhs and returns a
    copy of that for array.array and list).

    You can avoid all this work, however, and just use numpy.ndarray instead ;).

    Good luck,
    Jason
    Jason Swails, Mar 3, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zhou Fan
    Replies:
    4
    Views:
    328
    Alf P. Steinbach
    Mar 23, 2006
  2. jonnytheclown

    Correct way to inherit from 3rd party class

    jonnytheclown, Feb 21, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    138
    Peter Scott
    Feb 22, 2005
  3. Peter Otten
    Replies:
    0
    Views:
    134
    Peter Otten
    Mar 2, 2013
  4. Ian Kelly
    Replies:
    0
    Views:
    113
    Ian Kelly
    Mar 2, 2013
  5. Ian Kelly
    Replies:
    0
    Views:
    115
    Ian Kelly
    Mar 2, 2013
Loading...

Share This Page