Class and Object W3D3

Please Explain Ans

If you use the dir() function over the object list_1 = [1,2,3]. One of the methods that will come up is class(). Write the code which shows how to use this method on the object list_1. What is the output of this method once it is used over object list_1.

hi mohsin

I didn’t understand your question completely can you give an example if possible ?

this is the question of quiz assignment W3D3

If you run this code

list_1 = [1,2,3,4]

you will get this output

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

This is a list of methods associated with list object list_1. You can find the familiar ones like append method. If you remember in lists we can use append method to add one element to the list. It is used in the following way below

list_1.append(6) # this changes the list to [1,2,3,4,6]

In the same way you can observe that there is this method in the dir list called __class__ , this is the method that asked in quiz. If you run this method with the object list_1 it will just return empty list

list_1.__class__() # this return []

this output means that list_1 belongs to class list
Please try and run this code in colab to get a feel of the outputs

1 Like

ok sir…got it.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.