Skip to content
Advertisement

Python square brackets between function name and arguments: func[…](…)

I was learning how to accelerate python computations on GPU from this notebook, where one line confuses me:

mandel_kernel[griddim, blockdim](-2.0, 1.0, -1.0, 1.0, d_image, 20)

Here, mandel_kernel is a decorated (by cuda.jit) function, griddim and blockdim are tuples of length 2: griddim=(32,16), blockdim=(32,8).

Is this square brackets in between function name and argument list part of python syntax, or something specific to the cuda.jit decoration?

Advertisement

Answer

This is valid python syntax, i’ll try to break it down for you:

mandel_kernel is a dict, whose keys are 2-tuples (griddim, blockdim) and values are method (which is valid since methods are objects in python)

mandel_kernel[griddim, blockdim] therefore ‘returns’ (or evaluates to) a method

mandel_kernel[griddim, blockdim](-2.0, 1.0, -1.0, 1.0, d_image, 20) therefore calls that method with whatever arguments are inside the parenthesis.

This one line could be rewritten in three lines like so:

key = tuple(griddim, blockdim)
method = mandel_kernel[key]
method(-2.0, 1.0, -1.0, 1.0, d_image, 20)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement