0

This question has a part a and part b, where b is the what I am trying to achieve, but a is stumbling block.

(a) I have a view function in a React/Flask app that returns a tuple which contains three float values. This tuple is itself a value returned from another function call.

To return this tuple from view function as a response, I first tried to cast this to a string, using str(tuple). On the frontend this displayed as the literal tuple, comma separated, (3.0, 6.7, 9.1). However when I did:

return '{} {} {}'.format(value1, value2, value3) 

I got the string itself on the frontend, without the tuple () and comma separated values. So this part a question is, why does casting to a str, as per first approach, not work in the same way?

(b) In the end, the goal is that I need to return the tuple from the view function in Flask in such a way that I can access each value separately on the front end, so would I be better off trying to convert the tuple to an object? If so, how could I go about handling the tuple returned here to achieve that?

timman
  • 479
  • 5
  • 14

1 Answers1

1

So, as far as I understand, your question in part a is: Why does the following code

t = (3.0, 6.7, 9.1)
print(str(t))
print('{} {} {}'.format(3.0, 6.7, 9.1))

result it the following output

(3.0, 6.7, 9.1)
3.0 6.7 9.1

because you would expect

3.0 6.7 9.1
3.0 6.7 9.1

Well, t is a tuple and therefore a object. The str function defined by python will transform t to a string that represents the object. I don't know why they designed the function that way. I can just guess. It does make a lot of sense, though. Imagine you don't know what t looks like and you don't know the type. Now if you call str(t) and you see 3.0 6.7 9.1 you would probably think that t is a string "3.0 6.7 9.1". But in fact it is a tuple. Based on the output you can not know. With the brackets you will immediately think of a tuple. Because no one would print the brackets and commas by hand.

For part b: It depends. You can return the tuple as a string. Then you would have to do the parsing on your own. Not the best choice.

You could return it as a json string. So, create a dictionary

return {"value1": 3.0, "value2": 6.7, "value3": 9.1}

flask might instantly convert it to a string, maybe you have to do it on your own using json. This way it will return an object and in your frontend you will be able to extract the values using something like

response.value1

depending on the language of your frontend

David K. Hess
  • 16,632
  • 2
  • 49
  • 73
Sheradil
  • 407
  • 3
  • 14
  • Thank you @Sheradil. I thought objects had to have keys-value pairs. Why is a tuple considered an object? Because it can have keys but it's not required? – timman Mar 02 '21 at 18:49
  • Or is it more that a tuple is a data type that can hold objects? – timman Mar 02 '21 at 18:57
  • When I said object I meant data type. My bad. The tuple is one of the 4 basic data types python has. I am not sure what you mean with "objects had to have key-value pairs". The dictionary data type has keys and values. – Sheradil Mar 04 '21 at 13:07
  • I mean object in sense of JS and other langs where they maintain key value pairs. Not sure if Python is the same. Dict seems to do that work instead. – timman Mar 04 '21 at 14:01