Pointers in PYTHON

Martijn Faassen m.faassen at vet.uu.nl
Wed Jun 14 10:21:30 EDT 2000


Robin Porter <robinporter72 at yahoo.ca> wrote:

> I have been reading everything I can possibly get my hands on regarding
> PYTHON.  I have not yet come accross anything regarding the use of pointers
> for linked and double linked lists.

Why do you want a linked list? Anyway, linked lists aren't hard:

class Node:
    def __init__(self, next):
        self.next = next

linked_list = Node(Node(Node()))

Every name in Python's a reference, so no pointers are needed. Just think
of every name in Python as a pointer (to an object), if you like.

Doubly linked lists are along the same pattern, but have the trouble that
they introduce circular references, which is bad for Python's reference
counting based garbage collection scheme. You have to break one of the
references yourself for it to work:

class Node:
    def __init__(self, prev, next):
        self.prev = prev
        self.next = next

node1 = Node(None, None)
node2 = Node(None, None)
node1.next = node2
node2.prev = node1

# and now to clean up so that refcounting works:
node2.prev = None

>  I am just new to this language so please forgive my ignorance if it has
> been mentioned here before.

No problem.

> In addition to what ever information you can give me on pointers in PYTHON,
> could you please provide some info on combining C, Assembler, and Python
> for a project.

Take a look here:

Extending and Embedding the Python Interpreter

http://www.python.org/doc/current/ext/ext.html

And here:

Python/C API

http://www.python.org/doc/current/api/api.html

These are part of the standard Python documentation. You'll have to do
the assembler calls yourself, from C.

Regards,

Martijn
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list