pythonclass

Self-referencing classes in python?


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?


Solution

  • 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.