The use of __getitem__, __setitem__, and __delitem__ in Python

Source: Internet
Author: User

Reference: Bole Address

Used for indexing operations, such as dictionaries. Each of these means getting, setting, deleting data

#Python __doc__
class C (object): "" "
    Holy shit What The fuck" "
    def __init__ (self):
        self.value = {}
        Self.name = ' Wupeiqi '

    def __getitem__ (self, item):
        print ' __getitem__ ', item return
        Self.value[item]

    def __setitem__ (self, Key, value):
        print ' __setitem__ ', key, value
        Self.value[key] = value

    def __delitem_ _ (Self, key):
        print ' __delitem__ ', key
        del Self.value[key]
    def __len__ (self): return
        Len ( Self.value)

print c.__doc__ c

= C ()
#print c
#result = c[' K1 ']
c[' k2 '] = 5
c[' k1 '] = " Hello "
print c[' K2"]
print len (c)

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.