FAQ: Linked Lists: Python - Node Implementation

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!

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)

1 Like

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 Likes

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

1 Like

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()”?

com
i’m just asking why should i create get_value() method for the Node class , can’t i easily get the value from self.value attribute ?

2 Likes

and the same thing for get_next_node() method and self.next_node attribute

@appylpye sorry for taking your time sir , but may you help ?

Hi @hossamokasha76917137,

Promoting the use of methods, instead of direct access to instance variables, is an example of encapsulation, which is one of the principles of object-oriented programming. More broadly, encapsulation consists of hiding the internal details of the workings of a type, such as a Python class, and offering prescribed, documented techniques for interacting with instances of types.

The benefits of using methods, such as getters and setters, to access and alter the internal states of instances include the potential to place code within those methods to maintain logs of accesses, to validate data before it is stored or used, and to coordinate interactions among objects as instances are accessed or altered. Standardizing the modes of access to instances of a class also creates the potential to upgrade the hidden internal workings of that class without requiring users of the class to adapt their code to conform to the internal upgrades.

While the get_value() and get_next_node() methods of the Node class in the current exercise do not maintain logs of accesses or do other work beyond getting a value or a reference to the next Node, respectively, the potential exists to build additional functionality into those methods. In essence, by promoting encapsulation, the exercise sets a pattern that is worth emulating for the design of classes, in general.

8 Likes

I THINK I GOT IT … encapsulation it’s something like if i’m making a class that only return the result of dividing two numbers , i need to make a method that prevent enter Zero to the 2nd number , to avoid ZeroDivisionError
so i think i understand why should i create setters and getters methods, but i just have another question , isn’t that will take space from the memory ? so in every project i should think if i will need these methods or not to save my memory ?
or python doesn’t care about the memory ? because from the first of the computer science path codecademy didn’t speak about the memory .
and really thank you for your help sir :heart_eyes:

A few hundred or a few thousand bytes of code for a method is miniscule compared to gigabytes of total memory or storage, so if there is any benefit to including a particular method, go for it.

2 Likes

thanks a lot !!!

How is it possible that

print(my_node.get_value())

prints an integer without casting it as a string? On other occasions the str() function was necessary.

Welcome to the forums @eric-heim!

The print() function is casting it to a string for you since it sees that you are trying to print an integer to the screen. For example, this works just fine:

print(42) // Output: 42

You do need to cast an integer to a string when combining data types, as in the following example:

print(42 + " is the answer!") // Error: TypeError

This can be resolved using either of the following:

print("42" + " is the answer!")  // Output: 42 is the answer!
print(str(42) + " is the answer!")  // Output: 42 is the answer!

Hope that helps!