Python: Class concepts and how to use them

Source: Internet
Author: User

A class is a kind of thing, or an instance, used to describe a class of things that have common characteristics. In Python, we declare the class's key word is class, the class has functions and attributes, and the property is the characteristic of this kind of thing, and the function is what it can do, also is the method or function. We still use examples to illustrate the problem.

declaring classes
  1. Declare a class using class, that is, create a class. Use the class keyword, followed by the name of the class, and then a colon 650) this.width=650; "Src=" http://hiphotos.baidu.com/exp/pic/item/ Aa59892bd40735fae92fc8979c510fb30e240885.jpg "class=" Pic-cursor-pointer "height=" 135 "width=" "style=" border:0 px;float:left;margin:0px auto 0px 54px; "alt=" aa59892bd40735fae92fc8979c510fb30e240885 "/>

  2. There are two ways to do it. , they are all equivalent. 650) this.width=650; "Src=" http://hiphotos.baidu.com/exp/pic/item/27d647ee3d6d55fbd017f0086f224f4a21a4dd88.jpg " class= "Pic-cursor-pointer" height= "width=" 257 "style=" border:0px;float:none;margin:0px auto 0px 175.5px; "alt=" 27d647ee3d6d55fbd017f0086f224f4a21a4dd88 "/>650) this.width=650;" Src= "http://hiphotos.baidu.com/exp/pic/item/ 0865b518972bd4073b3355d379899e510eb309ec.jpg "class=" Pic-cursor-pointer "height=" 111 "width=" "style=" border:0 px;float:none;margin:0px auto 0px 54px; "alt=" 0865b518972bd4073b3355d379899e510eb309ec "/>

  3. After declaring the class, we use the __INIT__ function to overload the constructor, there is a self parameter in the class, he is any class, it represents the instance itself, if we want to set a Name property for the instance, we use the following method to set. The parameters behind __init__ can be used when creating an instance. 650) this.width=650; "Src=" http://hiphotos.baidu.com/exp/pic/item/ac0acf1373f08202464d171149fbfbedaa641b85.jpg " class= "Pic-cursor-pointer" height= "width=" 364 "style=" border:0px;float:none;margin:0px auto 0px 122px; "alt=" Ac0acf1373f08202464d171149fbfbedaa641b85 "/>

  4. Of course we can arbitrarily set the __init__ method parameter number 650) this.width=650; "Src=" http://hiphotos.baidu.com/exp/pic/item/ 9f1011b30f2442a7ccd17b8bd343ad4bd0130285.jpg "class=" Pic-cursor-pointer "height=" 107 "width=" "style=" border:0 px;float:none;margin:0px auto 0px 54px; "alt=" 9f1011b30f2442a7ccd17b8bd343ad4bd0130285 "/>

  5. Normally we use the __DEL__ function to overload destructors, but if you do not write this method in the declaration of the class, it will be added by default. So, usually do not write ... 650) this.width=650; "Src=" http://hiphotos.baidu.com/exp/pic/item/d0526df082025aafb9c6f8e5f9edab64024f1aec.jpg " class= "Pic-cursor-pointer" height= "137" width= "style=" border:0px;float:none;margin:0px auto 0px 54px; "alt=" D0526DF082025AAFB9C6F8E5F9EDAB64024F1AEC "/>

  6. You can also declare a method in the class, and then after you create the instance, we call the method again. The Declaration method is as follows: 650) this.width=650; "Src=" http://hiphotos.baidu.com/exp/pic/item/ 5beeba0f4bfbfbed8badb0517af0f736aec31fec.jpg "class=" Pic-cursor-pointer "height=" "width=" "style=" border:0px ; float:none;margin:0px auto 0px 54px; "alt=" 5beeba0f4bfbfbed8badb0517af0f736aec31fec "/>


Python: Class concepts and how to use them

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