Created
October 27, 2010 04:48
-
-
Save dcolish/648466 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ctypes import * | |
class Node(Structure): | |
pass | |
Node._fields_ = [("next", POINTER(Node)), | |
("foo", c_int)] | |
class List(Structure): | |
_fields_ = [("head", POINTER(Node))] | |
def __init__(self, foo): | |
self.head = pointer(Node(None, c_int(foo))) | |
def append(self, extra_foo): | |
p = self.head | |
while p.contents.next: | |
p = p.contents.next | |
p.contents.next = pointer(extra_foo) | |
def __str__(self): | |
value = '' | |
p = self.head | |
while p: | |
value += str(p.contents.foo) | |
p = p.contents.next | |
return value | |
def main(): | |
l = List(0) | |
for x in range(1, 10): | |
l.append(Node(None, c_int(x))) | |
print l | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A little example of using ctypes