Pointer in Python

There is no pointers in Python but there are pointers in Python indeed. Among different kinds of pointer implementation in Python, this post demonstrate an elegant way to use pointers, especially object pointers when doing Python OOP. Detailed ‘ctypes’ module of Python could be found: http://docs.python.org/2/library/ctypes.html#module-ctypes

Output:

*** Remote Interpreter Reinitialized  ***
>>>
node: name=a, number=-1, next=null
node: name=not a, number=1, next=b
node: name=func a, number=0, next=func b
None
None
(py_object(<NULL>), <__main__.LP_py_object object at 0x000000000265D648>)
(py_object(<__main__.Node object at 0x000000000266D2E8>), <__main__.LP_py_object object at 0x000000000265D648>)
node: name=not null now, number=-1, next=null
>>>

Source:

#——————————————————————————-
# Name:        pointerTry.py
# Purpose:     (Object) Pointer usage investigation/demonstraion in Python
#
# Author:      daveti
# Email:       daveti@cs.uoregon.edu
# Blog:        http://daveti.blog.com
#
# Created:     27/01/2013
# Copyright:   (c) daveti 2013
# Licence:     GNU/GPLv3
#——————————————————————————-

import ctypes

class Node(object):
    “Example class in Python OOP”
    def __init__(self, x):
        self.name = x
        self.number = -1
        self.next = None

    def __str__(self):
        return(‘node: name=%s, number=%d, next=%s’
                %(self.name, self.number,
                self.next.name if self.next != None else ‘null’))

def changeViaPointer(ptr):
    “Change the class Node member via ptr”
    ptr.contents.value.name = ‘func a’
    ptr.contents.value.number = 0
    ptr.contents.value.next = Node(‘func b’)

def main():
    “Demonstration for Python pointer operation”
    a = Node(‘a’)
    b = ctypes.py_object(a)
    c = ctypes.pointer(b)
    print(a)

    # Change the class member via pointer
    c.contents.value.name = ‘not a’
    c.contents.value.number = 1
    c.contents.value.next = Node(‘b’)
    print(a)

    # Change the class member via function call
    changeViaPointer(c)
    print(a)

    # What about ‘None’ object pointer?
    d = None
    e = ctypes.py_object(d)
    f = ctypes.pointer(e)
    print(d)
    f.contents.value = Node(‘not None now’)
    print(d)
    # NOT WORKING!

    # The correct ‘None’ pointer usage
    g = ctypes.py_object()
    h = ctypes.pointer(g)
    print(g, h)
    h.contents.value = Node(‘not null now’)
    print(g, h)
    i = g.value
    print(i)

if __name__ == ‘__main__’:
    main()

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s