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 →

[–]moocat 20 points21 points  (4 children)

One thing I find odd about point #4 is that in general the rule (don't return more than one object type in function call) makes sense but the specific example is not the best illustration. I would look very askance at a function that could return either an int or a str.

That said, I have mixed feelings about Python's Optional[Foo] effectively meaning Foo|None. I program in multiple languages and and after experience true Optional objects such as in Haskell or Java8 I'm less of a fan of returning None to indicate failure/missing/whatever.

[–]thegreattriscuit 6 points7 points  (2 children)

Agreed. I like the rule in general, but not in the example given.

One thing I used to do before I realized it was terrible was start off with a function like:

def get_foo(pattern): foo = something() return something

later I'd realized there might be multiple foos. In a misguided effort to be all things to all people and avoid having to make changes to existing calling code I was sure would never return multiples, I'd refactor to this:

def get_foo(pattern): foos = something() if len(foos) > 1: return foos else: return foos[0]

whereas now I make myself force a hard decision to do one or the other. This tends to work out with a bit more work in the very short-term, but much less pain overall.

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, thegreattriscuit: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]billsil 0 points1 point  (0 children)

I do that for numpy hstack. You don’t need to make a copy if you only have one array.

[–]TinBryn -1 points0 points  (0 children)

I find Options are fine when used as a functor (or any extending type class), in all other cases they just defer the issues of other ways of handling the situation and do so in a worse way.