Python dynamic strongly typed usage instance

Source: Internet
Author: User
This paper analyzes the dynamic strong type usage of Python. Share to everyone for your reference. Specific as follows:

Python variable declaration and definition

Unlike C #, Python does not need to define its type before using the variable, try running the following example:

i = Print I

As we can see from above, the variable i does not need to be defined before it is used, but it must be declared and initialized. Try running the following example:

i = 1print i + j

The above code produces an exception: "Nameerror:name ' j ' is not defined", and the Python hint variable j is undefined. This is not the same as a weakly typed language like basic. In basic, there is no exception when executing the above code, you can try it in the VBA development environment of Excel, change Print to MsgBox, the result will be output 1. This shows that Python is not a weakly typed language similar to basic.

On the other hand, one of the big differences between Python and C # is that while the program is running, the same variable name can represent different types of data (at different stages) to see the following example:

i = 1print I,type (i), id (i) i = 10000000000print I,type (i), id (i) i = 1.1print I,type (i), id (i)

The type of variable I undergoes the change of int, long and float during program execution, which is very different from static type language (such as C, etc.). Static language as long as a variable gets a data type, it will always be the type, and the variable name represents the memory location used to hold the data. The variable names used in Python are only references to various data and objects, and the memory location where the data is stored is obtained by ID (), and the 1, 10000000000, and 1.1 Three data we enter are stored in the memory locations indicated by the ID (). Until the garbage collection pulls it away (when the system determines that you are no longer using it). This is a typical feature of a dynamic language, which determines the type of a variable when it is assigned to it.

Python, on the other hand, is strongly typed and tries to run the following example:

#-*-Coding:utf-8-*-i = 10; j = ' ss ' Print i+j# correct notation is print str (i) +j, output 10ss

An exception is generated: "typeerror:unsupported operand type (s) for +: ' int ' and ' str '". In weakly typed languages such as basic, the above example runs normally and returns (though sometimes unpredictable) results.

So, we say that Python is both a dynamic type language and a strongly typed language, which is different from C #. It may take a while for a C # programmer to adapt to the way that Python's variables are declared, defined, and used, but I'm sure you'll soon like it because it makes things easier (and not unsafe). Also, C # 4.0 has begun to define and use variables in a similar way (by adding the keyword dynamic before the variable name), and if you learn the Python variables first, you will be able to adapt to the dynamic programming characteristics of C # 4.0 more quickly.

Hopefully this article will help you with Python programming.

  • 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.