码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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.

  • 相关阅读:
    [Linux]-----_inode与软硬连接
    【嵌入式】嵌入式系统稳定性建设:最后的防线
    代码随想录第四十三天|343. 整数拆分 ● 96.不同的二叉搜索树
    GESP C++ 三级真题(2023年9月)T1 ⼩ 杨储蓄
    ESP8266-Arduino编程实例-PCF8563实时时钟(RTC)驱动
    【网络安全】内网杀器之Cobalt Strike
    C++如何查看栈的变量
    用幻灯片讲解C++手动内存管理
    对称加密(symmetric encryption)和非对称加密(Asymmetric Encryption)(密钥、公钥加密、私钥解密)AES、RSA
    缓冲流(字节缓冲流、字符缓冲流)----Java
  • 原文地址:https://blog.csdn.net/smstong/article/details/127643763
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号