Oklol2958
Oklol2958

Reputation: 25

How to print out node object

lyst = file()

class Node:  
    def __init__(self,first,next=()):
        self.first = first
        self.next = next
    
def recursivelist(lyst):
    assert len(lyst) > 0
    if len(lyst) == 1:
        return Node(lyst[0])
    else:
        return Node(lyst[0],recursivelist(lyst[1:]))

print(recursivelist(lyst))

This will return just this: <main.Node object at 0x7f976fd24130>

When I am wanting to actually see it. Is there any way I can actually do that? I saw online that I can use the str method in the class, but I have no idea how to implement that.

Any help would be extremely helpful. Thank you so much.

Upvotes: 0

Views: 381

Answers (1)

Bernhard
Bernhard

Reputation: 1273

You need to implement the __repr__ and/or the __str__ function for your class, in this method you can clarify how you want an instance of your class to be represented when printed like that.

See documentation: https://docs.python.org/3.4/library/functions.html#repr https://docs.python.org/3.4/library/functions.html#func-str

Upvotes: 1

Related Questions