Python dynamic Strong-type usage instances

Source: Internet
Author: User
Tags in python

The example of this article analyzes the strong type usage of Python dynamic. Share to everyone for your reference. Specifically as follows:

Python variable declarations and definitions

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

?

1 i = Print I

As we can see from the top, 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:

?

1 2 i = 1 Print i + j

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

On the other hand, a big difference between Python and C # is that the same variable name can represent different types of data (at different stages) during the program's operation, and look at the following example:

?

1 2 3 4 5 6 i = 1 print i,type (i), id (i) i = 10000000000 print I,type (i), id (i) i = 1.1 Print i,type (i), id (i)

The type of the variable I undergoes changes in int, long, and float during program execution, which differs greatly from static type languages such as C. Static language as long as a variable obtains a data type, it will always be this type, the variable name represents the memory location used to hold the data. The variable names used in Python are just references to various data and objects. The ID () gets the memory location where the data is stored, and the 1, 10000000000, and 1.1 Three data we enter are saved in the memory location indicated by the ID () until the garbage collector pulls it away ( When the system determines that you no longer use it. This is a typical feature of a dynamic language that determines the type of a variable when it is assigned a value.

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

?

1 2 3 4 #-*-Coding:utf-8-*-i = 10; j = ' ss ' Print i+j #正确的写法是print str (i) +j, output 10ss

An exception is generated: "typeerror:unsupported operand type (s) for +: ' int ' and ' str '". In a weakly typed language such as basic, the example above will run normally and return (although sometimes unpredictable) results.

So, we say that Python is both a dynamic type language and a strongly typed language, and this is a different place from C #. The C # Programmer may take some time to adapt to the declaration, definition, and use of this variable in Python, but believe that you will soon like it because it makes things simpler (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 Python variables, you will be able to adapt to the dynamic programming features of C # 4.0 faster.

I hope this article will help you with your 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.