#!/usr/bin/python3 # -*- coding: utf-8 -*- """ The first line is constructing an object of type list, the second and third lines are calling the append() method, the fourth line is calling the sort() method, and the fifth line is retrieving the item at position 0. The sixth line is calling the __getitem__() method in the stuff list with a parameter of zero. print (stuff.__getitem__(0)) The seventh line is an even more verbose way of retrieving the 0th list item. print (list.__getitem__(stuff,0)) In this code, we care calling the __getitem__ method in the list class and passing in the list (stuff) and the item we want retrieved from the list as parameters. The last three lines of the program are completely equivalent, but it is more convenient to simply use the square bracket syntax to look up an item at a particular position in a list. We can take a look into the capabilities of an object by looking at the output of the dir() function: """ from typing import Any, List # %% Classes # Everything is a class, like lists: stuff: List[str] = list() stuff2: List[str] = [] # Those classes have "member functions": stuff.append("thing") stuff.append("python") stuff.sort() # Each of below are equivalent: print(stuff[0]) print(stuff.__getitem__(0)) print(list.__getitem__(stuff, 0)) # Step into them in spyder to see. # If a thing has __getitem__() then [] can be used on it! # Duck functions can apply to objects of classes, # if those objects walk like ducks and talk like ducks. # See things list provides print(dir(stuff))