Python3 the ability to learn such members, extend the parent class method

Source: Internet
Author: User

# #类成员包括: #字段: Static fields, normal fields      fields can also be understood as "variables" #方法: Common method, static method, class method; All methods belong to class      Methods can also be understood as "functions" #属性: With the definition of a method, with the form of field access           call method with field access, you need to define "properties" #当不想创建对象访问类中方法时, the method needs to be a static method # #成员修饰符 # Public fields     both inside and outside can access the #__name    private field, preceded by two underscores, accessible only in the class, &NBSP cannot be accessed through objects, and cannot be called in inherited parent classes and subclasses, only in this class #ps: It is not the last resort to access private fields externally,obj._foo__name    that way can access private fields # #特殊成员  __xxxx__#__init__      construction method, automatic execution of #__del__      when creating objects   destructor, the function is automatically executed before reclaiming the memory occupied by the idle object, does not need its own definition, Python automatically executes #__doc__       comment, description #__class__      the current Class #__call__      object is appended with parentheses, calling the __call__ method of the class, __call__ needs to be defined in the class #__str __       is the memory address of the object when the object is printed, and after the __str__ method is defined in the class, the output content and format can be customized #__dict__       Print all the methods that the object contains #__getitem__   __setitem__  __delitem__    These methods are actually called when the object argument is a list #__iter__The       object is not iterated by default, and when the method is defined, the __iter__ method (which is the rule) of the object is called by default when the object is placed in a For loop #issubclass (CLS1,CLS2)      determines if CLS1 is a subclass of CLS2, returns True or False#isinstance (obj,classname)     Determine if obj is an instance of classname (inheritance is also considered) # #调用父类方法 # Method 1   The method of actively executing the parent class:   super (Subclass, Self). The parent class method, which is used when extending the functionality of the parent class method The method name of the parent class and subclass needs the same # method 2   is called through the class, generally not ## class foo:#     cc =  123    #CC叫静态字段, saved in class, can be accessed by "over class. Field" or through object access, Java can only be accessed by class ##     def  __init__ (self):#         self.name =  ' test '     #name   called field (normal field), save in Object ##     def show (self):       #普通方法, calling   ,self by object must have #         print (self.name) #          return  "Sssss" ##     @ Staticmethod    &nBSP; #静态方法定义, called by the class, equivalent to the function #     def f1 (x):          #参数可有可无 #         print (x+ "==lllllll") ##       @classmethod       #类方法定义 #     def f2 ( CLS):      #cls   parameters must have, is the class name, called by the class #          print (CLS) ##      @property     #属性定义    calls through Foo.f3 #      def f3 (self):    #self必须有 #          return self.name + 1##      @f3 .setter     #给属性传值    via  foo.f3 = 123#     def f3 (self,value): #          print (value) ##      @f3. deleter    #属性删除, not to delete F3, justCall F3, execute code for pass (can be arbitrary code)   via DEL  FOO.F3#     DEF F3 (self):#          pass### class shuxing:##      def f1 (self):#         pass##      DEF F2 (Self,value): #         print (value) ##      def f3 (self):#         pass##      def __getitem__ (Self, item):#          pass##     def __setitem__ (Self, key, value):#          PASS##     DEF __DELITEM__ (self,  Key): #         pass###     foo =  property (FGET=F1,FSET=F2, fdel=f3)     #另一种属性定义方式,fget fset fdel  is a fixed keyword # #注意: Default rules   Static fields are accessed by class; Normal fields are accessed through the object # obj = foo () # print (FOO.F1 ("IIIII"))     #静态方法调用 # print (Obj.show ())          #普通方法调用 # foo.f2 ()           #类方法调用 # print (foo.f3)            #属性调用, no brackets #  d = foo.__dict__# print (Type (d), D) #扩展字典, increase the order of the dictionary properties, with the help of the list # dictionary can be assigned, you can print class mydict (dict) :     def __init__ (self):        self.li  = []        super (mydict,self). __init__ ()      def __setitem__ (Self, key, value):         Self.li.append (Key)         super (mydict, self). __setitem__ (Key,  value)    &nbsP;def __str__ (self):        t = []         # ml = super (mydict,self). Get ()          for k in self.li:             v =self.get (k)              T.append ("'%s ':%s"  % (k,v))         return  "{"  +  "," . Join (t)  +  "}" D = mydict () # d2 = mydict () d["K1"] = 123d["K2"]  = 456d["K3"] = 45611print (d)

Python3 Learning, extending the functionality of the parent class method

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.