Different meanings of brackets in Python

前端 未结 3 1648
时光说笑
时光说笑 2020-11-30 23:17

I am curious, what do the 3 different brackets mean in Python programming? Not sure if I\'m correct about this, but please correct me if I\'m wrong:

[] - # No         


        
3条回答
  •  粉色の甜心
    2020-11-30 23:32

    In addition to Maltysen's answer and for future readers: you can define the [] and () operators in a class, by defining the class methods:

    • __call__(self[, args...]) for ()
    • __getitem__(self, key) for []

    An example is numpy.mgrid[...]. In this way you can define it on your custom-made objects for any purpose you like.

提交回复
热议问题