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!
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.
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()”?
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.
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
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.
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!
Why does it use same name for input variable for <next_node>?
It was used when initializing. It should not be used again when defining function?
Instead Id like to use <new_next_node>. Correct me if I am wrong.