0

From Programming Language Pragmatics, by Scott

Object lifetimes generally correspond to one of three principal storage allocation mechanisms, used to manage the object’s space:

  1. Static objects are given an absolute address that is retained throughout the program’s execution.

  2. Stack objects are allocated and deallocated in last-in, first-out order, usually in conjunction with subroutine calls and returns.

  3. Heap objects may be allocated and deallocated at arbitrary times. They require a more general (and expensive) storage management algorithm.

The C programming language has static objects, stack objects and heap objects.

Does Python have static objects, stack objects and heap objects?

I saw in another post that CPython allocate all objects on the heap. Does it mean that all the objects in Python are heap objects?

But Python also has static methods. Are static methods in Python static objects in the PLP book?

Thanks.

Community
  • 1
  • 1
Tim
  • 1
  • 141
  • 372
  • 590
  • static methods are not static objects in the PLP book, no. They are descriptor objects that disable the binding behaviour of methods (so no `self` is passed in). – Martijn Pieters Sep 15 '17 at 22:13

1 Answers1

4

Python objects are mostly heap objects - however, there are some special PyObject singleton values in CPython that are static in C; though this is an implementation detail. For example the usual built-in types have static storage duration. There are no stack (Python) objects that I know of.

The static storage duration, as understood here, has absolutely nothing to do with static methods.

  • Thanks. What are "special singleton values in CPython that are static"? Are there stack objects in CPython? – Tim Sep 15 '17 at 22:32
  • @Tim I would imagine that the small integers are static singletons too. You can get their memory address with `id()`. Notice that `id(255)` and `id(256)` are near each other, but `id(257)` is someplace completely different. Again, this is all implementation details and you shouldn't care at all how they're physically allocated. – Mark Ransom Sep 15 '17 at 22:58
  • @MarkRansom In C, static, stack and heap objects are not implementation details, but language concepts which are important for programming in C. I think it is also the case in Python. – Tim Sep 15 '17 at 23:02
  • 2
    @Tim no, Python doesn't work like that. You can declare a local variable and pass it to a function that puts it in a global variable, and because it's not on the stack it won't be destroyed when it goes out of the local scope. Python eliminates the distinctions so you don't have to worry about them, and it's not the only language to do so. – Mark Ransom Sep 16 '17 at 02:22
  • @MarkRansom Thanks. I don't quite understand "You can declare a local variable and pass it to a function that puts it in a global variable, and because it's not on the stack it won't be destroyed when it goes out of the local scope. " Could you give an example? – Tim Sep 16 '17 at 20:56