All data types in Python are “reference type“

Posted smstong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了All data types in Python are “reference type“相关的知识,希望对你有一定的参考价值。

LinuxExam.net: All data types in Python are "reference type"https://www.linuxexam.net/2022/11/all-data-types-in-python-are-reference.html

1 Value VS Reference in Java/C#

Programmers familiar with Java/C# should know that those two languages have two categories of data types, primitive (value) type and reference type.

The key difference between value type and reference type is that value typed data is directly a value while reference typed data is a reference (pointer) to the real data.

For value typed data like int, double Java/C# would just allocate memory space for its value.

For reference typed data like String, Java/C# would instead allocated memory space only for a pointer. The value of the pointer is the memory address where the actual value is allocated by "new" keyword to create an object.

Both Java and C# provide syntax sugar for creating String objects, so no explicitly "new" keyword is needed normally.

正在上传…重新上传取消https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqFdNA7Xld-zBf_p9nb2i2vWv1dnuAx9aMXHjUU8ccJzM_roslwXDDobotdfU4meBBY56dzECpybo_jTEaQNY8ngOwklPZV8OHkn5J2ougvdCmzyKqZszVu2hlJwuMu6m9IlqdGzYU_clH14u-xGnzqkJ19Ag0R1Cm6Cbm4yrzjhsKH6jRbHciBp6b/s1360/DataTypes.jpeg

For value typed data, they're pure data; for reference typed data, the actual data usually has more information included, mostly pointers to methods to operate on the data.

To make it more clear, let compare the "int" and "Integer" in Java.

正在上传…重新上传取消https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinWkjn1J9hirYEsjDNd_HQlXA2YHc7EgqhfBJSYdDRmc0TePk-lLJjx2qD9OcaAjS_RSBrQS75iIOQsJZggTLTTQRG3NCWjZ1qHhBeoPc05-eehUI_L39T5UpJvlqCaJqWRtpmAB1vbLwTZz7b9KBNPh8DnaL3qBlEK0Z9R2Y9UnMHLptJBWKSDI2Z/s1460/JavaIntVsInteger.jpeg

One thing about C# specially is "struct" is value-typed.

2 All data are reference typed in Python

In Python, data types become much simpler. "Everything in Python is an object!"

And any object is reference typed. So all data in Python is reference typed.

正在上传…重新上传取消https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAc4fLaDF7Y7mnR4ts4SM8e43ZFBye85ixJQC_xoTziWVCfWP7cJ-Si5za2qmSl2kMB23YM9uxQyr7D8s10a5lvPjek2qAWirNaRXex7sSa_bKHBnJUIfXFzbT9laKdHN7H6dU5ZNLCpO2iy7biVe7zZ7A-RfTqQREtJnAUn9o9b8s-7o5jrCgW9uZ/s1540/PythonDataType.jpeg

As all data are reference typed, any value is an object and has methods.

正在上传…重新上传取消https://blogger.googleusercontent.com/img/a/AVvXsEhmx7-gxk9g9sF2D9OtZegkG1L0MnUFMP58oiSDKrC7n-PoTKYiCdIlP9G756kWvNNGNMm4wQt0Hnq3NzFECGDiz6GknMkYpskLD9bXbnSAwGBug4Z_y-3T17XKS5IhSKa4tgguVhljeaYy8Hw1r_Bwoaaj2FqmxM0wMtfvGO3yLAge_fnC0YnO_R8H


 

Please be noted that "100.__str__()" is not valid in Python due to conflicting with a float number syntax like "100.12". The wordaround is enclsing it with parentheses.

3 Thoughts

The main reason why Java/C# differentiate Value and Reference is PERFORMANCE. Value typed data are more efficient to operate and local variables only reside on stack, so no memory management load at all.

Python is a "PURE OBJECT ORIENTED" language where every single thing is a reference typed object.  For example, Even if you just want a number 1, Python still needs to create a object for it. Creating and garbage collecting objects are not free for sure. In order to improve performance, Python makes many types like numbers and string "immutable". One advantage of "immutable" types is immutable objects can be shared safely.

For example, 

n1 = 1

n2 = 1

正在上传…重新上传取消https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhInU12tknCaDL51tBJRGEZiy2utYGDIO4kOezhyEFdBx739rUNpxZJmPI-OoqPrzsHGtbMi7FSQg73w7lA1ArGy076w_3u6slGPgrRCLp3Fv86hBe8nIIowbXYXdTxW6IpVEmBqLsKOoFj8GMOc_6fC8zX4uY7Q7eDNPdGJGbYOh6F8T-3MZnKiy69/s1220/ImmutableObjects.jpeg

Instead of creating two individual int objects, Python maybe just create one and let both n1 and n2 reference to this single object.

But be aware that "sharing immutable objects" is totally implementation's choice, and not a Python specification. Python users should NOT assume its exsitance.

以上是关于All data types in Python are “reference type“的主要内容,如果未能解决你的问题,请参考以下文章

Design a key value data structure support Get/Fetch/Delete/RandomDelete all in O

Abstract Data Types in C

python python的is_all_type帮助器

Data Types in Java

python list_all_same_type.py

Python timezone package All In One