If you are not familiar with the Python skills-the practical application of code efficiency, you can read the following articles to learn more about the practical application of the Python skills, I hope you can learn what you want after reading the following articles.
Objects are equal. What is the difference between type (a) = type (B) and type (a) is type (B? Why the latter? What is the relationship between the isinstance () function and this function?
In Example 4.1, we provided a script to demonstrate how to use the isinstance () and type () functions in the runtime environment. Then we will discuss how to use type () and how to port this example to use isinstance (). Run typechk. py and we will get the following output:
- -69 is a number of type: int
- 9999999999999999999999 is a number of type: long
- 98.6 is a number of type: float
- (-5.2+1.9j) is a number of type: complex
- xxx is not a number at all!!
For example, the type (typechk. py) function displayNumType () accepts a numeric parameter. In the Python technique, use the built-in function type () to confirm the numeric type or not a numeric type ).
- #!/usr/bin/env python
- def displayNumType(num):
- print num, 'is',
- if isinstance(num, (int, long, float, complex)):
- print 'a number of type:', type(num).__name__
- else:
- print 'not a number at all!!'
- displayNumType(-69)
- displayNumType(9999999999999999999999L)
- displayNumType(98.6)
- displayNumType(-5.2+1.9j)
- displayNumType('xxx')
The above is an introduction to the Python skills-code efficiency-related content. I hope you will gain some benefits.