Python core programming notes Python objects, python core programming

Source: Internet
Author: User

Python core programming notes Python objects, python core programming
Chapter 2 Python objects 1. Python objects

Python uses an object model to store data. Constructing a value of any type is an object. All objects have three features:

  • Identity, which can be viewed through the built-in function id (). This value is the memory address of the object.
  • Type, which can be viewed by the built-in function type.
  • Value, which is a data item represented by an object.
>>> p = 12>>> id(p)31108092>>> type(p)<type 'int'>>>> p12>>> 
2. Standard Type

Integer integer, Long Integer, float, complex number, Boolean bool, string, list, tuple, dictionary.

3. Other built-in types
  • Type object

    The type itself is an object and its type is 'type '.

>>> type(1)<type 'int'>>>> type(type(1))<type 'type'>
  • None -- Null Object of Python

    Python has a special type called Null object or NoneType. It has only one value: None, and the Boolean value of None is False.

  • File

  • Set
  • Functions/methods
  • Module
  • Class
4. Internal type

We generally don't pay too much attention to and use the internal types. Just take a look.

  • Code object

    The Code object is a compiled Python source code snippet that can be executed. You can use the built-in function compile () to obtain code objects. Code objects can be executed by exec commands or built-in functions eval.

  • Frame

  • Tracking object

    When a program exits abnormally, a tracing record object containing stack tracking information for exceptions is created:

>>> pri   Traceback (most recent call last):    File "<stdin>", line 1, in <module>   NameError: name 'pri' is not defined
  • Slice object

    When the slice Syntax of Python is used, the slice object is created.

  • Omitted object

    It is used as a mark in the slice syntax. For example, str [: 2.

  • Xrange object

    Calling the built-in function xrange () will generate an XRange object. xrange is the sibling version of range. It is used for ultra-large datasets that need to save memory or cannot be completed by range.

5. Standard Operators
  • Object Value Comparison
  • Object Identity comparison

    This involves Python reference counting, which is summarized in the previous article. Python also provides the is and is not operators to test whether two variables point to the same object.

>>> a = 1>>> b =a>>> c = 2>>> a == bTrue>>> a is cFalse>>> a is not cTrue>>> a is bTrue

We can also determine whether they direct to the same object through id:

>>> id(a),id(b),id(c)(31108224, 31108224, 31108212)
6. Standard built-in functions
  • Cmp (obj1, obj2)

    If obj1 is greater than obj2, 1 is returned. If obj1 is less than-1, 0 is returned.

>>> a,b,c,d = 1,2,3,2>>> cmp(b,a),cmp(b,c),cmp(b,d)(1, -1, 0)
  • Type (obj)
  • Str (), repr (), "Operator

    The str (), repr (), and "operators can all get object content in string mode. The string obtained by str () is readable. The string obtained by repr () can be used to obtain the object again.

>>> str([1,2,3])'[1, 2, 3]'>>> repr([1,2,3])'[1, 2, 3]'>>> '[1,2,3]''[1,2,3]'
7. Type factory Functions

Python2.2 unifies classes and types. The so-called built-in type conversion functions such as int (), type (), and list () have all become factory functions. That is to say, they look like functions, essentially classes. When they are called, an instance of this type is actually generated, just like a factory generates a cargo.

8. Classification of standard types

Store, update, and access models.

9. types not supported by Python
  • Char or byte
  • Pointer
  • Integer

    Python does not contain int, short, and long statements like C. When you use an integer value out of the range, python will automatically return a long integer to you. The value range of a long integer in python is large.

  • Float vs double

    The float of Python is actually a double of C. The floating point type is always inaccurate. Therefore, Python also provides the Decimals module, which has any precision. The Decimals module is useful when dealing with certain values such as money.

Original article: http://blog.csdn.net/u012162613/article/details/44049607

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.