FAQ: Nodes: Python - Nodes Python Introduction

This community-built FAQ covers the “Nodes Python Introduction” exercise from the lesson “Nodes: Python”.

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

Computer Science

Linear Data Structures

FAQs on the exercise Nodes Python Introduction

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!

Hello,

I have just finished Python 3 course and I feel like some notions about ‘class’ is still not organised well in my head;(
Please help!

class Node:
  def __ init__(self, value, link_node = None):
    self.value = value
    self.link_node = link_node

So in the code block above, link_node has been set as ‘None’. And why should it be redefined as ‘self.link_node = link_node’ again?

1 Like

None is the default argument, so link_node is only set to None when no argument is provided for this parameter

__init__() is still a method, parameters like value and link_node still have the scope rules. These variable only exist within the method

self is the current instance of the class, so doing self.value = value will create self.value class instance variable.

2 Likes

Thank you for your quick reply!

So if I understood correctly…

node_a = Node(11, node_b)

In above code, node_a will have 11 as its’ data and it’s linked to node_b.

You mentioned

link_node is only set to None when no argument is provided for this parameter

So by setting parameter to None, we get to create the class object without link_node? Is that why None is set in __init__method?

Thank you for your time again! :slight_smile:

yes

None is the default argument when no value is provided, so here:

node_a = Node(11, node_b)

link_node will be node_b while if we didn’t provide a default argument:

node_a = Node(11) # no argument for link_node

now link_node will be None (the default argument)

if we did:

class Node:
  def __ init__(self, value, link_node):
    self.value = value
    self.link_node = link_node

node_a = Node(11) 

we would get an error, we didn’t provide an argument/value for link_node parameter and link_node doesn’t have a default argument on which it can fallback.

3 Likes

in the ‘learn’ text it says:
“The node’s data will be specified when creating the node and immutable (can’t be updated). The link will be optional at initialization and can be updated.”

Is it always the case with nodes that the data is immutable and the link is mutable?

I have been reading around, just wondering what people think if they stumble upon this.

I’m seeing that the best way to establish a default value which is mutable (changeable) would be:

class Node:
  def __init__(self, value, link_node=None):
    self.value = value
    if link_node is None:
      link_node = []
    self.link_node = link_node

I have also seen others do something similar to this, however it is not recommended:

class Node:
  def __init__(self, value, link_node=None):
    self.value = value
    self.link_node = link_node or []

Would the best practice be to establish a default value for a class variable be my first code snippet?

One thought the link_node is to another node, as in the next node. Why would we incorporate more structure? The default value is mutable since we can pass in a Node instance to override it. Furthermore we can set link_node at any time, either to another Node instance, or back to None when there is no next node.

Ok awesome, that’s exactly what I needed. Thank you for your insight! Keeping it simple. It becomes so easy to add unnecessary complexity.

1 Like
>>> a = Node("Blue tile")
>>> b = Node("Red tile")
>>> a.link_node = b
>>> a.value
'Blue tile'
>>> a.link_node.value
'Red tile'
>>> b.link_node
>>> 
>>> print (b.link_node)
None
>>>
1 Like