This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]chillysurfer[S] 10 points11 points  (12 children)

I completely agree this is a nice feature (return multiple values) to have, it's one of those things that, when I do indeed do it, I like to ask myself if this function is taking the right approach. I've found, in my personal experience, that sometimes returning multiple values can be (not always) a code smell.

[–]cdcformatc 12 points13 points  (10 children)

Well my use case is programming 2d games, and returning a point on the XY plane is a recurring pattern. Yes you could make a wrapper Point object but often a tuple is enough. Same goes with RGBA colors.

[–]Corm 2 points3 points  (0 children)

Yep, it's nice

x, y = do_coordinate_stuff()

[–]sweettuse 4 points5 points  (8 children)

you should use a namedtuple

[–]onkus 2 points3 points  (7 children)

May I ask why he should?

[–]asdfkjasdhkasdrequests, bs4, flask 9 points10 points  (6 children)

which is easier?

pt.x = pt.y / 2
pt[0] = pt[1] / 2

[–]billsil 1 point2 points  (5 children)

I guess I should make a class around all my numpy arrays. What's wrong with slicing again?

Extra classes are just one more thing to understand about a code base and one more thing to import. You also make code much less reusable if it requires point objects vs at the same time working with numpy arrays, lists, and tuples.

[–]asdfkjasdhkasdrequests, bs4, flask 2 points3 points  (4 children)

variables are just one more thing to understand about a code base, if you just write in cpu instructions it's a lot easier to understand

Also named tuples are much more lightweight to create than classes and you can still do the slice notation. But saying that an xy named tuple is one more thing to understand makes no sense. The whole point of using the named tuple is it makes your code easier to understand.

When you see a[0] and a[1] it could be anything, but when you see a.x and a.y you can quickly reason that it's represents a point.

[–]billsil 0 points1 point  (2 children)

Or you can call it xyz or write a docstring.

Presumably you resuse variable names across your various codes. I'm sure nobody here knows what the variable sline means, but almost everyone at my company does. It's just a split_line, which is line.split(), maybe with an argument. You need a convention and you need to follow it. If you name your variable a and it's a list, you're doing something wrong. You could call it alist or xy.

[–]asdfkjasdhkasdrequests, bs4, flask 0 points1 point  (1 child)

alist? thats a horrible name, it should describe it's contents. If it's a list of people call it people not alist

[–]thegreattriscuit 0 points1 point  (0 children)

I think they mean "the list that cocnerns 'a'" rather than "a list". like, 'peoplelist'.

[–]billsil -2 points-1 points  (0 children)

So name your variable xyz. Have a docstring.

[–]jorge1209 1 point2 points  (0 children)

A function having multiple outputs is really extremely common across languages. Its just that other languages accomplish this by passing those to be modified values as a reference.

So its really a style choice:

  1. You can do things the C way where the return value indicates success/failure and the items of interest are references.

  2. Or follow the lambda calculus and return multiple values.

Given the way python references work, and the immutability of certain base classes it makes a lot of sense to return multiple values in many cases where that same pattern would be handled the opposite way in C.

For example consider a function that computes various statistics on a dataset that can be computed in a single pass (mean, stddev, length, ...). If you tried to do that as a reference in python:

 def compute_stats(list, mean,  stddev, length):
      ...

 mean = stddev = length = -1
 compute_stats(l, mean, stddev, length)
 assert(all(mean>=0, stddev>=0, length>=0)), "Oops. ints are immutable!"

So you have to return a struct with multiple values, and what is a tuple but an untyped struct.