8.python Face Object part.7 (__setitem__,__getitem,__delitem__)

Source: Internet
Author: User

OBJ[XXX] = "XX" This action triggers the __setitem__ method of the object.

del obj["xxx"] This action triggers the __delitem__ method of the object.

obj["XXX"] triggers the __getitem__ method of the object.




Class Foo:

def __init__ (self,name):

Self.name=name

def __getitem__ (self, item):

Print (Self.__dict__[item])

def __setitem__ (self, Key, value):

Self.__dict__[key]=value

def __delitem__ (self, key):

Print (' del Obj[key], I execute ')

Self.__dict__.pop (Key)

def __delattr__ (self, item):

Print (' del obj.key, I execute ')

Self.__dict__.pop (item)

F1=foo (' SB ')

f1[' age ']=18

f1[' Age1 ']=19

Del F1.age1

Del f1[' age ']

f1[' name ']= ' AAA '

Print (f1.__dict__)


This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1918007

8.python Face Object part.7 (__setitem__,__getitem,__delitem__)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.