Python Forum
feature request: ternary fallthroughs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
feature request: ternary fallthroughs
#1
I suspect theres a better way of doing things, but consider the following code:

def get_args():
   if len(sys.argv) > 1:
        val = int(sys.argv[1]) if int(sys.argv[1]) else fallthrough
    else:
        print("expected a number as an arg")[/font]
It's almost like a try block but I feel like it reads better...

opinions?

here is a link to a full example:

https://github.com/rexlx/ternary_fallthr...example.py
Reply
#2
Not positive that this will do exactly what you want, but pydantic is a library that includes data validation and may be of use to you. See Pydantic Validators
Reply
#3
thanks, that an interesting library ive never heard of.

Fallthroughs exist in other languages, it just means to perform the next condition provided a criteria is met. Not sure if you looked at the full example, but it essentially offloads the issue at hand immediately. If i set it to False (as you would in a traditional ternary operation), I have to return that, test for it, and decide what to do with that data.
Reply
#4
I do not understand your example, or the linked full example, at all.
buran likes this post
Reply
#5
(May-05-2021, 06:22 PM)deanhystad Wrote: I do not understand your example, or the linked full example, at all.

A fallthrough statement transfers control to the next case. in this case, the else clause that says "you either didnt give me an arg, or it wasn't an integer. So theres absolutely no reason for the program to carry on after that. The fallthrough doesnt have to end a program, it just tells the program to go ahead and jump to the next case

It's a principle from go for case switches. I think it would work wonderfully in python conditionals.
Reply
#6
first of all int(sys.argv[1]) if int(sys.argv[1] doesn't make sense at all. Also note that it will fail if sys.argv[1] is '0', because 0 is False

And I disagree with

(May-05-2021, 12:57 PM)rexrf Wrote: It's almost like a try block but I feel like it reads better...

EAFP
: “it’s easier to ask for forgiveness than permission vs LYBL, Look before you leap
see
https://devblogs.microsoft.com/python/id...rsus-lbyl/

(May-05-2021, 06:34 PM)rexrf Wrote: A fallthrough statement transfers control to the next case.

Still unclear what the next case is... If sys.argv[1] cannot be converted to int (and raise an exception) or even if it doesn't raise an exception there is no other case that I see.

Finally - I understand that your example is more general, but just to mention that using sys.argv is the most primitive CLI arguument handling. There is argparse or even third party packages like click that make handling, parsing and validation of CLI arguments easy.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
this isnt about parsing args or using modules lol, also i forgot to do a commit that attempts to explain things better in example.py, should be up to date.

heres what is happening:
val = int(sys.argv[1]) if int(sys.argv[1]) else fallthrough

1. val will be converted into an int

IF

The supplied arg is indeed an int. any type that is not an int will fail otherwise

IN WHICH CASE

fallthrough to the next case, which handles the event now.

It's error handling with a single word. And when you supply this program with a zero, it will just not make any matches, which isn't an error at all. the program is running as expected.

Look people, i didnt invent the idea, ken thompson (you know the guy that cowrote UNIX and C) did, so i guess if you're smarter than ken thompson, sure. this doesnt make any sense.
Reply
#8
(May-05-2021, 07:02 PM)rexrf Wrote: heres what is happening:
val = int(sys.argv[1]) if int(sys.argv[1]) else fallthrough
if sys.argv[1] is not convertable to int it will not reach the if part at all because you will get exception.
Not to mention that converting to int twice is redundant. try/except is much cleaner and idiomatic.

As a side note, the 57 lines in your example can be shortned to ~ 20 if done properly - there is a lot of redundant code
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
I think the examples do not do a good job describing the idea. It is clear to you, but not so clear to those lacking your context.
ndc85430 and buran like this post
Reply
#10
You are simply not reading what I'm writing.

Im converting the int only once, im testing that it is indeed and int the second go.

It says convert the int ONLY if it IS one. otherwise, move on with your life. It's a functional paradigm so if you are an OO advocate, it might be new to you.

It exists in other languages and people understand it just fine. This is not some new radical idea

heres rational people talking about it casually

https://stackoverflow.com/questions/4082...ugh-or-not
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python on a ternary computer Skaperen 0 1,561 Jun-20-2020, 02:21 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020