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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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.
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)
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.
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?
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: