首页 > 编程语言 >python 魔术方法:序列语义方法 __len__/__detitem__/__setitem__/__delitem__

python 魔术方法:序列语义方法 __len__/__detitem__/__setitem__/__delitem__

时间:2022-11-02 20:13:19浏览次数:37  
标签:__ xl python self args detitem MyClass def

python 魔术方法:序列语义方法 :
1. 作用: 自定义序列的相关魔法方法允许我们自己创建的类拥有序列的特性,让其使用起来就像 python
的内置序列(dict,tuple,list,str,set)可以被遍历的对象

2. 序列魔术方法介绍 :
用于返回序列长度:__len__ 
用于按下标获取序列中的元素:__getitem__
用于按下标设置序列的值:__setitem__
用于删除某个下标的元素:__delitem__
# __len__方法 的使用
 class MyClass:

     def __init__(self,args):
         self.xl = list(args)

     def __len__(self):
         return len(self.xl)

 a = MyClass([11,22,33,44])
 print(len(a))

 

序列语义:

 

 class MyClass:

     def __init__(self,*args):
         self.xl = list(args)

     pass
 a = MyClass()
 print(a[0])

 

可以看到对象本身并不支持 序列的索引方法
TypeError: 'MyClass' object is not subscriptable

 

__getitem__方法:

class MyClass:

    def __init__(self,*args):
        self.xl = list(args)

    # __getitem__ 将对象变成可操作的对象
    def __getitem__(self, item):
        return self.xl[item]

a = MyClass(11,22,33,44)
print(
    a[0]
)
输出结果 11

 

__setitem__ 方法:可以实现对对象的值进行修改
class MyClass:

    def __init__(self, *args):
        self.xl = list(args)

    # __getitem__ 将对象变成可操作的对象
    def __getitem__(self, item):
        return self.xl[item]

    def __setitem__(self, key, value):
        self.xl[key] = value
        return self.xl

    def __str__(self):
        return str(self.xl)

a = MyClass(11, 22, 33, 44)

a[0] = 9999
print(a)


""" 输出结果:[9999, 22, 33, 44]"""

  

__delitem__ :实现将对象的key 删除

class MyClass:

    def __init__(self, *args):
        self.xl = list(args)

    # __getitem__ 将对象变成可操作的对象
    def __getitem__(self, item):
        return self.xl[item]

    def __delitem__(self, key):

      del self.xl[key]


    def __str__(self):
        return str(self.xl)

a = MyClass(11, 22, 33, 44)

del a[0]
print(a)

  

 

 

标签:__,xl,python,self,args,detitem,MyClass,def
From: https://www.cnblogs.com/manxingsir/p/16852244.html

相关文章

  • 嵌入式-C语言基础:字符串比较函数strcmp及其实现
    #include<stdio.h>#include<string.h>intmystrcmp(char*p1,char*p2){intret=0;if(p1!=NULL||p2!=NULL){while(*p1==*p2){......
  • kubectl explain --recursive
    Addthe--recursiveflagtodisplayallofthefieldsatoncewithoutdescriptions.InformationabouteachfieldisretrievedfromtheserverinOpenAPIforma......
  • 安装 openstack
    一、openstack组件1.Horizon管理openstack各种服务,基于web管理接口2.Keystone管理认证,提供授权和认证管理服务3.Nova在节点上管理虚拟机的服务4.Neutron软件定义网......
  • 软件企业区域月度交付会议的改进方式
    文/王不留(微信公众号:程序员生存指南) 1.受行业影响,我们的软件产品以现场实施为主。公司壮大后,采用了总部+区域经营单元的组织模式,区域分布广,所属项目多。 为了加强总部和......
  • 软件企业研发人员能力提升方式
    文/王不留(微信公众号:程序员生存指南) 1.作为一家行业软件企业,公司目前有七大产品线,四十项主产品,以及延伸出的几十种定制化产品。其中,多款主打产品,在行业内占有率很高,是公......
  • day8
    [0206.翻转链表]/***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),next(nullptr......
  • 如果你是项目主管,你会不会炒了这样刁难你的客户?
    文/王不留(微信公众号:程序员生存指南) 一位兄弟向我抱怨,不想干项目经理了,客户处处刁难他,天天找他麻烦。一天24小时不让他安宁。现在只想两耳不闻窗外事,一心只想写代码。 ......
  • 软件企业研发体系常见问题及解决思考
    文/王不留(微信公众号:程序员生存指南) ◎ 一个公司的运营通常可以分成四大块:售前、研发、交付、职能。 售前主要包含销售人员和咨询人员。项目型公司会以售前为主力,重点......
  • 如何迎新年?手工自制2021年精美简朴行事历
    文/王不留(微信公众号:王不留) 即将迈入2021年,要不自己做了日历吧。这次就推荐两个制作日历的网站。我们手工自制行事历。 如果需要一页纸一个月,并能留出空格标记事项,我们......
  • 如何成为某个领域的达人?
    文/王不留(微信公众号:程序员生存指南)  虽然互联网的世界,想成为网红,需要一个事件或幕后推手进行营销。但对自己的专业领域的知识储备也有很高要求。 要强化自己的专业知......