0
class Myclass(list):

    def remove_max(self):
        self.remove(max(self))

l = [1,2,3]

object = Myclass(l)

object.remove_max()

print(object)

in Myclass which inherits from class list why does the Myclass(l) assign the object a value [1,2,3]? And even if we gave it a string say 'abc' instead of list l as an input why is the value of object set as the list ['a'. 'b']?

2
  • Could you please clarify the code and the question? I would guess the last four lines are not supposed to have indentation? And what do you mean with "assign"? object = Myclass(l) assigns [1,2,3] as the list constructor would. The next line removes the element 3. Commented Jun 15, 2017 at 13:37
  • yeah im sorry for the incorrect indentation.i have edited the code now Commented Jun 16, 2017 at 14:52

1 Answer 1

1

As you do not define an __init__ method, the parameter given at construction time is given to the immediate parent, here list. And when you build a list from an iterable, you get the list composed from the different items. So:

  • if l is a list list(l) is a copy of the initial list
  • if s is a string (which is an iterable of characters), list(s) is the list composed with the characters from s
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.