Python Learning Notes

Source: Internet
Author: User
Tags access properties stack trace

Python Object

Python uses the object model to store data. Constructing a value of any type is an object. Although Python is often used as an "object-oriented programming language," it is entirely possible to write useful scripts without using classes and instances. However, Python's object syntax and architecture encourage us to use these features, so let's look at Python objects carefully.

All Python objects have three properties: identity, type, and value

Identity: Each object has a unique identity, 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. This value is rarely used in general, so you don't have to care what it is.

Type: The type of an object determines what type of value the object can hold, what operations can be done, and what rules to follow. You can use the built-in function type () to view the type of the Python object. Because types are also objects in Python, type () returns an object instead of a simple string.

Value: The data item represented by the object.

The above three attributes are assigned when the object is created, except for the value, and the other two properties are sick a read-only. For modern types and classes, the type of the object can also be changed, but this is not recommended.

If an object supports an update operation, its value can be changed, otherwise his value is read-only. Whether the value of the object can change the changes that are called Objects (there is time to write this). These attributes persist as long as an object has not been destroyed.

Python has a range of built-in data types, and you can create custom types if necessary to meet your needs. Most typically use standard types, which are implemented by creating and instantiating classes for specific data stores. In addition, some Python objects have properties, values, or associated executable code, such as methods. Python uses the period notation to access properties. Attributes include the name of the corresponding object, and so on. The most commonly used properties are functions and methods, but some Python types also have data properties. Objects that contain data attributes are: Classes, class instances, modules, complex numbers, and files.

Standard type: Integer, Boolean, long, float, plural, string, list, tuple, dictionary

Other built-in types: type, null object (None), file, collection, function, module, class

Internal type: Code, frame, trace record, slice, omit, Xrange

Here we discuss type and none of the two types of use, introduce the internal type, others have time to write.

Type object:

Although it seems that the type itself is a bit special, it is important to say here that a series of intrinsic behaviors and characteristics of an object (such as which operations are supported and what methods are available) 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.

The following is a formal introduction to the built-in function type (). You can get type information for a particular object by calling the type function.

Print (Type (42))

<class ' int ' >

Take a closer look at the return value of the type function above, we get a simple output of <class ' int ' >. It should be realized here that it is not a simple one to tell you that 42 is a string of type int. Seeing that <class ' int ' > is actually a type object, it happens that it outputs a string to tell you that it is an int type object.

So what is the type of the type object? Experimental experiment:

Print (Type (type (42)))

<class ' type ' >

Yes, the type of all types of objects is ' type ', which is also the default meta-class for the root and Python standard classes of all Python types.

In a word, a class is a type, and an instance is an object of the corresponding type.

Null object in Python-none

Python has a special type, called a null object or Nonetype, and it has only one value, which is none. It does not support any operations and does not have any built-in methods. The value of the Void,none type closest to the C language is very similar to the null value of C, none has no useful property, and its Boolean value is always False (all standard objects are available for Boolean testing, and objects of the same type can be compared in size.) Each object inherently has a Boolean value, an empty object, a numeric object with a value of 0, or a Boolean value of the None object is False)

Internal type, the general programmer does not deal with these objects, here is a brief introduction:

Code object: The code object is a compiled Python source code fragment, which is an executable object. The code object can be obtained by calling the built-in function compile (). Code objects can be executed by the EXEC command or by an eval () built-in function. The code object itself does not include any execution environment information, which is the core of the user-defined function and dynamically obtains the context when executed (in fact, the code object is a property of the function), and a function has some properties other than the code object properties, including the function name, document string, default argument, and the global namespace.

Frame object: The Frame object represents the execution stack frame of the python. The Frame object contains all the information that the Python interpreter needs to know at run time. Its properties include links to the previous frame, code objects being executed, local and global namespace dictionaries, and current directives. Each time a function call produces a new frame, each frame object creates a C-stack frame accordingly. One place to use the frame object is to track the Record object.

Trace Record object: Python throws an exception when the code goes wrong. If the exception is not captured and processed, the interpreter exits the run and displays a diagnostic message. When an exception occurs, a trace record object that contains the stack trace information for the exception is created. If an exception has its own handler, the handler can access the trace Record object.

Slice object: A Slice object is created when the slice syntax is extended using Python. Extended slice syntax allows for different index tile operations, including step slices, multidimensional slices, and omitted slices. The syntax for a multidimensional slice is sequence[starts1:end1,start2:end2], or use the ellipsis, Sequence[...,start1:end1]. The slice object can also be generated by the built-in function slice (). The step slice allows stepping slices with the 3rd slice element, whose syntax is sequence[start index: End index: Step value]. Here are some examples of step slices:

Foostr = ' ABCDE '
Print (Foostr[::-1])
Print (Foostr[::-2])
Foolist = [123, ' Xba ', 342.23, ' abc ']
Print (Foolist[::-1])

Results:

Edcba
Eca
[' abc ', 342.23, ' XBA ', 123]

Omit object:

The ellipsis object is used in the extended slice syntax to mark the function. This object represents an ellipsis in the slice syntax. Similar to the null object none, the ellipsis object has a unique name of ellipsis, and its Boolean value is always true.

Xrange Object

Calling the built-in function xrange () generates a xrange () object, and Xrange () is the sibling version of the built-in function range () for large data collection occasions where memory usage or range () cannot be completed.

   Learn to update in ....

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.