Python __setitem__ (), __getitem__ (), __delitem__ ()

Source: Internet
Author: User


__xxxitem__: Called when manipulating properties using [']

__setitem__: This method is called whenever a property is assigned, so it cannot be assigned a value in the method = value will die loop

__getitem__: This method is called when a nonexistent property is accessed

__delitem__: This method is called when a property is deleted

Class A (object):      def __init__ (self):          self[' B '] = "BB"          self[' d '] = "DD"          del self[' d ']            def __setitem __ (self,name,value):          "         @summary: The method is called whenever the property is assigned, so it cannot be assigned within the method = value will die loop         '          print "__setitem__:set%s Value%s"% (Name,value)          self.__dict__[name] = Value            def __getitem__ (self,name):          " '          @summary: Call the method         '          print ' __getitem__:no attribute named '%s '%name          return None when accessing a property that does not exist            def __delitem__ (self,name):          "'          @summary: This method ' print ' is called when the property is deleted          __delitem__:delect Attribute '%s ' "%name          del self.__dict__[name]          print self.__dict__              if __name__ = =" __main__ ":      X = A (      b = x[' BB ')  


Python __setitem__ (), __getitem__ (), __delitem__ ()

Related Article

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