Re: A question about reference in Python.
- From: "James Mills" <prologic@xxxxxxxxxxxxxxxxxxx>
- Date: Mon, 8 Dec 2008 16:09:13 +1000
Hi,
This is really really really pointless code and a really really pointless
exercise, but nonetheless, here is a very very basic and minimal
implementation of what you're expecting. This should almost
*never* be done in Python! Python is a superior dynamic programming
language, but it's NOT C!
Here goes:
jmills@atomant:~/tmp$ ./list.py
0x[0]
1x[1]
2x[2]
3x[3]
Traceback (most recent call last):x[4]
File "<stdin>", line 1, in <module>
File "./list.py", line 36, in __getitem__
return node.value
AttributeError: 'NoneType' object has no attribute 'value'
jmills@atomant:~/tmp$
And the code:
#!/home/jmills/bin/python -i
class Node(object):
value = None
prev = None
next = None
def __init__(self, value, prev=None, next=None):
self.value = value
self.prev = prev
self.next = next
class List(object):
data = None
def __init__(self, *seq):
if seq:
first = prev = node = None
for x in seq:
if not first:
first = Node(x)
prev = node = first
else:
node = Node(x, prev)
prev.next = node
prev = node
self.data = first
def __getitem__(self, x):
node = self.data
for i in xrange(x):
node = node.next
return node.value
x = List(0, 1, 2, 3)
Notes:
I have not implemented any error checking whatsoever.
I have not implemented any of your normal list
operations whatsoever (except 1). __getitem__.
Have fun,
cheers
James
On Mon, Dec 8, 2008 at 3:26 PM, Group <kermit.group@xxxxxxxxx> wrote:
Hello, I'm studying algorithom. For concentrating on the question itself, I
intend to use Python to implement the algorithoms.
Now, I want to write a Red-Black Tree, and a List structure. In C/C++, I can
use pointers to refer to children notes (or next notes). But, in Python,
how
can I do it? Except the sequence, I know not any way.
You'd better help me understan how can I transform the following C code into
Python:
/* a List */
struct {
int data;
int *next;
int *prev;
}
That's all. Thanks!
Kermit
--
http://mail.python.org/mailman/listinfo/python-list
--
--
-- "Problems are solved by method"
.
- Prev by Date: Re: Rich Comparisons Gotcha
- Next by Date: Re: A question about reference in Python.
- Previous by thread: Re: A question about reference in Python.
- Next by thread: Re: A question about reference in Python.
- Index(es):
Relevant Pages
|