Python memory model for this program

后端 未结 3 888
名媛妹妹
名媛妹妹 2020-12-06 15:31

My query is on the below program with respect to symbols that are storing values and functions, when ran on http://pythontutor.com/.

3条回答
  •  悲&欢浪女
    2020-12-06 16:08

    1. In Python, don't worry so much about memory segments and what goes on behind the scenes. Rather, environments (scopes) are more important. The block-and-pointer diagram you included is a reasonable way to visualize the memory. The white portion shows what the global environment looks like. When the function is called, a new (blue) environment is created.

    2. const is a variable. Variables in Python are weakly dynamically typed, and can store anything. In fact, Python integers don't overflow, and can store numbers exceeding 264. In this case, const is a variable (with a confusing name) that contains the number 2.

    3. A function is an abstract notion of a callable blob of code. You can assign it to a variable just like any other value.

    4. You could consider it a function pointer if it makes you feel comfortable, but then you would be outing yourself as a C programmer. A Python programmer would just say that op has the function sub as a value.

提交回复
热议问题