A detailed description of the creation and use of classes in Python

Source: Internet
Author: User
Tags mul
This article mainly introduces the creation and use of Python implementation class, combined with the simple calculator function example to analyze the Python class definition and use method, the need for friends can refer to the following

This article describes how to create and use a Python implementation class. Share to everyone for your reference, as follows:


#coding =utf8# in order for the division to always return the true quotient, regardless of whether the operand is shaped or floating-point. The From __future__ import Pision "class is the core of object-oriented programming, and it acts as a container for related data and logic. Define class Syntax: Class ClassName (Base_class[es]): "Optional documentation string" Static_member_declarations METHOD_DECLA Rations----------------------------------------------------------------define classes using the Class keyword. You can provide a selectable parent class or base class. If there is no suitable base class, object is used as the base class by default. The class line is followed by an optional document string, a static member definition, and a method definition.  ' Class Calculatorclass (object): ' ' first Class:calculatorclass ' #定义一个静态变量来保存当前版本号 version=1.0 #设置输入参数并给参数赋值 def __init__ (self,one=10,two=20): Self.first=one self.second=two "Addition" def Add (self): return self.first+self.      Second "Subtract, take positive" def sub (self): if Self.first > Self.second:return (self.first-self.second) Else: Return (Self.second-self.first) "' Multiplication ' ' Def mul (self): return Self.first * self.second" "Division" Def P (self ): If Self.second!=0:return self.first/self.second else:pass "modulo" def mod (self): if self.sec Ond!=0:return self.first%self.second Else:pass "" The class above creates a static variable version, and the use case holds the release information. __init__ () is a special method that is executed automatically when a class instance is created. This method can be used as a build function, but it does not create an instance. It is just the first method that executes after the object is created. Its purpose is to perform some of the necessary initialization work for that object. "' Create a Computer instance ' ' ' Cal=calculatorclass (5,2) '" "by creating an instance, calling the methods and properties of the class" print "The current version:", Cal.versionprint "----------------------------------" print "The number add:", Cal.add () print "The number sub:", Cal.sub () print "The number mul:", Cal.mul () print "The number p:", CAL.P () print "The number mod:", Cal.mod ()

The results of the operation are as follows:

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.