PYTHON_67 special Member of object-oriented class Setitem/getitem/delitem

Source: Internet
Author: User

#成员的种类: Normal field  static field normal  method   static method  class method  General characteristics
Class FFF:
x= ' xxxxxdddd '
__x1= ' x11111111 ' __ ' is a member modifier, and X1 is the modified variable field that is only accessed internally
def __init__ (self):
Print (' 111 ')
Self.__name= ' name222 ' #这个name依旧是那个私有的字段 is still not to be called
def fetch (Self,bak):
Print (BAK)
Print (FFF.__X1)
def __call__ (self, *args, **kwargs):
Print (' calll1111 ')



Class Bar (FFF):
def fetch (self):
Print (' Bbbbar ')

Bbb=bar ()
BBB ()
bbb[' K1 ']
Output ————————————————————————————————————————————————————————————————————————————————————————————————————————————— —————————————————————————————————————————————————————————

Traceback (most recent):
File "d:/users/administrator/pycharmprojects/untitled/venv/yingshe/chengyuanxiushifu.py", line A, in <module >
bbb[' K1 ']=5
TypeError: ' Bar ' object does not support item assignment
111
calll1111

This bbb[' K1 ' is an error because there is no way to call the Add Member __getitem__ ()

*************************************************************************************************************** *************************************************************************************************************** ************************************************

#成员的种类: Normal field  static field normal  method   static method  class method  General characteristics
Class FFF:
x= ' xxxxxdddd '
__x1= ' x11111111 ' __ ' is a member modifier, and X1 is the modified variable field that is only accessed internally
def __init__ (self):
Print (' 111 ')
Self.__name= ' name222 ' #这个name依旧是那个私有的字段 is still not to be called
def fetch (Self,bak):
Print (BAK)
Print (FFF.__X1)
def __call__ (self, *args, **kwargs):
Print (' calll1111 ')
def __getitem__ (self, item):
Print (item)



Class Bar (FFF):
def fetch (self):
Print (' Bbbbar ')

Bbb=bar ()
BBB ()
bbb[' K1 '] #中括号 The default execution of GetItem methods and the SetItem Delitem method This set of operations is modeled after the dictionary operation
Output ————————————————————————————————————————————————————————————————————————————————————————————————————————————— —————————————————————————————————

111
calll1111
K1

Here we go, no more errors.

*************************************************************************************************************** *************************************************************************************************************** ***********************************************

#成员的种类: Normal field  static field normal  method   static method  class method  General characteristics
Class FFF:
x= ' xxxxxdddd '
__x1= ' x11111111 ' __ ' is a member modifier, and X1 is the modified variable field that is only accessed internally
def __init__ (self):
Print (' 111 ')
Self.__name= ' name222 ' #这个name依旧是那个私有的字段 is still not to be called
def fetch (Self,bak):
Print (BAK)
Print (FFF.__X1)
def __call__ (self, *args, **kwargs):
Print (' calll1111 ')
def __getitem__ (self, item):
Print (item)
def __setitem__ (self, Key, value):
Print (Key,value)



Class Bar (FFF):
def fetch (self):
Print (' Bbbbar ')

Bbb=bar ()
BBB ()
bbb[' K1 ']=123
Output ————————————————————————————————————————————————————————————————————————————————————————————————————————————— —————————————————————————

111
calll1111
K1 123

*************************************************************************************************************** *************************************************************************************************************** ***********************************************

#成员的种类: Normal field  static field normal  method   static method  class method  General characteristics
Class FFF:
x= ' xxxxxdddd '
__x1= ' x11111111 ' __ ' is a member modifier, and X1 is the modified variable field that is only accessed internally
def __init__ (self):
Print (' 111 ')
Self.__name= ' name222 ' #这个name依旧是那个私有的字段 is still not to be called
def fetch (Self,bak):
Print (BAK)
Print (FFF.__X1)
def __call__ (self, *args, **kwargs):
Print (' calll1111 ')
def __getitem__ (self, item):
Print (item)
def __setitem__ (self, Key, value):
Print (Key,value)
def __delitem__ (self, key):
Print (' Delitem ')



Class Bar (FFF):
def fetch (self):
Print (' Bbbbar ')

Bbb=bar ()
BBB ()
bbb[' K1 ']=123
Del bbb[' K1 ']
Output ————————————————————————————————————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————

111
calll1111
K1 123
Delitem

*************************************************************************************************************** *************************************************************************************************************** **********************************************

*************************************************************************************************************** *************************************************************************************************************** **********************************************

PYTHON_67 special Member of object-oriented class 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.