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.

正在上传…重新上传取消icon-default.png?t=M85Bhttps://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.

正在上传…重新上传取消icon-default.png?t=M85Bhttps://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.

正在上传…重新上传取消icon-default.png?t=M85Bhttps://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.

正在上传…重新上传取消icon-default.png?t=M85Bhttps://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

正在上传…重新上传取消icon-default.png?t=M85Bhttps://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.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值