__getitem__ 专用方法

Python中__getitem__方法详解
本文详细介绍了Python中__getitem__方法的工作原理及其使用方式。该方法在类中实现后,可以通过特定语法由Python自动调用,实现对类实例中元素的获取。文章还解释了这种语法糖背后的机制,并提供了实例说明。

(1)  __getitem__ 专用方法很简单。像普通的方法 clear,keys 和 values 一样,它只是重定向到字典,返回字典的值。但是怎么调用它呢?哦,你可以直接调用 __getitem__,但是在实际中你其实不会那样做:我在这里执行它只是要告诉你它是如何工作的。正确地使用 __getitem__ 的方法是让 Python 来替你调用。
(2)  这个看上去就像你用来得到一个字典值的语法,事实上它返回你期望的值。下面是隐藏起来的一个环节:暗地里Python 已经将这个语法转化为 f.__getitem__("name") 的方法调用。这就是为什么 __getitem__ 是一个专用类方法的原因,不仅仅是你可以自已调用它,还可以通过使用正确的语法让 Python 来替你调用。

 

你甚至可以重新定义的它!

 

http://blog.youkuaiyun.com/yuan_j_y/article/details/9317817

 

转载于:https://www.cnblogs.com/qingyuanjushi/p/5990928.html

报错信息 C:\Users\30342\AppData\Local\Programs\Python\Python39\python.exe "C:\Users\30342\Desktop\trial\python ascycio库\asynciohttp_practice\asynciohttp_access.py" Traceback (most recent call last): File "C:\Users\30342\Desktop\trial\python ascycio库\asynciohttp_practice\asynciohttp_access.py", line 1, in <module> import asyncio,aiohttp File "C:\Users\30342\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\__init__.py", line 6, in <module> from .client import ( File "C:\Users\30342\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\client.py", line 170, in <module> class _RequestOptions(TypedDict, total=False): File "C:\Users\30342\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\client.py", line 196, in _RequestOptions middlewares: Optional[Sequence[ClientMiddlewareType]] File "C:\Users\30342\AppData\Local\Programs\Python\Python39\lib\typing.py", line 243, in inner return func(*args, **kwds) File "C:\Users\30342\AppData\Local\Programs\Python\Python39\lib\typing.py", line 316, in __getitem__ return self._getitem(self, parameters) File "C:\Users\30342\AppData\Local\Programs\Python\Python39\lib\typing.py", line 433, in Optional return Union[arg, type(None)] File "C:\Users\30342\AppData\Local\Programs\Python\Python39\lib\typing.py", line 243, in inner return func(*args, **kwds) File "C:\Users\30342\AppData\Local\Programs\Python\Python39\lib\typing.py", line 316, in __getitem__ return self._getitem(self, parameters) File "C:\Users\30342\AppData\Local\Programs\Python\Python39\lib\typing.py", line 421, in Union parameters = _remove_dups_flatten(parameters) File "C:\Users\30342\AppData\Local\Programs\Python\Python39\lib\typing.py", line 215, in _remove_dups_flatten all_params = set(params) File "C:\Users\30342\AppData\Local\Programs\Python\Python39\lib\typing.py", line 694, in __hash__ return hash((self.__origin__, self.__args__)) TypeError: unhashable type: 'list'
最新发布
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值