理解pointer in Python

  • What is Pointer

  • Pointer In C

    Pointers allow you to create great efficiency in parts of your code. They also cause confusion for beginners and can lead to various memory management bugs.

    Essentially, they are variables that hold the memory address of another variable for memory management.

    Whenever we create a variable or an object in a programming language, it’s stored in a particular CPU address. Whenever we output the data, it pulls from that address.

  • Pointer in Python

    Pointers in Python don’t really exist.

    For the cases where you need to mimic pointer behavior, you can simulate pointers in Python without the memory-management nightmare.

    Python tends to try to abstract away implementation details like memory addresses from its users. Python often focuses on usability instead of speed. As a result, pointers in Python don’t really make sense.

  • 前情回顾

  • Objects

    Everything in Python is an object.

    Each object in Python consists of three parts:

    1. Reference Count

      It deals with the memory in the CPU. It represents the number of Python variables referring to a memory location.

      You can get the exact value by sys.getrefcount(object) which return the reference count of the object. From its documents, the count returned is generally one higher than you might expect, because it includes the (temporary) reference as an argument to getrefcount().

    2. Type

      It refers to the kind of object like int, float, string, etc…

      The type is used at the CPython layer to ensure type safety during runtime.

    3. Value

      It’s the actual value of an object stored in the memory.

    Objects are of two types Immutable(int, float, double, str, bool, complex, tuple) and Mutable(list, set, dict).

    We can test whether an object is immutable or mutable by using id() and is :

    • id() returns the memory address of the object
    • is checks whether two
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值