Dave Python exercises 3-objects

Source: Internet
Author: User

# Encoding = UTF-8 </P> <p> # *************** Part 1: object ****************** <br/> # Python object <br/> # python uses the object model to store data. Constructing a value of any type is an object. All Python objects have three features: identity, type, and value. <Br/> # identity: <br/> # each object has a unique identity. You can use the built-in function ID () to identify any object. This value can be considered as the memory address of the object. <Br/> # type: <br/> # The object type determines what type of value the object can save, what operations can be performed, and what rules are followed. You can use the built-in function type () to view the python object type. Because the type in python is also an object, type () returns an object instead of a simple string. <Br/> # value <br/> # data items represented by objects <br/> # Python has a series of basic (built-in) data types, if necessary, you can also create custom types to meet your application needs. Most applications usually use the standard type. For specific data storage, you can create and instantiate classes. </P> <p> # object attributes <br/> # Some Python objects have attributes, values, or associated executable code, such as methods ). Python uses the dot (.) flag to access attributes. Attributes include the name of the corresponding object. The most common attributes are functions and methods, but some Python types also have data attributes. Objects containing data attributes include (but are not limited to) classes, class instances, modules, plural numbers, and files. </P> <p> # *************** Part 2: standard Type ***************** <br/> # Number (divided into several subtypes, three of which are integer types) <br/> # integer <br/> # Boolean <br/> # Long Integer <br/> # floating point type <br/> # complex type <br/> # string <br/> # list <br/> # tuples <br/> # dictionary </P> <p> # ************* Part 3: other built-in types: ***************** <br/> # type <br/> # Null Object (none) <br/> # file <br/> # Set/fixed set <br/> # functions/methods <br/> # module <br/> # class </P> <p> # type object and type object <br/> # Call type () function: <br/> # print (type ('Dave is DBA ') <br/> #--> <Class 'str'> <br/> # print (type (88 )) <br/> # --> <class 'int'> </P> <p> # What is the type of the type () function? <Br/> # print (type (88) <br/> # --> <class 'type'> </P> <p> # This shows that, all types of objects are type, which is also the root of all Python types and the default metaclass of all Python standard classes </P> <p> # none, python Null Object <br/> # Python has a special type called null object or nonetype. It has only one value, that is none. It does not support any operations or any built-in methods. None has no useful attributes, and its Boolean value is always false. </P> <p> # core Note: Boolean value <br/> # All standard objects can be used for Boolean testing and can be compared between objects of the same type. Each object has a Boolean value of true or false. The Boolean value of null object, any number with zero value, or null object none is false. <Br/>#< br/> # The Boolean value of the following objects is false. <Br/> # None <br/> # False (Boolean Type) <br/> # Number of all values to zero: <br/> #0 (integer type) <br/> # (floating point type) <br/> # 0l (Long Integer type) <br/> #0.0 + 0.0j (plural) <br/> # "(empty string) <br/> # [] (empty list) <br/> # () (empty tuples) <br/>#{} (empty dictionary) <br/> # The boolean values of objects whose values are not listed above are true, such as non-empty and non-zero. If the user-created class instance defines nonzero (_ nonzero _ () or length (_ Len _ () and the value is 0, then their Boolean value is false. </P> <p> # ************** Part 4: internal types: ****************** <br/> # Internal types include: <br/> # Code <br/> # frame <br/> # tracking record <br/> # slicing <br/> # omitted <br/> # xrange <br/> # generally, programmers do not directly deal with these objects. </P> <p> # code object <br/> # The Code object is a compiled Python source code snippet, which is an executable object. You can call the built-in function compile () to obtain the code object. Code objects can be executed by Exec commands or eval () built-in functions. The Code object itself does not contain any execution environment information. It is the core of a user-defined function and dynamically obtains the context when it is executed. (In fact, the Code object is a function attribute.) In addition to the Code object attribute, a function also has some attributes required by other functions, including the function name, document string, and default parameters, and global namespace. </P> <p> # frame object <br/> # frame object indicates the execution stack frame of Python. The frame object contains all the information that the python interpreter needs to know at runtime. Its attributes include links to the previous frame, code objects being executed, local and global namespace dictionaries, and current commands. Each function call generates a new frame, and each frame object creates a C stack frame accordingly. A frame object is a tracking record object. </P> <p> # tracking record object <br/> # Python triggers an exception when your Code fails. If the exception is not captured and processed, the interpreter will exit the script and display the following diagnostic information: <br/> # traceback (innermost last ): <br/> # file "<stdin>", line N ?, In ??? <Br/> # errorname: error reason <br/> # When an exception occurs, a tracing record object containing stack tracing information for the exception is created. If an exception has its own handler, the handler can access this tracing record object. </P> <p> # Slice object <br/> # when using the slice syntax extended by python, a slice object is created. The extended slice syntax allows you to operate on different index slices, including step slice, multi-dimensional slice, and omitted slice. The syntax of multi-dimensional slicing is sequence [start1: end1, start2: end2], or the ellipsis, sequence [..., start1: end1]. the Slice object can also be generated by the built-in function slice. The step slice allows you to use the third slice element for Step slice. Its syntax is sequence [Starting index: Ending index: Stepping value]. Python has long supported the extended step-by-step syntax, but does not work until python2.3. </P> <p> # omitting an object <br/> # omitting an object is used to extend the syntax of the slice, and serves as a mark. This object represents a ellipsis in the slice syntax. Similar to null object none, the omitted object has a unique name ellipsis, and its Boolean value is always true. </P> <p> # xrange object <br/> # calling the built-in function xrange () generates an xrange object. xrange () is the sibling version of the built-in function range, this function is used in scenarios with large datasets that require memory savings or which range () cannot be completed </P> <p> # ************** Part 5: standard Type Operators ****************** </P> <p> # comparison of object values <br/> # comparison Operators used to determine whether objects of the same type are equal, all built-in types support comparison. Comparison returns <br/> # boolean values true or false. If you are using a version earlier than python2.3, because these versions do not <br/> # boolean type, the comparison result is 1 (true) or 0 (false ). <Br/> # note that the actual comparison operation varies by type. In other words, the numeric type is compared based on the value size and symbol, <br/> # the string is compared based on the Character Sequence Value, and so on. <Br/> # Compare operations are performed on Object values, that is to say, it compares the value of the object rather than the object itself </P> <p> # multiple comparison operations can be performed on the same row, and the order of values is from left to right. <Br/> # print (3> 4> 5) <br/>#==> (3> 4) AND (4> 5) </P> <p> # Object Identity comparison <br/> # as a supplement to comparison of values, Python also supports comparison of objects. Objects can be assigned to another variable (by reference ). Because each variable points to the same (shared) data object, as long as any reference changes, other references of this object will also change. <Br/> # Python provides the is and is not operators to test whether two variables point to the same object. </P> <p> # A is B <br/> # This expression is equivalent to the following expression <br/> # ID (A) = ID (B) </P> <p> # obj1 is obj2 obj1 and obj2 are the same object <br/> # obj1 is not obj2 obj1 and obj2 are not the same object </P> <p> # Note: integer objects and string objects are immutable objects, so Python caches them very efficiently. This causes us to think that python should create a new object without the illusion of creating a new object. See the following example: <br/> # A = 1 <br/> # print (ID ()) <br/> # --> 506090016 <br/> # B = 1 <br/> # print (ID (B )) <br/> # --> 506090016 <br/> # both variables A and B reference 1, so their positions are equal. <Br/> # print (A is B) <br/> # --> true </P> <p> # If it is changed to floating point: <br/> # C = 2.0 <br/> # print (ID (C )) <br/> # --> 31298760 </P> <p> # D = 2.0 <br/> # print (ID (d )) <br/> # --> 30315720 </P> <p> # Boolean Type <br/> # Boolean logical operators and, or and not are all Python keywords, the priority of these operators ranges from high to low. the not operator has the highest priority. <br/> # It is only one level lower than all comparison operators. The and or operators are equal to the lower level. <Br/> # the logic of high ^ not expr is not (no) <br/> # | expr1 and expr2 expr1 and expr2 logic and <br/> # Low | expr1 or expr2 expr1 and expr2 logic or </P> <p> #* * *********** Part 6: standard built-in functions ******************* <br/> # Python provides some built-in functions for these basic object types: CMP (), repr (), STR (), type (), and the SLR quotation mark ('') operator equivalent to the Repr () function. </P> <p> # function <br/> # CMP (obj1, obj2) compares obj1 and obj2, and returns an integer I based on the comparison result: <br/> # I <0 if obj1 <obj2 <br/> # I> 0 if obj1> obj2 <br/> # I = 0 if obj1 = obj2 <br /> # repr (OBJ) or 'obj 'returns the string representation of an object <br/> # STR (OBJ) returns the string representation of an object suitable for readability <br/> # type (OBJ) get the type of an object, and return the corresponding type object </P> <p> # type (). Accept an object as a parameter and return its type. Its return value is a type object. </P> <p> # CMP () <br/> # the built-in CMP () function is used to compare the obj1 and obj2 objects. If obj1 is smaller than obj2, a negative integer is returned, if obj1 is greater than obj2, a positive integer is returned. If obj1 is equal to obj2, 0 is returned. Comparison is performed between objects, either standard objects or user-defined objects. If it is a user-defined object, CMP () will call the special method _ CMP _ () of this class __(). </P> <p> # Only filecmp is available in Python 3.2. CMP (), used to compare two files </P> <p> # STR () and repr () (and ''operator) <br/> # built-in function STR () and repr () or quotation mark operator ('') can easily obtain the content, type, numeric attribute, and other information of an object in string mode. The STR () function makes the string readable, and the character obtained by the Repr () function <br/> # The string can usually be used to obtain the object again, in general, the equation OBJ = eval (Repr (OBJ) is true. <Br/> # the two functions accept an object as its parameter and return an appropriate string. <Br/> # Although STR (), repr () and ''operations are very similar in terms of features and functions, in fact, repr () exactly the same as ''<br/> #. They return an object's" official "string, that is to say, in most cases, <br/> # This object can be re-obtained through the evaluate operation (using the eval () built-in function), but STR () is different. STR () strives to <br/> # generate an object's readable string representation, and its return results cannot be used for eval () Evaluate, but it is suitable for <br/> # combined with print statement output </P> <p> # type () and isinstance () <br/> # Python does not support methods or function overloading. Therefore, you must ensure that you call the desired function or object. <Br/> # type () returns the type of any Python object, not limited to the standard type. <Br/> # In addition to the built-in function type (), there is also a useful built-in function called isinstance (), which is also used to confirm the type of an object. <Br/> # Every time you call a function, the performance will be paid. If we can reduce the number of function calls, the program performance will be improved. </P> <p> # *************** Part 7: type factory functions ****************** <br/> # Python 2.2 unifies types and classes, all built-in types are also classes. Based on this, the original <br/> # so-called built-in conversion functions such as int (), type (), list (), etc, now all are factory functions. That is to say, although they <br/> # look a bit like functions, they are actually classes. When you call them, an instance of this type is actually generated <br/> #, just like the factory producing goods. <Br/> # The following familiar factory functions are called built-in functions in the old Python version: <br/> # int (), long (), float (), complex () <br/> # STR (), Unicode (), basestring () <br/> # list (), tuple () <br/> # type () <br/>#< br/> # New Data Types of classes of the new style are supported, and corresponding factory functions are added. The factory functions are listed below: <br/> # dict () <br/> # bool () <br/> # Set (), frozenset () <br/> # object () <br/> # classmethod () <br/> # staticmethod () <br/> # super () <br/> # property () <br/> # file () </P> <p> # ************** Part 8: standard Classification ******************** </P> <p> # "Basic ", these types are standard or core types provided by python. <Br/> # "Built-in" because these types are provided by python by default <br/> # "data ", because they are used for general data storage <br/> # "object", because the object is the default abstraction of data and functions <br/> # "original ", because these types provide the lowest level of granular data storage <br/> # "type ", because they are data types </P> <p> # There are three different models that can help us classify basic types, each model shows the <br/> # relationships between these types. These models help us better understand the relationships between types and their working principles. </P> <p> # Storage Model <br/> # The first way to classify a type is to see how many objects of this type can be saved. The python <br/> # type can accommodate one or more values, just as in most other languages. A type that can save a single literal object <br/> # We call it atomic or scalar storage. The types that can accommodate multiple objects are called container storage. (When a container object has <br/> #, it is called a composite object in the document. However, these objects not only refer to types, but also include objects such as class instances) <br/> # The container type brings about a new problem, that is, whether it can accommodate different types of objects. All Python container pairs <br/> # can accommodate different types of objects. <Br/> # A string looks like a container type because it "contains" characters (and often contains more than one character ), however, the <br/> # character type does not exist in Python, so the string is a self-contained text type. <Br/> # classification of Python types <br/> # scalar/atomic numeric (All numeric), string (all text) <br/> # container type list, tuples, and dictionaries </P> <p> # update Model <br/> # Another way to classify standard types is, ask a question for each type: "Can the value of an object be updated after it is created successfully?" <Br/> # Some types allow updating their values, while others do not. Variable objects allow update of their values, but immutable objects do not allow change of their values. <br/> # classification of Python types <br/> # variable type list, dictionary <br/> # unchangeable types of numbers, strings, and tuples </P> <p> # Access Model <br/> # Although the previous two types of model classification methods are introduced in Python very useful, they are not the primary model for distinguishing data types <br/>. For this purpose, we use the access model. That is to say, the data type is classified into <br/> # rows based on the method of accessing the stored data. There are three access methods in the access model: direct access, order, and ing. <br/> # type classification based on the Access Model <br/> # classification Python type <br/> # direct access to numbers <br/> # Sequence access string, list, And tuples <br/> # ing access dictionary </P> <p> # standard type classification <br/> # data type storage model update model access model l <br/> # digital scalar unchangeable direct access <br/> # string scalar unchangeable sequential access <br/> # list container changeable sequential access <br/> # tuples container unavailable change sequential access <br/> # dictionary container can change ing access <br/> # You can directly access non-container types. All numeric types belong to this type. <Br/> # sequence type indicates that elements in the container are accessed in the order of index starting from 0. You can access one element or multiple elements at a time. <br/> # this is what we know about slice ). Strings, lists, and metadata are all in this category. As we mentioned earlier, <br/> # Python does not support character types. Therefore, although strings are simple text types, because it has the ability to access substrings in sequence <br/> #, it is also categorized into sequential types. <Br/> # The ing type is similar to the index attribute of the sequence. However, its index does not use the ordered numeric offset value. Its elements <br/> # unordered storage, access through a unique key. This is the ing type, which holds a set of hash key-value pairs. </P> <p> # *************** Part 9: unsupported types: ******************* <br/> # Char or byte <br/> # Python has no char or byte type to save a single character or an 8-bit integer. You can use a string of 1 to represent a character or an 8-bit integer. <Br/>#< br/> # pointer <br/> # Python manages the memory for you, so there is no need to access the pointer. In python, you can use the ID () function to obtain the ID number of an object, which is closest to the pointer address. <Br/> # It doesn't make much sense because you cannot control this value. In python, everything is a pointer. <Br/> # int vs short vs long <br/> # A normal integer in python is equivalent to a standard Integer type and does not need to be similar to int, short, long <br/> # Integer type. In fact, Python's integer implementation is equivalent to a long integer in C. Because Python's integer and long integer <br/> # are closely integrated, users almost do not need to worry about anything. You only need to use one type, that is, the python integer. Even if the value <br/> # is beyond the expression range of an integer, for example, if two large numbers are multiplied, Python automatically returns a long integer to you. <br/> # No error is returned. <Br/>#< br/> # Float vs double <br/> # the C language has two floating point types: single precision and double precision. The floating point type of python is actually the double precision floating of the C language <br/> # point type. Python considers that the benefits of two floating-point types are not proportional to the overhead caused by the two floating-point types. <br/> # therefore, Python does not support single-precision floating-point numbers. For those who prefer to discard a larger value range and require higher accuracy <br/> # for users, Python also has a decimal floating point number type decimal, however, you must import the decimal module to use it <br/>. Floating Point Numbers are always inaccurate. Decimals has any precision. The <br/> # decimal type is useful when processing values such as money. Float is enough for processing weight, length, or other measurement units. </P> <p>

Bytes -------------------------------------------------------------------------------------------------------
Blog: http://blog.csdn.net/tianlesoftware
WEAVER: http://weibo.com/tianlesoftware
Email: dvd.dba@gmail.com
Dba1 group: 62697716 (full); dba2 group: 62697977 (full) dba3 group: 62697850 (full)
Super DBA group: 63306533 (full); dba4 group: 83829929 (full) dba5 group: 142216823 (full)
Dba6 group: 158654907 (full) chat group: 40132017 (full) chat group 2: 69087192 (full)
-- Add the group to describe the relationship between Oracle tablespace and data files in the remarks section. Otherwise, the application is rejected.

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.