Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does numpy.r_ use brackets instead of parentheses?

Tags:

python

numpy

Numpy.r_, .c_ and .s_ are the only Python functions I've come across that take arguments in square brackets rather than parentheses. Why is this the case? Is there something special about these functions? Can I make my own functions that use brackets (not that I want to; just curious)?

For example, the proper syntax is:

    np.r_['0,2', [1,2,3], [4,5,6]]

I would have expected it to be:

    np.r_('0,2', [1,2,3], [4,5,6])
like image 721
kdamica Avatar asked May 26 '13 01:05

kdamica


People also ask

What is the difference between parentheses and brackets in Python?

What is the difference between parentheses (), brackets [], and braces {} in a python code? Brackets are used to make lists Braces are used to make dictionary Parenthesis are used to make tuple But for indexing in all of those, only brackets are used.

Why are brackets used in Python?

[] (Index brackets) Index brackets ([]) have many uses in Python. First, they are used to define "list literals," allowing you to declare a list and its contents in your program. Index brackets are also used to write expressions that evaluate to a single item within a list, or a single character in a string.

Why do we use double square brackets in Python?

Indexing DataFrames You can either use a single bracket or a double bracket. The single bracket will output a Pandas Series, while a double bracket will output a Pandas DataFrame. Square brackets can also be used to access observations (rows) from a DataFrame.

Do brackets matter in Python?

[] brackets are used for lists. List contents can be changed, unlike tuple content. {} are used to define a dictionary in a "list" called a literal. "parentheses ... are referred to as tuples" -- Totally incorrect.


1 Answers

Any Python class can be made so that its instances accept either or both notation: it will accept parens by implementing a function called __call__, and brackets by implementing __getitem__.

np.r_ happens to be of a class that implements __getitem__ to do fancier things than its usual. That is, the class of r_ (called np.lib.index_tricks.RClass) does something like this:

class RClass:
    def __getitem__(self, item):
        # r_ fancyness

Likely, this was done so that it can take advantage of slice notation - eg, when you have a list (or np array or any other object implementing this protocol) l, and you do:

l[:5]

, Python automatically creates a slice object to pass to __getitem__.

This syntax doesn't work with __call__ - a user would have to create the slice explicitly, by doing l(slice(5)).

Note that __call__ can take whatever arguments you like; while __getitem__ always takes exactly one argument: when you do something like my_array[1:3, 2:5], Python passes in a single tuple of slices. But, as you see with r_, the contents aren't restricted to numbers and slices - similarly to any other function, Python will happily pass in any object and leave it to the class to work out what it means.

like image 198
lvc Avatar answered Oct 20 '22 18:10

lvc