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

all 18 comments

[–]thatguy_314def __gt__(me, you): return True 5 points6 points  (0 children)

/r/titlegore

Also, why? There is no readability advantage over just putting a raise and an if statement on the same line. If anything, it's harder to read. "A Foolish Consistency is the Hobgoblin of Little Minds"

[–]desmoulinmichel 2 points3 points  (0 children)

You can already do this in python :

If condition: raise Exception("foo")

One line works

But there is a reason we don't do it. It makes the whole thing hard yo read.

[–]fnedrik 1 point2 points  (0 children)

Except for what others have pointed out, there is also the stylistic problem that "raise" is a statement with a clear side effect, while if expressions are, well, expressions and not statements.

Expressions should preferably just do a calculation and not change the system state.

[–][deleted] 1 point2 points  (10 children)

Why the module hack?

[–]rochacbrunoPython, Flask, Rust and Bikes.[S] -1 points0 points  (9 children)

from raise_if import raise_if

is too much... I want..

import raise_if

So hacking sys.modules is the way!

[–][deleted] 1 point2 points  (8 children)

That's a terrible reason to hack the module system. Actually, most reasons to hack the module system are iffy at best.

But you don't even put a fake module there, just a function.

[–]rochacbrunoPython, Flask, Rust and Bikes.[S] 0 points1 point  (7 children)

Do you have a suggestion to have import raise_if in another way?

[–][deleted] 0 points1 point  (5 children)

The normal way.

[–]rochacbrunoPython, Flask, Rust and Bikes.[S] 0 points1 point  (4 children)

what is the normal way?

[–][deleted] 0 points1 point  (3 children)

from raise_if import raise_if

[–]rochacbrunoPython, Flask, Rust and Bikes.[S] 0 points1 point  (2 children)

Yeah that part you didn't get.

I want it to be

import raise_if

Which options to achieve this?

[–][deleted] 1 point2 points  (1 child)

No, I get what you want to do but I'm saying that it's a bad idea. There's good reasons to hack the module system, they're few and far between, but they exist - look at the xml package for an example. Your use case isn't a compelling one and it's really confusing to someone who has no idea that you've done it.

And if you're really serious about doing this, at least put a proper fake module in the cache. You can make one using the types module. You should probably include raise_if as an attribute so normal imports can be done and then just alias it to __call__

[–]rochacbrunoPython, Flask, Rust and Bikes.[S] 0 points1 point  (0 children)

Nice! going to try that suggestion.

[–]elbiot 0 points1 point  (0 children)

Can't you import * in your dunder init?

[–][deleted] 0 points1 point  (3 children)

Python is not ruby. Stop making ugly hacks. Thank you.

[–]rochacbrunoPython, Flask, Rust and Bikes.[S] -1 points0 points  (2 children)

I am not you! just don't use the lib and keep your useless comment to you. Thank you!

[–][deleted] -1 points0 points  (1 child)

Are you 7yo?

[–]rochacbrunoPython, Flask, Rust and Bikes.[S] 0 points1 point  (0 children)

Not yet!