Python handy notes standard type built-in function

Source: Internet
Author: User
Python provides some built-in functions for basic object types: CMP (), repr (), str (), type (), and (")" operator equivalent to REPR ()

(1) Type ()

The use of type is as follows:

Type (object)

Accepts an object as a parameter and returns its type. His return value is a type object.

>>>type (' R ')

>>>type (3)

>>>type (Type (5))

(2) CMP ()

The built-in function cmp () is used to compare two objects obj1 and obj2. If OBJ1 is greater than obj2, a positive integer is returned, or a negative integer is returned if it is less than 0 if it is equal.

>>>a,b=4,12
>>>CMP (A, B)
-1
>>>b=4
>>>CMP (A, B)
0
>>>a,b= ' xyz ', ' abc '
>>>CMP (A, B)
23

(3) Str (), repr ()

It is convenient to get information such as the object's content, type, numeric properties, and so on, in string form. The STR () function gets a good string readability, and repr () gets a string that can often be used to regain the object.

>>>STR (1)
' 1 '
>>>str (2E4)
' 20000.0 '
>>>REPR ([0,1,2,3])
' [0,1,2,3] '

Let's take some time to tell you about the built-in functions in Python alone.

In Python, enter the following command to see the python's built-in function, dir:

>>> dir (__builtins__)

1, str () and repr () and the ' operator

The built-in function str () and the repr () or the inverse quote operator ("') can be conveniently used as a string to get the object's
Information such as content, type, numeric attributes, and so on. The STR () function gets a good string readability, while the repr () function gets the character
The string can usually be used to regain the object, which is typically the case of obj = = Eval_r (repr (obj)).
These two functions accept an object as its argument, returning the appropriate string. In the example below, we will randomly take
Some Python objects to view their string representations.

>>> Str (4.53-2J)
' (4.53-2j) '
>>>
>>> Str (1)
' 1 '
>>>
>>> Str (2E10)
' 20000000000.0 '
>>>
>>> Str ([0, 5, 9, 9])
' [0, 5, 9, 9] '
>>>
>>> repr ([0, 5, 9, 9])
' [0, 5, 9, 9] '
>>>
>>> ' [0, 5, 9, 9] '
' [0, 5, 9, 9] '

Although Str (), repr () and ' operations are very similar in terms of their properties and functions, the fact that repr () and ' are doing exactly the same thing, they return an "official" string representation of an object, which means that in most cases
The object can be re-obtained by the evaluation operation (using the Eval_r () built-in function), but Str () is different. STR () is committed to generating an object's readability of a string representation, and its return results are usually not available for eval_r () evaluation, but
Used for print statement output. Again, not all strings returned by REPR () are able to get the original object using the Val_r () built-in function:

>>> eval_r (' type ') ')
File " ", line 1
Eval_r (' type (type)) ')
^
Syntaxerror:invalid syntax

This means that the repr () output is friendly to Python, and the output of STR () is more friendly to people. Even so,
In many cases, the output of the three is still exactly the same.

Core NOTE: Why do we have repr () and need '?

In the course of Python learning, you occasionally encounter an operator and a function that does the same thing. This is because some occasions functions are more suitable for use than operators. For example, a function is easier to use than an operator when working with an executable such as a function, or when different functions are called from different data items. Another example is the Double star (* *) exponentiation and the POW () built-in functions, x * * y and pow (x, y) are all performed X-squares.

Note: In fact, the Python community is currently discouraged from using the ' operator '.

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