What's the difference between the square brack

2019-02-05 18:04发布

问题:

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?

回答1:

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.real
3.0
>>> c.imag
4.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.



回答2:

. 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.



回答3:

[] 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


回答4:

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:

class MyClass:
    def myfunc(self):
        pass

print(MyClass.__dict__)

You will get all attribute (name:value) of this class. Note the entry for myfunc.