I come from a Javascript background (where properties can be accessed through both .
and []
notation), so please forgive me, but what, exactly, is the difference between the two in Python?
From my experimentation it seeems that []
should always be used, both to get the index of a list
or set
and to get the value from a certain key in a dictionary
. Is this correct, and, if not, when do you use a .
in Python?
[]
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..
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 likelist.append
anddict.update
. It is also used for accessing other data on other kinds of objects; for instance, compiled regular expression objects have apattern
attribute holding the regex pattern (you would access it withrx.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. Soobj.foo
orobj[foo]
may do something special ifobj
is an object that defines its own behavior for them. There are various custom types that make use of this for their own purposes.The dot operator is used for accessing attributes of any object. For example, a complex number
has (among others) the two attributes
real
andimag
:As well as those, it has a method,
conjugate()
, which is also an attribute: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:
... or by index in the case of a sequence like a list or string:
These collections also, separately, have attributes:
... 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
:... 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.Actually, Python uses the square brackets to enclose a key. For lists, this is an integer (unsigned) index or a slice, while for dicts this is a (hasable) object like string, tuple, etc. or even an integer (singe and unsigned). This is straight-forward to many other languages which use a similar or even identical syntax.
The
.
is used to access members of an object, much like for C++, C, Java, JavaScript, etc. It would be quite simple to write a dictionary class which allows to use the dot-syntax to access its elements. However, for this, the keys have to be valid Python identifier(letter { letter | digit |
_}
. However, this is not very common.A set does not support indexing, as it is not ordered internally and there is not relation between a "key" and a "value". For a list, you do not "get the index", but you do "get the value for an index". For a dict this is similar, but the "index" is more flexible. However, the dict does not allow slicing and is (like the set) unordered.
Sidenote: Python uses an internal dict for an object to organize its members. Just try on the console:
You will get all attribute (name:value) of this class. Note the entry for
myfunc
.