0

I am trying to understand what are the technical and philosophical differences between the subscription method and simply calling an attribute. For example, in pandas, you can call a column either by subscribing it like so df['col_name'] or calling it as an attribute df.col_name.

Although I think pandas is kind of unique in the sense that with most objects these methods are not interchangeable, I don't understand why both methods exist. For example, why is it inappropriate to call a list item like my_list.0 but my_list[0] is OK. Or why most user-defined classes are non-subscribable by default. Can someone point out what are the different use cases for the two methods and why one may be preferable in certain situations?

Also, pretty much every Python OOP tutorial goes into detail on how to define a class attribute and call it. But I have not seen an explanation of how to make an object subscribable. Is that typically done through inheritance from dict?

Thank you.

nijshar28
  • 173
  • 7

0 Answers0