Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Python objects     - How to use the ftp tool to transfer files between Windows and Linux (Linux)

- Java string intern constant pool resolution Introduction (Programming)

- Difference Docker mirror and containers (Server)

- How to avoid two Chrome icon appears in ELementary OS Freya (Linux)

- Eclipse, Tomcat configuration JNDI connection Oracle data source example (Server)

- Basic data types JavaScript type system and the type of packaging (Programming)

- How to install Hadoop on CentOS7 (Server)

- Explore Android SQLite3 multithreading (Programming)

- Ordinary users how the Linux system shutdown (Linux)

- Oracle 11g RAC automatically play GI PSU patch ( (Database)

- S5PV210 development board for embedded development environment to build under Ubuntu (Linux)

- Eclipse improve the efficiency of good habits (Programming)

- MySQL Tutorial: Philosophical Reflections on the unauthenticated user (Database)

- Use the Find command to help you find those files that need to be cleaned (Linux)

- Install VMware Tools in Debian (Linux)

- Let Mac OS X dedicated high-speed mobile hard disk can also be read in Linux (Linux)

- Create several practical points of high security PHP site (Linux)

- Install Oracle database error process of [INS-35172] (Database)

- C language to view various types of data size (Programming)

- TWiki LDAP error appears the problem is solved (Linux)

  Python objects
  Add Date : 2017-08-31      
  Python is an object-oriented language, everything is an object in Python, functions are objects, but also the type of the object.

Read on to find Python objects related content.

Python Basic concepts

Python object has three basic elements:

Identity: The only sign of identity of the object, the object is the memory address (available builtin function id () to obtain)
Type: The type of the object determines the object can save what type of value, what kind of operation can be carried out (available built-in function type () to obtain)
Value: object represents data
Let's look at the following code:

num = 7

print id (num)
print type (num)
print num
print dir (num)


def isEven (num):
    return (num% 2 and [False] or [True]) [0]
print id (isEven)
print type (isEven)
print isEven
print dir (isEven)
 You can see by the code, we define a type int value objects, and a function object, through the built-in dir () function, we can see the object supports properties / methods:

Python origin objects

There are two basic objects in Python, < type 'object'> and < type 'type'>, these two objects is the origin of all objects.

See the following code < type 'object'> and < type 'type'> relationship:

Codes used in "__class__" property to see the type of object, and "__bases__" property to view the object's parent class, these two properties are important, you can view the object of the present relationship with other objects.

In the Python object system, the relationship between < type 'object'> and < type 'type'> like chicken and egg relationship, we can not say to whoever (create) who both are interdependent, and together constitute the Python base object system.

< Type 'type'> of type < type 'type'> (itself), < type 'type'> parent is < type 'object'>
< Type 'object'> of type < type 'type'>, < type 'object'> no parent
Described here is not feeling a bit around, it does not matter, we continue to look at the categories below about the object.

Python object classification

In Python, all objects can be divided into two categories: Type Object (type of object, yes, but also the type of an object) and Non-type Object (untyped object).

Here we have to list, mylist example for analysis:

First of all, the built-in list is a Python container types, that list is a type of object
According to "__class__" attribute can be seen, this type of list type object is < type 'type'>
According to "__bases__" attribute can be seen that the parent object is the list of this type < type 'object'>
Mylist by the "__class__" attribute (< type 'list'>) can be seen, mylist is an object of type object list generated (Non-type Object (untyped Object))
Through the above analysis we can verify the principles of Python object class:

If an object is a < type 'type'> instance, that it is Type Object (type of object), otherwise it is Non-type Object (untyped object)
Type Object and Non-type Object

Through the above analysis can be seen, Type Object (Object type) include:

< Type 'type'>
< Type 'object'>
By < type 'type'> generated object (type of object)
Type Object (type of object) has two important features:

Can be subclassed (subclassed)
Can be instantiated (instantiated)
Back to the top of the object graph, Type Object (type of object) the object in the first two squares is stored.

Then the rest of the object is a Non-type Object (untyped object), return to the object graph, Non-type Object (untyped object) is stored in the third grid of objects. Solid line can not appear in the third grid, since the object here is not to subclass (inheritance); similarly, the dotted arrow can not appear in the box in the third, because the object here can not be instantiated.

Look < type 'type'>

< Type 'type'> itself is a type of object; at the same time, < type 'type'> all types of objects (including < type 'type'> itself) type, that is, for all types of object type () or get "__class__" attribute will be < type 'type'>.

We can import the module types, then () built-in function dir to see through all of the built-in types of objects, objects of these types are < type 'type'>:

Look < type 'object'>

Similarly, < type 'object'> is a type of object (because the type (object) is < type 'type'>); at the same time, < type 'object'> all types of objects (remove < type 'object'> itself) father.

type () and __class__

It should mention that, type (), and the built-in function "__class__" this property, these two methods can get the type of object, the results are generally two ways to obtain the same.

However, in Python Classic (classic class), type () and "__class__" the result is different (not presented here classic class and the new-style class):

For the new-style class, type () and "__class__" The result is the same.

to sum up

This article describes the Python object some basic points:

Three elements of Python objects
Python object two basic objects < type 'type'> and < type 'object'>
Classification Python object: Type Object (type of object) and Non-type Object (untyped object)
These points should be able to have a basic understanding of Python objects. When the object needs to be analyzed, might try to use the object graph paper, by this diagram can be more intuitive to see the relationship between objects.

Previous article introduces the basic concepts of Python objects, then take a look at some of the content of this Python objects.

Compare Python object

Python object has three elements: identity, type and value, so we are starting to see the angle from the three comparison between objects.

Object identity comparison

Result of the comparison of the identity of the object, in fact, the comparison target memory address that the built-in function id () of. It can be used to determine whether the different variables pointing to the same address.

Examples can be obtained through the output, f1 and f2 point to different objects (address); however, i1 and i2 point to the same object (address).

This difference arises because Python integers and strings objects will be cached, so no new object, but point to the cached object. Different versions of the Python processing buffer is not the same, so the same example may produce different results.

For the object identity comparison may also be used in Python "is" keyword:

obj1 is obj2
# Equivalent
id (obj1) == id (obj2)

obj1 is not obj2
# Equivalent
id (obj1)! = id (obj2)

Object type comparison

() Built-in function by type, you can get the type of an object, and then you can compare the type.

Example uses three methods to compare object types:

Directly to the type (obj) is compared with the type
The type (obj) identity with the identity of the type of comparison
Through built-in isinstance () function to determine whether an object is an instance of a particular type has obtained
A list of its first parameter is an object, the second a type name or type name
Its return value is a Boolean
A brief look at the difference between the first two ways, the first way is to directly type the value of the two objects are compared; while the second is to compare the identity of two types of objects, the principle of this approach is that if two the identity of a different type of object, then the value of two different type of object recognition.

Object comparison value

For Python objects, you can directly use the comparison operators (>, < , ==, etc.) to compare object values. About Python's built-in objects, compare a set of rules, such as comparing two objects list compare list is in accordance with rules.

Here we look at the main custom type for comparison between.

Self-defined type object value comparisons

In Python, all types have a set for comparison "magic method", for custom type, we can define a custom object definition type by comparing the behavior of these methods:

__cmp __ (self, other): __cmp__ is 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,> =.

Look at an example, we define a "statement" type, and to achieve some of the more methods for this type of object is changed, we can directly compare the value of the object by comparing the operator.

One thing to note is that with regard to the type of object to be customized, if not implemented comparison operator corresponding "magic method", it will default to the object identity (id ()) were compared.

The difference between "is" and "==" in

It should be noted about the difference between "is" and "==" between:

"Is" used to compare object identity are equal, that is, the comparison target memory address is the same (variable points to the same object)
"==" To compare the contents of the object (value) is equal to

For variables c and d, since the value of the integer exceeds the Python buffer range, all d would be a new generation of Python objects. Therefore, the values of c and d are the same, but the identity is different.

Variable objects and immutable object

In Python, everything is an object, not the so-called traditional values exist Python, everything is an object passing reference (to be considered a transfer site).

Learned in the previous article, depending on the type of Python objects, Python objects can be divided into: Type Object (type of object) and Non-type Object (untyped object).

Similarly, according to the object variability, Python object may be divided into two categories: variable (mutable) objects and immutable (immutable) objects.

Immutable (immutable) objects: content objects immutable, when you try to change the contents of the object, will create a new object; that is to say the identity of the object (id ()) will change
For example: number, string, tuple
Variable (mutable) Object: variable content of the object, when changing the contents of the object when the object's identity (id ()) does not change
For example: list, dict, set

tuple is "variable"

Although tuples are immutable objects themselves, but this does not mean that the object variable tuple variable is not included.

Now we analyze the code according to the diagram, the fourth element tpl tuple rather special, is a list; therefore, in tpl the fourth element of this list is stored in the address (identity id (), references) .

Immutability tuple determines the fourth element tpl corresponding content, that list the address can not be changed, but the contents of the address pointed to 37,865,712 can be changed.

to sum up

This introduction of the relatively Python objects, including the object's identity, type, and compare values. For self-defined types of objects, if you need to compare operation can be defined by the autocorrelation compare operation "magic method."

In addition, this paper introduces the Python object variability, compared to the variable (mutable) different objects and immutable (immutable) objects.
- Java, boolean operators & =, | = ^ = use (Programming)
- To create a full command line Android Build System (Linux)
- Export error: ORA-04063: package body dmsys dbms dm model exp has error (Database)
- Redis 3.0.3 Cluster Setup (Database)
- Spring JDBC Comments (Programming)
- How to modify the Sublime in Tab four spaces (Linux)
- To achieve a two-way static NAT stateless available modules on Linux (Linux)
- The method of CentOS-7.0. The installation and configuration of Tomcat-7 (Server)
- Ubuntu 14.04, 13.10 install OpenCV 2.4.9 (Linux)
- How to make Linux a non-root user uses less than 1024 ports (Linux)
- Virtualbox virtual machine can not copy CentOS Network (Linux)
- Python 3 for instructions encoded string conversion (Programming)
- VMWare virtual machine without rebooting way to add virtual disk (Linux)
- Service manager OpenBSD on rccl (Server)
- History of the most complex validation e-mail address regular expression (Linux)
- Java object serialization (Programming)
- Linux installation and error under codeblocks exclude [Ubuntu 10.04] (Linux)
- Java Access Control (Programming)
- Story timestamp and time zones: daily programmer (Programming)
- How to fix Not Enough Free Disk Space On / boot on Ubuntu (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.