question archive Write in python please :)   Exercise 12-4 Create a class SecretList, which inherits from the built-in list class

Write in python please :)   Exercise 12-4 Create a class SecretList, which inherits from the built-in list class

Subject:Computer SciencePrice: Bought3

Write in python please :)

 

Exercise 12-4

Create a class SecretList, which inherits from the built-in list class. The only method SecretList should include is an overridden __str__ (you don't even need an __init__). Design this method in a way such that when a SecretList is printed out (using the print function), only the first element of the list is shown. If the SecretList has no elements, then __str__ should return the string '[]' as normal.

 

You can assume that SecretLists will only contain integers.

 

Hint:

In SecretList.__str__, self is assumed to be an object of type SecretList. SecretList inherits from list, so any list operation or method should work on self, including indexing and using the len() function.

 

Examples (Values in italics are printed, values in bold are returned).

>>> sec = SecretList()

>>> print(sec)

[]

>>> str(sec)

'[]'

 

>>> sec.append(79) 

>>> sec.append(3)

>>> sec.append(0)

 

>>> len(sec)

3

>>> print(sec)

[79]

>>> str(sec)

'[79]'

 

>>> sec.pop(0)

79

>>> len(sec)

2

>>> print(sec)

[3]

>>> str(sec)

'[3]'

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE