FAQ: Nodes: Python - Nodes Python Setter

This community-built FAQ covers the “Nodes Python Setter” 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 Setter

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!

In the class Node, why would one need the methods set_link_node(), get_link_node(), and get_value()?

Just simply access the instance variables like self.value, self.link_node, and assign values to the link_node attribute to like self.link_node = node2.

In my opinion the suggested solution is just making things complicated.

4 Likes

It is because creating attributes “on the fly” is not recommanded since you can’t control the user input:

class Car :
    def __init__(self, num_of_wheels = None):
        self.num_of_wheels = num_of_wheels

    def set_num_of_wheels(self, num_of_wheels):
        if num_of_wheels < 3:
            print("your car will have some issues")
        else:
            self.num_of_wheels = num_of_wheels

car_01 = Car()
car_01.set_num_of_wheels(2)
1 Like

Why does below code use same input variable name for <link_node>?
Since it was used earlier when <def init(self, link_node)>?
Does it cause error by giving same input variables?
I rather use <link_node_new> for it.

def set_link_node(self, link_node):

Would it be unwise to just exclude the argument from the init method? Wouldn’t this in a way be like saying you cannot assign this on on initialization?

  def __init__(self, value):
    self.value = value
    self.link_node = None

Also If working on a team project there is nothing but convention that would stop someone from accessing the attribute directly without the setter/getter methods?

thing = Node(5)
other_thing = Node(6)
thing.link_node = other_thing

And I have questions about the __ and _ attribute prefix, is this just convention for other developers?

class Node:
  def __init__(self, value, link_node=None):
    self._value = value
    self.__link_node =link_node

It appears that the double underscore hides things entirely where as the single underscore is still accessible as long as its included in the explicit call.

print(thing.value)
>>> 'Node' object has no attribute 'value'
print(thing._value)
>>> 5
print(thing.link_node)
>>> 'Node' object has no attribute 'link_node'
print(thing.__link_node)
>>> 'Node' object has no attribute '__link_node'

Idk but would it be considered wrong or bad practice to complete the class as so:

class Node:
  def __init__(self, value):
    self.__value = value
    self.__link_node = None

  def get_value(self):
    return self.__value
  
  def get_link_node(self):
    return self.__link_node
  
  def set_link_node(self,link_node):
    self.__link_node = link_node

thing = Node(5)
other_thing = Node('salt')
thing.set_link_node(other_thing)
print(thing.get_value())
print(thing.get_link_node())

>>> 5
>>> <__main__.Node object at 0x7f8602c6d128>