Python中的方括号和点符号有什么区别? [英] What's the difference between the square bracket and dot notations in Python?

查看:37
本文介绍了Python中的方括号和点符号有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自 Javascript 背景(可以通过 .[] 符号访问属性),所以请原谅我,但究竟是什么两者在 Python 中的区别?

从我的实验看来,应该始终使用 [] 来获取 listset 的索引并获取dictionary 中某个键的值.这是正确的,如果不正确,你什么时候在 Python 中使用 . ?

解决方案

点运算符用于访问任何对象的属性.例如一个复数

<预><代码>>>>c = 3+4j

具有(除其他外)两个属性 realimag:

<预><代码>>>>真实3.0>>>图像4.0

除此之外,它还有一个方法,共轭(),它也是一个属性:

<预><代码>>>>c.共轭<0x7f4422d73050处复杂对象的内置方法共轭>>>>c.共轭()(3-4j)

方括号表示法用于访问集合的成员,无论是在字典或其他映射的情况下通过键:

<预><代码>>>>d = {'a': 1, 'b': 2}>>>d['a']1

... 或者在像列表或字符串这样的序列的情况下通过索引:

<预><代码>>>>s = ['x', 'y', 'z']>>>s[2]'z'>>>t = '卡波!'>>>t[3]'哦'

这些集合也分别具有属性:

<预><代码>>>>d. 流行音乐<0x7f44204068c8处dict对象的内置方法pop>>>>s.reverse<0x7f4420454d08处的列表对象的内置方法reverse>>>>t. 更低<在 0x7f4422ce2688 处的 str 对象的内建方法下层>

...再说一次,在上述情况下,这些属性恰好是方法.

虽然所有对象都有一些属性,但并非所有对象都有成员.例如,如果我们尝试使用方括号表示法来访问复数 c 的成员:

<预><代码>>>>c[0]回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:复杂"对象不可下标

...我们得到一个错误(这是有道理的,因为复数没有明显的方法来拥有成员).

可以使用特殊方法 __getitem__()__getattr__() 分别.解释如何这样做超出了这个问题的范围,但您可以在 Python 教程.

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?

解决方案

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.

这篇关于Python中的方括号和点符号有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆