FAQ: Queues: Python - Queues Python Size

from node import Node

class Queue:

Add max_size and size properties within init():

def init(self, max_size = None):
self.head = None
self.tail = None
self.max_size = max_size
self.size = 0

def peek(self):
return self.head.get_value()

In the function peek(), whats the difference between doing ‘return self.head.get_value()’ and ‘return self.head.value’

It doesn’t make sense not to just call the attribute of the instance of the Node class.

hihi

Not sure if anyone can help enlighten me?
How is using the method self.get_size() different from just using the class variable self.size?

For example, in the peek method:
def peek(self):
if self.size == 0:
print(“Nothing to see here!”)
else:
return self.head.get_value()

vs

def peek(self):
if self.get_size() == 0:
print(“Nothing to see here!”)
else:
return self.head.get_value()

both gives the same result?

I wrote a brief answer about this previously that may help. In particular the link to an SO question may provide you with answers as to why they’re often used-

Did you ever get a response? I’ve felt stuck a number of times already on exercises where the course seemingly makes leaps, and includes concepts that haven’t been covered. It usually amounts to small details, but it’s as though the original author just assumes that the reader just knows these small details without having covered them. Super frustrating when you find out your code would have worked, but the course was looking for a more streamlined answer using a technique that wasn’t shown before.

This thread is a good example. The OP showed code exactly as I wrote it, the same way we were shown thus far in the course. It was not correct. Only thru the knowledge of this reply did I figure out that the course was looking for an answer written in a way that was not yet taught.

Edit: in reading this link again, I’m even more sure that there are assumptions being made. “preferring return expressions over literals” is never made clear or even explained in a lesson, however in the exercise in this topic the statement
" * If so, return True if the max_size is greater than self.get_size()"
the course explicitly TELLS us to return a boolean, and then looks for a returned expression.

In below line of code, it is calling get_value method of class Node without creating instance of?

return self.head.get_value()