Python class Base

Source: Internet
Author: User

          44, class:                     General forms of class definitions:                           1, simple form: instanced objects are not self- Data attributes that are unique to your own. >>> class Fistclass ():...     data1 = ' Hello World ' # # # ==> This is the Data property or class member property of the class. ...     def printdata (self): ###==> This is the method of the class, and there is at least one parameter self!!! ...             print (' nihao%s '%self.data1) ...>>> class1 = Fistclass () ##==& GT, Class instantiation >>> class1.class1.__class__ (         class1.__format__ (      & nbsp class1.__le__ (            class1.__reduce_ex__ (     class1.__weakref__ class1.__delattr__ (       class1.__ge__ (            class1.__lt__ (& nbsp           class1.__repr__ (          class1.data1class1.__dict__           class1.__getattribute__ (  class1.__module__          class1.__setattr__ (       class1.printdata (class1.__dir__ (            class1.__gt__ (            class1.__ne__ (  &nbs P         class1.__sizeof__ (       class1.__doc__            class1.__hash__ (          class1.__new__ (          &NBSP;CLASS1). __str__ (           class1.__eq__             class1.__init__ (           class1.__reduce__ (        class1.__subclasshook__ ( >> > class1.data1 # #类数据属性调用 ' Hello World ' >>> class1.printdata () # #类的方法的调用nihaohello world    & NBSP;&NBSP;&NBSP;&NBSP;2, defining data properties unique to an object:  &NBsp       >>> class Seclass ():...     data1 = ' Hello Seclass ' ...    &NB Sp;def SetData (self,x):...            &NBSP;SELF.STR1 = x...     def printdat A (self):...             print (self.str1) ...>>> ins1 = Seclass () >>> Ins1.ins1.__class__ (         ins1.__format__ (        ins1.__le__ (  & nbsp         ins1.__reduce_ex__ (     ins1.__weakref__ins1.__delattr__ (       ins1.__ge__ (            ins1.__lt__ (            I ns1.__repr__ (          ins1.data1ins1.__dict__           ins1.__ getattribute__ (  ins1.__module__         ins1.__setattr__ (        Ins1.printdata (ins1.__dir__ (  &NBSp        ins1.__gt__ (            ins1.__ne__ (        & nbsp   ins1.__sizeof__ (        Ins1.setdata (ins1.__doc__            INS 1.__hash__ (          ins1.__new__            ins1.__str__ (  & nbsp        ins1.__eq__ (            ins1.__init__ (          ins1.__reduce__ (        ins1.__subclasshook__ ( >>> ins1.data1 ' Hello Seclass '           >>> ins1.setdata (' abc ') >>> ins1.printdata () abc>>> Ins2 = Seclass () >>> ins2.data1 ' Hello seclass ' >>> ins2.setdata (' xy ') >>> ins2.printdata () xy    3, __init__ () method:      When you create an instance, Python automatically calls the __init__ method in the class to implicitly provide properties for the instance.     &NBSP;__INIT__ is called a constructor.       such asThe __init__ method is not defined in the fruit class, the first instance is simply creating a simple namespace.      >>> class Thirdclass ():...     data3 = ' Hello Thirdclass ' ...    &NB Sp;def __init__ (self,who):...             self.name = who...>>> Ins3 = THIRDCL (' Lucy ') >>> ins3.ins3.__class__ (         ins3.__format__ (     )   ins3.__le__ (            ins3.__reduce_ex__ (     ins3.__weakref__ ins3.__delattr__ (       ins3.__ge__ (            ins3.__lt__ (  & nbsp         ins3.__repr__ (          ins3.data3ins3.__dict__      & nbsp    ins3.__getattribute__ (  ins3.__module__         ins3.__setattr__ (       ins3.nameins3.__dir__ (           ins3.__gt__ (      & nbsp;     ins3.__ne__ (            ins3.__sizeof__ (       ins3.__doc) __            ins3.__hash__ (          ins3.__new__ (    &N Bsp      ins3.__str__            ins3.__eq__ (          & nbsp ins3.__init__ (          ins3.__reduce__ (        ins3.__subclasshook__ (  >>> ins3.data3 ' Hello thirdclass ' >>> ins3.name ' Lucy '    4, __dict__:     >>> ins3.__dict__ property of the instance, {' Name ': ' Lucy '}>>> thirdclass.__dict__ class. Mappingproxy ({' data3 ': ' Hello Thirdclass ', ' __init__ ': <function thirdclass.__init__ at 0x7f8e0a440ea0>, ' __dict __ ': <attribute ' __dict__ ' of ' thirdclass ' objects>, ' __module__ ': ' __main__ ', ' __doc__ ': None, ' __weakref__ ': < Attribute ' __weakref__ ' of ' Thirdclass ' objects>})   &NBSP;5, variables available in class:     >>> Class C1 ():...     var1 = ' Hello C1 ' ###==> class static variables!!! ...     def __init__ (self,who):...             self.insdata = who ###==& A gt; an instance variable. ...             self.name = ' 123 ' ###==> Local variables: This method only. ...>>> ins1 = C1 () >>> ins1.ins1.__class__ (         ins1.__format__ (        ins1.__le__ (            ins1.__reduce_ex__ (     ins1.__ weakref__ins1.__delattr__ (       ins1.__ge__ (            ins1.__lt__ (             ins1.__repr__ (          INS1.INSDATAINS1.__DICT__&NBSP ;          ins1.__getattribute__ (  ins1.__module__         ins1.__ setattr__ (       ins1.nameins1.__dir__ (&NBsp          ins1.__gt__ (            ins1.__ne__ (      & nbsp     ins1.__sizeof__ (        ins1.var1ins1.__doc__            I Ns1.__hash__ (          ins1.__new__ (          &NBSP;INS1.__STR__ (            ins1.__eq__ (            ins1.__init__ (   )       ins1.__reduce__ (        ins1.__subclasshook__ ( >>> ins1.name ') 123 ' >>> ins1.var1 ' Hello C1 ' >>> ins1.insdata12   6, class inheritance: Python allows multiple inheritance, property search methods: From left to right, bottom to top.      >>> class PClass (object):...     gender = ' Male ' ...    &NBSP;DEF __ init__ (self,who):...             self.name = who...>>> class Cclass (PClass): ...     def DisplayinfO (self):...             print (self.gender,self.name) ...>>> ins1 = Cclass (' Lucy ') >>> ins1.ins1.__class__ (         ins1.__format__ (        ins1.__le__ (            ins1.__reduce_ex__ (     ins1.__weakref__ins1.__ delattr__ (       ins1.__ge__ (            ins1.__lt__ (    &NBS P       ins1.__repr__ (          Ins1.displayinfo (ins1.__dict__           ins1.__getattribute__ (  ins1.__module__        &NBSP;INS1.__SETATTR__ (        ins1.genderins1.__dir__ (           ins1.__gt__ (    & nbsp       ins1.__ne__ (            ins1.__sizeof__ (        INS 1.nameins1.__doc__      &NBSp     ins1.__hash__           ins1.__new__ (          &NBSP;INS1. __str__ (           ins1.__eq__             ins1.__init__ (&nbs P         ins1.__reduce__ (        ins1.__subclasshook__ ( >>> Ins1.gender ' Male ' >>> ins1.name ' Lucy ' >>> ins1.displayinfo () male Lucy

Python Class class basics

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.