User-Defined types in Python

Source: Internet
Author: User

In Python, object-oriented technology Python is an object-oriented programming language that naturally provides object-oriented programming methods. However, it is very difficult to define an object-oriented programming method. The key to the problem is to understand the meaning of the object. Objects have a wide range of meanings. They are the abstraction, simulation, and refinement of the real world and the concept world. Object methods are similar to functions, but there are two differences: 1. methods are defined inside the class and are part of the class. The relationship between them is obvious; 2-The call syntax is different. [python] >>> class Time :... pass...> def printTime (time ):... print str (time. hours)...> now = Time (); >>> now. hours = 10; >>>> printTime (now) 10 >>>>>> class Time :... pass...> def printTime (time ):... print str (time. hours)...> now = Time (); >>> now. hours = 10; >>> printTime (now) 10 >>> we can change the function scope by changing the indentation. Attribution: [python] >>> class Time :... def printTime (self ):... print str (self. hours )...... def increment (self, hours ):... self. hours = hours + self. hours... while self. hours> = 12 :... self. hours = self. hours-12... self. day = self. day + 1... class Time :... def printTime (self ):... print str (self. hours )...... def increment (self, hours ):... self. hours = hours + self. hours... whi Le self. hours> = 12 :... self. hours = self. hours-12... self. day = self. day + 1 ......> optional parameters we have seen some built-in functions. The number of accepted parameters is variable. Similarly, you can define variable parameter functions. The following function calculates the sum of all the numbers from the number head to the tail and the step size. [Python] >>> def total (head, tail, step ):... temp = 0 ;... while head <= tail :... temp = temp + head ;... head = head + step ;...... return temp; ...... >>> total (1,100) Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: total () takes exactly 3 arguments (2 given) >>> total (1,100, 3) 1717 >>>> def total (head, tail, step = 2 ):... temp = 0 ;... while head <= tail :... temp = temp + head ;... head = head + step ;...... return temp; ...... >>> total (1,100); 2500 >>>> total (1,100, 3); 1717 >>>>> def total (head, tail, step ):... temp = 0 ;... while head <= tail :... temp = temp + head ;... head = head + step ;...... return temp; ...... >>> total (1,100) Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: total () takes exactly 3 arguments (2 given) >>> total (1,100, 3) 1717 >>>> def total (head, tail, step = 2 ):... temp = 0 ;... while head <= tail :... temp = temp + head ;... head = head + step ;...... return temp; ...... >>> total (1,100); 2500 >>> total (1,100, 3); 1717 >>>

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.