I want to know how to use an instance variable (class A) in class B. Here is a small code quote that runs normally:
class Node:
def __init__(self, value):
self.value = value
self.next = None
class Stack:
def __init__(self):
self.head = Node("head")
self.size = 0
def __str__(self):
cur = self.head.next
out = ""
while cur:
out += str(cur.value) + "->"
cur = cur.next
return out[:-3]
I wonder how I can use instance variables (.next and .value) in the class Stack. I think instance variables (.next and .value) can appear and be used only in Node class. As I know, we should use single inheritance and class Stack should change to class Stack(Node).
Also, I don't understand what while cur means. Is it the same as while True?
Could you explain this problem for me?