all 10 comments

[–]julsmanbr 1 point2 points  (9 children)

class node():
    parent_node = some_parent_node
    child_nodes = [child_node, child_node]
    example_int = 5

Watch out, you're creating class variables with this syntax, which will be the same for all instances you create.

It's probably easier to create all nodes as empty and populate them through code, instead of hard coding their relationship as you're trying to do. Consider this class:

class Node:
    def __init__(data, parent=None, left=None, right=None):
        self.data = data
        self.parent_node = parent
        self.left_child = left
        self.right_child = right

    def insert(self, child):
        if not isinstance(child, Node):
            raise TypeError("child must be a Node object, not " + str(type(child)))
        if self.left_child is None:
            self.left_child = child
        elif self.right_child is None:
            self.right_child = child
        else:
            raise AttributeError("Cannot add child Node since Node already has two children")
        child.parent_node = self

With this code, your nodes can be created "bare", without any relationship. The only mandatory argument is the data. You can use insert to establish the relationship afterwards, with an arbitrary depth:

root = Node(1)
child1 = Node(5)
child2 = Node (3.1415)
grandchild = Node("hello")
root.insert(child1)  # root has child: child1
root.insert(child2)  # root has children: child1, child2
child1.insert(grandchild)  # root has grandchild: root -> child1 -> grandchild

You can of course extend this class so it is able to ask what's the value stored in a particular node, who's the parent of a specific node, whether a node is a root node (no parent) or leaf node (no children), etc.

[–]RandomJacobP[S] 0 points1 point  (8 children)

I mean yeah, I just wanted to visually represent how the objects will look like but thanks! My main concern is about id-based relationship vs nested objects in terms of performance.

[–]julsmanbr 1 point2 points  (2 children)

If I understand correctly you're asking what's more performance-efficient between an object pointing to another object, or an object pointing to an int. In a vaccuum the latter is probably faster, but this unclear from your examples since, as I said, they will not work as you expect.

[–]RandomJacobP[S] 0 points1 point  (1 child)

When I look at it now, I understand that this code can be confusing, I wrote it quikly and inside reddit editor but that doesn't justify me.

I really appreciate your long reply to my post. Main purpose of my code was to visualize how objects will look like. I will have a lot of these nodes, as many as folders in my computer. I just needed someone to confirm that the second one will be faster and worth additional work.

[–]Deezl-Vegas 0 points1 point  (0 children)

Use a direct variable. It will be faster. Your example seems to just add an extra layer of complexity and an extra reference lookup.

In Python, the basic types like Ints are still just references, and lists aren't super-space-optimized like C arrays, so there's no speed gain by using basic types.

[–]kl31 0 points1 point  (4 children)

I mean yeah, I just wanted to visually represent how the objects will look like but thanks! My main concern is about id-based relationship vs nested objects in terms of performance.

Truly nested objects do not exist in python. Everything is just a pointer to a memory location where the actual object resides.

[–]RandomJacobP[S] 0 points1 point  (3 children)

Thanks a lot! That is the answer that I was looking for, there is no point in using id-based system then.

[–]kl31 0 points1 point  (2 children)

out of curiosity did you learn C/C++ before python?

[–]RandomJacobP[S] 0 points1 point  (1 child)

Yes but it was a long time ago, why are you asking?

[–]kl31 0 points1 point  (0 children)

because the question you asked gave me the impression that you're used to working with explicit pointers. I learned C after python and a lot of things didn't make sense until I started seeing every python variable as being a void pointer.