Chapter Fourth Python objects

Source: Internet
Author: User
Tags access properties scalar

4.1 Python Object

  All Python has three features: identity, type, and value.
Identity:
Each object has a unique identity that identifies itself, and any object's identity can be obtained using the built-in function ID (). This value can be considered to be the memory address of the object. You rarely use this value, and you don't have to be too concerned about what it is.

Type
The type of object determines what type of value the object can hold, what actions can be taken, and what rules to follow. You can use the built-in function type () to view the type of the Python object. Because the type is also an object in Python
(Remember when we mentioned that Python is an object-oriented sentence?), so type () returns an object instead of a simple string.

Value
object represents the data

4.1.1 Object Properties

Some Python objects have properties, values, or associated executable code, such as method. Python uses the dot (.) notation to access properties. Attributes include the name of the object, and so on, as described in Chapter 2.14 's remarks. Most commonly used
Properties are functions and methods, but some Python types also have data properties. Objects that contain data attributes include, but are not limited to: classes, class instances, modules, complex numbers, and files.

4.2 Standard Type
?? Number (divided into sub-types, three of which are integer)
?? Integral type
?? Boolean type
?? Long integer type
?? Floating point Type
?? Plural type
?? String

?? List
?? Meta-group
?? Dictionary
In this book, we refer to standard types as "basic data types" because they are basic data types built into Python, and we'll cover them in chapters 5th, 6 and 7.

4.3.1 type object and type Object
In this chapter we will discuss all the types of Python, although it seems that the type itself is a little bit special, we should mention it here. You must remember that a series of intrinsic behaviors and characteristics of an object (such as which operations are supported,
Which methods) must be defined beforehand. From this point of view, the type is the best place to save this information. The information required to describe a type cannot be done with a string, so the type cannot be a simple string, and the information
Cannot and should not be saved with the data, so we define the type as an object. Let's take a formal introduction to the built-in function type (). You can get the type of a particular object by calling the type () function
Information:
>>> Type (42)
<type ' int ' >
Let's take a closer look at this example, and notice the interesting return value of the type function. We get a concise output result <type ' int ' >. But you should realize that it's not a simple thing to tell you. 42 is an integer.
The string. You see that the <type ' int ' > is actually a type object, and it happens to output a string to tell you that it is an int type object.

Now you should ask yourself, what is the type of object? Here, let's experiment:
>>> type (type (42))
<type ' type ' >
Yes, all types of object types are type, and it is the default meta-Class (Metaclass) for all Python-type roots and all Python standard classes.

Core NOTES: Boolean values
All standard objects are available for Boolean testing and can be compared between objects of the same type. Each object is born with a cloth
True or False value. A null object, any number with a value of zero, or a Boolean value of None for the null object is false.
The Boolean value of the following object is false.
?? None
?? False (Boolean type)
?? All values are of zero:

?? 0 (integral type)
?? (floating point type)
?? 0L (Long integer type)
?? 0.0+0.0j (plural)
?? "" (empty string)
?? [] (Empty list)
?? () (Empty tuple)
?? {} (Empty dictionary)
The Boolean value of an object that is not a value other than the one listed above is True, such as Non-empty, Non-zero, and so on. User-created class instance if nonzero (__nonzero__ ()) or length (__len__ ()) is defined and the value is 0, that
Their boolean value is False.

4.5 Standard type operators
Comparison of 4.5.1 object values
Comparison operators are used to determine whether objects of the same type are equal, all built-in types support comparison operations, and the comparison operation returns a Boolean value of True or False. If you are using a version earlier than Python2.3, because these versions are not yet
Boolean type, you will see that the comparison result is an integer value of 1 (which represents true) or 0 (on behalf of false). Note that the actual comparison operation varies by type. In other words, numeric types are compared by the size and symbol of numeric values, and strings are compared by character sequence values, and so on.

>>> 2 = = 2
True
>>> 2.46 <= 8.33
True
>>> 5+4j >= 2-3j
True
>>> ' abc ' = = ' xyz '
False
>>> ' abc ' > ' xyz '
False

>>> ' abc ' < ' XYZ '
True
>>> [3, ' abc '] = = [' abc ', 3]
False
>>> [3, ' abc '] = = [3, ' ABC ']
True
Unlike many other languages, multiple comparison operations can be performed on the same line, and the order of evaluation is left to right.
>>> 3 < 4 < 7 # same as (3 < 4) and (4 < 7)
True
>>> 4 > 3 = 3 # Same as (4 > 3) and (3 = = 3)
True
>>> 4 < 3 < 5! = 2 < 7
False

4.6 Standard type built-in functions
In addition to these operators, we have just seen that Python provides some built-in functions for these basic object types:
CMP (), repr (), str (), type (), and the SLR quote (') operator equivalent to the repr () function.
Table 4.4 Standard type built-in functions
function function
CMP (OBJ1, OBJ2) compares Obj1 and Obj2, and returns the integer I as a result of the comparison:
I < 0 if obj1 < obj2
I > 0 if obj1 > obj2
i = = 0 if obj1 = = Obj2
Repr (obj) or ' obj ' returns a string representation of an object
STR (obj) returns an object that fits into a well-readable string representation
Type (obj) to get the kind of an object and return the corresponding type Object

Data type Storage model update model access model L
Digital Scalar cannot change direct access
String Scalar cannot change sequential access
List Container can change sequential access
Tuple Container cannot change sequential access
Dictionary Container can change map access


Table 4.7 Classification of types with access model as standard
Access model
Category Python Type
Direct access to numbers
Sequential access to strings, lists, tuples
Map Access Dictionaries

Chapter Fourth Python objects

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.