tags:

views:

103

answers:

5

In Python, can you have classes with members that are themselves pointers to members of the type of the same class? For example, in C, you might have the following class for a node in a binary tree:

struct node {
    int data;
    struct node* left;
    struct node* right;
} 

How would you equivalently create this in python?

+6  A: 

Python is a dynamic language. Attributes can be bound at (almost) any time with any type. Therefore, the problem you are describing does not exist in Python.

Ignacio Vazquez-Abrams
Huh? This didn't answer the OP's question """How would you equivalently create this in python?"""
John Machin
@John Machin: Obviously the OP thought it did, since he accepted the answer. :)
musicfreak
@John: Although he wrote "How would you equivalently create this in python?", what he was actually asking was "How do you declare members that have the type of the current class?". The answer, of course, is that you don't, since you don't need to in Python.
Ignacio Vazquez-Abrams
@musicfreak: "Obviously"??? The OP's mouse clicked on the "tick" button -- this proves neither thought nor acceptance.
John Machin
@Ignacio: You are channelling the OP? Or are you working backwards from your answer to derive the question?
John Machin
@John: Years of doing support on IRC teaches you that people don't always ask the question they want answered.
Ignacio Vazquez-Abrams
+5  A: 

Emulating a C struct in Python (using str instead of int as the data type):

"Declaration":

class Node(object):
    data = None # str
    left = None # Node object or None
    right = None # Node object or None

Usage:

root = Node()
root.data = "foo"

b = Node()
b.data = "bar"
root.left = b

z = Node()
z.data = "zot"
root.right = z
John Machin
You're declaring class members here, not instance members, aren't you?
Russell Borogove
Sorry, can somebody please explain the above comment?
amssage
@amssage: You might want to check out the answers to [this other question](http://stackoverflow.com/questions/2424451/about-python-class-and-instance-variables) and/or read the page from the [Python tutorial on classes](http://docs.python.org/tutorial/classes.html).
Daniel Pryden
Whoa, I never realized that.
Russell Borogove
@Russell Borogove: What did you never realise?
John Machin
That instances inherit class variables. I think I ran into some confusing behavior when I was first learning Python OO and just avoided using class variables entirely since then. I set up all my instance defaults in __init__.
Russell Borogove
Confusion probably caused by using a mutable value. And they're not "variable" as far as the instance is concerned until the instance changes it. This means that you can have a whole bunch of "class constants" including lists, dicts, sets which instances treat as read-only reference info. Happiness prevails so long as you obey the "peek but don't poke" rule.
John Machin
@Russell Borogove: As an instance of a borogove, perhaps you can enlighten us as to what Lewis Carroll meant by "mimsy" :-)
John Machin
A portmanteau word combining 'miserable' and 'flimsy'. http://www.alice-in-wonderland.net/school/alice1019.html
Russell Borogove
A: 

http://code.activestate.com/recipes/286239-binary-ordered-tree/ is a sample binary tree created using that structure.

ridecar2
+1  A: 

How would I equivalently create this in python?

class node( object ):
    def __init__( self, data, left, right ):
        self.data = data
        self.left = left
        self.right = right

Since all Python variables are, in effect, typeless references, you don't have to mention up front that left and right are going to be instances of nodes.

Russell Borogove
+2  A: 

You can't declare types in Python - therefore, there are no problems declaring types in Python.

THC4k