Python object (bottom)

Source: Internet
Author: User

The previous article describes some of the basic concepts of Python objects, which are followed by a look at some of the Python object-related content.

Comparison of Python objects

The Python object has three elements: identity, type, and value, so let's look at the comparison between the objects from these three angles respectively.

Object Identity Comparison

The comparison of object identity is actually the memory address of the comparison object, that is, the result comparison of the built-in function ID (). Can be used to determine whether different variables point to the same address.

See examples directly:

The output from the example can be obtained, F1 and F2 point to different objects (addresses), but I1 and I2 point to the same object (address).

This difference occurs because Python caches both integer and string objects, so it does not produce new objects, but instead points to cached objects. Different versions of Python handle the cache differently, so the same example may produce different results.

For object identity comparisons, you can also use the "is" keyword in python:

 is Obj2 # equivalent ID (obj1) = = is not obj2#  equivalent ID (obj1)! = ID (obj2)
Object Type Comparison

By using the type () built-in function, you can get the type of an object and then you can compare the type.

Look at the code:

The example uses three ways to compare object types:

    • Comparing type (obj) directly to a type
    • Compare the identity of type (obj) with the identity of the types
    • Use the built-in isinstance () function to determine whether an object is instantiated by a particular type.
      • Its first argument is an object, the second is a list of type names or type names
      • Its return value is a Boolean type

A simple look at the difference between the first two methods is to directly compare the values of two types of objects, whereas the second compares the identities of two types of objects, the principle of which is that if the identities of two types of objects are different, the values of the two Type objects must be different.

Object value Comparison

For Python objects, you can use comparison operators (>,<,==, and so on) to compare object values directly. With respect to Python's built-in objects, there is a set of comparison rules, such as the comparison of two list objects according to the list comparison rules.

Here we'll look at comparisons between objects of a custom type.

Comparison of object values for custom types

In Python, all types have a "magic method" for comparison, and for custom types, we can implement these methods to define the comparison behavior of objects of a custom type:

    • __cmp__ (self, Other): __cmp__ are the most basic of the comparison magic methods. __CMP__ should return a negative integer if self < Other, zero if self = = other, and positive if self > other.
    • __eq__ (self, other) defines behavior for the equality operator, = =.
    • __ne__ (self, other) defines behavior for the inequality operator,! =.
    • __lt__ (self, other) defines behavior for the Less-than operator, <.
    • __gt__ (self, other) defines behavior for the Greater-than operator,.
    • __le__ (self, other) defines behavior for the less-than-or-equal-to operator, <=.
    • __ge__ (self, other) defines behavior for the greater-than-or-equal-to operator, >=.

Looking at an example, we define a "statement" type and implement some comparison methods so that we can compare the object values directly with the comparison operator for the modified type.

It is important to note that for objects of a custom type, if there is no "magic method" corresponding to the comparison operator, then the object identity (ID ()) will be used by default for comparison.

Difference between "is" and "= ="

Notice the difference between "is" and "= =":

    • ' is ' is used to compare the identity of the object, that is, to compare the memory address of the object (whether the variable points to the same object)
    • "= =" is used to compare the object's content (value) for equality

Look at the following code:

For variables C and D, because the values exceed the Python cache range for integers, all d is a new object generated by Python. Therefore, the values of C and D are the same, but the identities are different.

Mutable objects and immutable objects

In Python, everything is an object, there is no so-called pass value in Python, and all that is passed is a reference to the object (which can also be considered a pass-through).

As we learned in the previous article, you can divide Python objects into: Type Object (Type objects) and Non-type object (non-type objects), depending on the type of Python object.

Similarly, depending on the variability of the objects, Python objects can also be divided into two categories: variable (mutable) objects and immutable (immutable) objects.

    • Immutable (immutable) object: The content of an object is not mutable, and when an attempt is made to change the content of an object, a new object is created, i.e. the identity (ID ()) of the object changes
      • Example: Number, string,tuple
    • Variable (mutable) object: The object's content is variable, and the object's identity (ID ()) does not change when the object content is changed
      • Example: List, Dict, set

Look at a simple code:

A tuple is "mutable."

Although the tuple object itself is immutable, this does not mean that tuples contain mutable objects that are immutable.

Look at the following example:

Now we analyze this code, TPL this tuple of the fourth element is more special, is a list; So, the fourth element in the TPL is the address of the list (ID (), reference).

The immutability of the tuple determines the content of the fourth element of the TPL, that is, the address of the list cannot be changed, but the content that this address 37865712 points to can be changed.

Summarize

This article describes a comparison of Python objects, including the identity of the object, the type, and the comparison of the values. For objects of a custom type, if a comparison operation is required, the "magic method" associated with the custom comparison operation can be used.

In addition, the paper briefly introduces the variability of Python objects, and compares the differences between variable (mutable) objects and immutable (immutable) objects.

Python object (bottom)

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.