Using return with parenthesis

I was working on the Hash Map Function lesson and entered the following code.

. . .
def hash(self, key):
key_bytes = key.encode()
hash_code = sum(key_bytes)
return (hash_code)

which caused an error but then i got ride of the () in the return and it worked fine, my question is why? (in the post the indents are off for some reason so disregard the indentation issue)

I understand return in general and know it will return None, if nothing is present etc. but I don’t understand why no () vs. including () makes a difference?

Hi @ion_killah,

For advice on how to format code for posting, so that it displays indentation and other important details, see How to ask good questions (and get good answers).

It is also a good idea to supply a link to the exercise to which the post pertains.

While placing parentheses around the return value is unnecessary, it does not affect the result. Therefore, it should not cause a problem. What error message did you see when the parentheses were included?

1 Like

@ion_killah, in general, it’s probably considered better form to omit the parentheses with return. Since return is not a function, they are not required, and the (definite, but not overwhelming) preference seems to be to omit them.

return can evaluate and return a sequence, called a tuple, of expressions.

If you have not yet studied the tuple data type, it is a data type consisting of a sequence of items separated by commas. By default, Python prints tuples surrounded by parentheses, leading people to suppose that parentheses are required, but they are not, unless it’s necessary to place them to avoid ambiguity (for instance, if the tuple contains mathematical expressions requiring parentheses.)

# cool things you can do with tuples

t = 5, 7  # tuple packing
print(t[0], t[1])  # the print function can take a tuple as an argument
a, b = t   # tuple unpacking

  # Here is a function with multiple expressions in the return statement: a tuple
def sq_cube_fourth(x): 
    return x**2, x**3, x**4

q = sq_cube_fourth(5)  # call the function

print(q)   # this will be a tuple
r, s, t = q  # now let's unpack it

''' Output:
(5, 7)  # Python adds parentheses
5 7
(25, 125, 625)  # again with the parentheses

There are other rules, the most important being that you can’t change the items within a tuple, as you can with lists. As with strings, if you need to change something, you must build a new tuple.

First thank you for the responses. Got it, it shouldn’t make a difference but it is a preference or best practice element.

It may not have been an error in the terminal but here is another example.

class HashMap:
  def __init__(self, array_size):
    self.array_size = array_size
    self.array = [None for item in range(array_size)]

  def hash(self, key):
    key_bytes = key.encode()
    hash_code = sum(key_bytes)
    return hash_code
  def compressor(self, hash_code):

It wont let me move on it says “Does HashMap.compressor() return hash_code % self.array_size?”

But if i remove the () on the return it lets me progress, however based on my understanding from this discussion it shouldn’t matter.

Including the parens casts the return value to a tuple, which might not be the expected return from the standpoint of the SCT (or the caller, for that matter). They do have an effect on the return value, make no mistake.

Ah, thank you. I didn’t think of it casting it.

1 Like

You’re welcome. It is a bit unexpected if you are coming from JavaScript. JS ignores parens except in grouping for precedence concerns. There is no such thing as a tuple in that language.

Surrounding a single expression with parentheses will not convert it to a tuple. To create a tuple from a single expression, we need to place a comma within the parentheses after the expression.

See the following:

class Spam:
  def __init__(self, topping):
    self.topping = topping
  def get_topping(self):
    return self.topping
    # return(self.topping)
    # return(self.topping,)

s = Spam("eggs")



Now, use the parentheses:

  def get_topping(self):
    # return self.topping
    # return(self.topping,)



Finally, use the parentheses and a comma:

  def get_topping(self):
    # return self.topping
    # return(self.topping)



parentheses aren’t even part of the syntax for a tuple, it’s just the comma

the sct is pattern matching with regex, because that’s such a good ideanot really

Yes, indeed, this will give us a tuple:

  def get_topping(self):
    return self.topping,

I stand corrected. Thank you, all.

Yeah, by focusing on a pattern in the user’s code rather than directly on the result, the SCT can mislead the user into thinking there was an error, when in fact there was not …

Messages from SCTs should strive to make it clearer whether they are a complaint from the SCT itself or whether they originate from the Python interpreter or even relate to the actual result, especially in the introductory courses. With experience, users can make the distinction, however, learners cannot be expected to recognize the difference right from the start.

1 Like

Thanks, to all of you for clearing that up. Nice to know its just the backend system checking what I have done, which is sending that response out and not an actual error. For curiosities sake what does SCT?

Submission Correctness Test