0

I understand in Python that assigning a name to an object creates a reference to the object, and that when an objects' ref count goes to zero it is garbage collected. I want to understand whether I should be using pointers in my code or if allowing regular garbage collection is better.

import time

while True:
    foo = time.clock() #every iteration get the clock time

In my code above, every iteration of the loop assigns the variable name "foo" to reference a new float object returned from time.clock(). Since I know that "foo" will always make reference to a float object returned from time.clock() is it more efficient to use a pointer? (e.g. from ctypes module)? If this were C I would use a pointer but in Python I'm not sure if it matters.

willpower2727
  • 769
  • 2
  • 8
  • 23
  • 1
    Er, no. The ctypes module is for interfacing with C, you would never use it in pure Python code. You should take a hint from the fact that there is no built-in pointer type. – Daniel Roseman Sep 14 '16 at 13:29
  • 2
    `ctypes` is meant to be used when interfacing Python with other languages, particularly C. Native python does not have, nor need, pointers. – cdarke Sep 14 '16 at 13:30
  • 1
    Also it's not clear how you think a "pointer to a float object" would be any different from assigning a name to that object. – Daniel Roseman Sep 14 '16 at 13:31
  • You should also remember that Python is implemented in languages other than C, like C# (Iron Python), Java (Jython) and Python (PyPy), so pointers would be foreign even to the implementation. – cdarke Sep 14 '16 at 13:32
  • @DanielRoseman am I wrong in my understanding that the object and the name/pointer are separate from each other? Do they not occupy distinct addresses in memory? – willpower2727 Sep 14 '16 at 13:35
  • No you're not wrong, which just reinforces my question. A name *is* a pointer, more or less (except that you can't do anything with the address of the pointer itself, and shouldn't want to). – Daniel Roseman Sep 14 '16 at 13:36
  • By the way, "*when an objects' ref count goes to zero it is garbage collected*" is a simplification. It is more correct to say that it is a *candidate* for garbage collection. See http://stackoverflow.com/questions/9449489/when-are-objects-garbage-collected-in-python – cdarke Sep 14 '16 at 13:37
  • In the C implementation of Python, print `id(foo)` inside the loop. Does that look like a virtual address (pointer)? – cdarke Sep 14 '16 at 13:38
  • If the ref count goes to zero the object is typically deleted immediately. The garbage collection is required to delete objects that have circular references as the ref count will never go to zero. – Barry Scott Sep 14 '16 at 13:43
  • @cdarke it prints out a number like "67558536." Warning, I'm not real familiar with virtual and physical memory. I would suppose it is virtual? I have some more homework to do. – willpower2727 Sep 14 '16 at 13:45
  • I think it's clear that even thinking of making a pointer in Python is a mistake, I have missed a fundamental characteristic of the language... – willpower2727 Sep 14 '16 at 13:51
  • I assumed you were familiar with pointers. A pointer in C is a virtual address if you are working on an operating system like Windows, Linux, OS X, etc. The address itself is *usually* split into a page number and an offset within a page. Pointers only give real RAM addresses if you are writing something that is embedded in kernel, like a device driver. – cdarke Sep 14 '16 at 14:49

1 Answers1

0

There is no notion of pointer in pure Python. What you are referring to is the ctypes module, it is meant to interact with code actually written and compiled in a C compatible language (where pointers actually exist).

So your question is invalid.

Guillaume
  • 5,497
  • 3
  • 24
  • 42