Is there a way to somehow see what parameters are in the list.__init__(...questioned parameters...) constructor?

Hello, i wanted to ask, is there a way to somehow see what parameters are in the list.__init__(...questioned parameters...) constructor?
I started to think about that when i tried to properly override __init__ dunder method. Like here:

class SortedList(list):
  def __init__(self, args):
    super().__init__(args)
    self.sort()

And realised that it would be very interesting to know how exactly __init__ constructor is writen in the very first class of “list”.
Of course i’ve tried to find it by myself but not succeeded.
Could somebody help?
Thank you.
(Sorry mtf for taging, but i couldn’t write under the topic unless i reply to someone and of course you also have a deep knowledge of python.)

We can use the built in dir() function to list all the methods and attributes of an object.

>>> dir(list)
['__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']
>>> 

Our subclass is to have its own .append() method which reaches up to the parent class via the super() function.


Eg.

>>> class SortedList(list):
	def append(self, value):
		super().append(value)
		self.sort()

		
>>> a = SortedList()
>>> a
[]
>>> a.append(7)
>>> a.append(1)
>>> a.append(11)
>>> a
[1, 7, 11]
>>> 

Notice that our append method has .sort() built in. The method is inherited from list.

We see there is an .extend() method in the dir listing above. We can add that method to our class in the same manner.

>>> class SortedList(list):
	def append(self, value):
		super().append(value)
		self.sort()
	def extend(self, values):
		super().extend(values)
		self.sort()

		
>>> a = SortedList()
>>> a.extend([99, 22, 77, 55])
>>> a
[22, 55, 77, 99]
>>> a.extend([66, 11, 44])
>>> a
[11, 22, 44, 55, 66, 77, 99]
>>> a.append(33)
>>> a
[11, 22, 33, 44, 55, 66, 77, 99]
>>> 

Thank you for your quick reply and very detailed explanation, but unfortunately i was asking about something else. I knew about that function “dir()” before, and i also did that task correctly.
What i was talking about is even here the function “dir()” is not showing us what parameters we need to pass into the constructor and what variables are there(in the instance) after we instantiate an instanse of a “list” class.
How can i do for example something like this:

class FakeClass:
  def __init__(self, first=1, second='2', third=None):
    self.first = first
    self.second = second
    self.third = third

fake_instance = FakeClass()

>>> magic_function(fake_instance)
["first=1", "second='2'", "third=None"]

and e.g. for our first example

list_a = list()

>>>magic_function(list_a)
[#all parameters or variables of "list_a"]

Much appreciation for the answering.

1 Like

The list class only takes zero or one argument. When zero argument is given, it instantiates an empty list. When one argument is given, such as a string, it instantiates a list composed of the characters in that string.

What you may be looking for is,

https://docs.python.org/3/library/inspect.html

However, this is venturing afield and one can hardly see the benefit of this level of inspection.

We can, as above, use dir() to compare the class definition to an instance of the class.

>>> dir(FakeClass)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> dir(fake_instance)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'first', 'second', 'third']
>>> 

Note that the instance has the attributes that were set in __init__().

1 Like
>>> [*filter(lambda x: x not in dir(FakeClass), dir(fake_instance))]
['first', 'second', 'third']
>>> 
>>> import inspect
>>> inspect.getmembers(fake_instance, lambda x: type(x) == dict)
[('__dict__', {'first': 1, 'second': '2', 'third': None})]
>>> 
>>> a = inspect.getmembers(fake_instance, lambda x: type(x) == dict)[0][1]
>>> a.get('first')
1
>>> fake_instance.first
1
>>> 
1 Like