What's the difference between the square bracket and dot notations in Python? What's the difference between the square bracket and dot notations in Python? python python

What's the difference between the square bracket and dot notations in Python?


The dot operator is used for accessing attributes of any object. For example, a complex number

>>> c = 3+4j

has (among others) the two attributes real and imag:

>>> c.real3.0>>> c.imag4.0

As well as those, it has a method, conjugate(), which is also an attribute:

>>> c.conjugate<built-in method conjugate of complex object at 0x7f4422d73050>>>> c.conjugate()(3-4j)

Square bracket notation is used for accessing members of a collection, whether that's by key in the case of a dictionary or other mapping:

>>> d = {'a': 1, 'b': 2}>>> d['a']1

... or by index in the case of a sequence like a list or string:

>>> s = ['x', 'y', 'z']>>> s[2]'z'>>> t = 'Kapow!'>>> t[3]'o'

These collections also, separately, have attributes:

>>> d.pop<built-in method pop of dict object at 0x7f44204068c8>>>> s.reverse<built-in method reverse of list object at 0x7f4420454d08>>>> t.lower<built-in method lower of str object at 0x7f4422ce2688>

... and again, in the above cases, these attributes happen to be methods.

While all objects have some attributes, not all objects have members. For example, if we try to use square bracket notation to access a member of our complex number c:

>>> c[0]Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: 'complex' object is not subscriptable

... we get an error (which makes sense, since there's no obvious way for a complex number to have members).

It's possible to define how [] and . access work in a user-defined class, using the special methods __getitem__() and __getattr__() respectively. Explaining how to do so is beyond the scope of this question, but you can read more about it in the Python Tutorial.


. is used for accessing attributes (including methods). [] is used for accessing what are called "items", which typically are the contents of various kinds of container objects.

JavaScript does not distinguish these two things, but Python does. You are correct that [] is used for accessing the data in a list or dict. . is used, for instance, for accessing methods like list.append and dict.update. It is also used for accessing other data on other kinds of objects; for instance, compiled regular expression objects have a pattern attribute holding the regex pattern (you would access it with rx.pattern).

In general, the convention is that [] is used for "open-ended" data storage where you don't know ahead of time how much or what sorts of data the object will hold; . is more commonly used for specific data that the object has "by nature" and which is accessed with a predefined name. For instance, just knowing that something as a list doesn't tell you what's in it (for which you use []), but it does tell you that you can append to it (and to access the append method you use .).

The other major difference between Python and JavaScript in this regard is that in Python, the behavior of both . and [] can be customized by the object. So obj.foo or obj[foo] may do something special if obj is an object that defines its own behavior for them. There are various custom types that make use of this for their own purposes.


[] is the index to a container, such as a list or dictionary.

. is the member of an object and modules. It can be a method, member data, or attribute.

>>> xs = [1, 7, 3, 4, 5, 4, 3, 4, 1]>>> xs.count(4)3>>> xs[1]7