Usage of class definition such as basic python tutorials

Source: Internet
Author: User
Class in Python is an abstract concept, which is more abstract than a function. this is the core concept of Python. it faces the object programming method (OOP). Others include: java, C ++, and so on are all objects in the object-oriented programming language (oop). It is a very important knowledge point, we can simply regard it as a collection of data and methods used to access and operate the data. After learning functions, we know why to replace functions with classes if code is reused?
Class has such advantages

1) class objects are polymorphism: multiple forms, which means that we can use the same operation method for different class objects without the need to write additional code.
2) encapsulation of classes: after encapsulation, you can directly call class objects to operate some internal class methods without allowing users to see the details of code work.
3) inheritance of classes: classes can be directly used to inherit their methods from other classes or meta classes.

Definition class syntax

The code is as follows:


>>> Class Iplaypython:
>>> Def fname (self, name ):
>>> Self. name = name

Look at the first line. The syntax is followed by the class name. Don't forget the "colon" to define a class.
Tip: the class name, first letter, has a non-text rule, preferably in upper case, so you need to identify and distinguish each class in the code.
The second line starts with a class method. as you can see, it is very similar to a function, but unlike a common function, it has a "self" parameter, it is used to reference the object itself.

Source: http://www.iplaypython.com/jichu/class.html

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.