Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does appending [0] to methods in python do?

Tags:

python

What does appending [0] to methods in python do? For example, in the following [0] is appended to a method.

print "Unexpected error:", sys.exc_info()[0]
like image 258
PythonRunner Avatar asked Jan 28 '26 04:01

PythonRunner


1 Answers

It calls the method and gets the 0th element from the returned value.

>>> def test():
...   return ['item0', 'item1']
...
>>> test()
['item0', 'item1']
>>> test()[0]
'item0'
>>>
like image 59
CraigTeegarden Avatar answered Jan 29 '26 19:01

CraigTeegarden