FAQ: Linked Lists: Python - Node Implementation

#1

This community-built FAQ covers the “Node Implementation” exercise from the lesson “Linked Lists: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Linear Data Structures

FAQs on the exercise Node Implementation

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

#2

I’ve put in this and it says it is all correct. But the console prints out this

<bound method Node.get_value of <main.Node object at 0x7f58567cf588>>

Should it not print out 44? What am I doing wrong? What schoolboy error have I committed now?

class Node:
def init(self, value, next_node=None):
self.value = value
self.next_node = next_node

def get_value(self):
return self.value

def get_next_node(self):
return self.next_node

def set_next_node(self, next_node):
self.next_node = next_node

my_node = Node(44)
print(my_node.get_value)

#3

Hello, @dcepok, and welcome to the discussions.

Actually, it is quite common, even among experienced programmers, to make the mistake of omitting the parentheses, when the intention is to call a method that does not require arguments to be specified within those parentheses.

As parentheses were omitted, this statement displays a representation of the get_value method of the Node class:

print(my_node.get_value)

For more informative output, call that method, as follows:

print(my_node.get_value())

Then the output would be:

44

Whenever you unexpectedly see output that appears something like this, look for an intended method call where parentheses were omitted:

<bound method Node.get_value of <__main__.Node object at 0x10c78cf98>>

The above was edited on March 20, 2019 to add some discussion regarding the unexpected output.

#4

Thank you @appylpye, and needless to say it worked. That was good advice for future reference.
Kind regards
D

1 Like
#6

Why do we define the functions get_node_value and get_next_node? Say I have a Node called node.
Wouldn’t “node.value” be the same as “node.get_node_value()”?