Invoking return through a function?
Alberto Riva
alb at nospam.chip.org
Sun Oct 29 13:02:33 EDT 2017
On 10/29/2017 12:21 PM, Rick Johnson wrote:
> On Sunday, October 29, 2017 at 9:19:03 AM UTC-5, Alberto Riva wrote:
>> Hello,
>>
>> I'm wondering if there is a way of writing a function that
>> causes a return from the function that called it. To
>> explain with an example, let's say that I want to exit my
>> function if a dict does not contain a given key. I could
>> write:
>>
>> def testFun():
>> ...
>> if key not in dict:
>> return
>> ...
>>
>> But if this is a test I need to do a lot of times, I'd like
>> to replace it with something shorter and more explicit:
>>
>> def testFun():
>> ...
>> checkKey(dict, key)
>> ...
>>
>> and I'd like checkKey to cause a return *from testFun*.
>
> But under what exact _circumstances_?
>
> So, you'd like a call to `checkKey(...)` to cause a return
> in `testFunc` when:
>
> (1) the key exists?
>
> (2) the key doesn't exist?
>
> (3) the dict is actually a list?
>
> (3) The call raises an exception?
>
> (4) a seg fault occurs?
>
> (5) GvR regains his sanity, self-respect and dignity by
> 86'ing type-hints?
In this specific case, I wanted to invoke a return if the key doesn't
exist (as I wrote in my post). But this is not strictly relevant, my
question was more general. To phrase it in yet another way: is there
*any other way* to invoke return on a function, except by explicitly
writing "return" in its body?
> Be more specific please. And _always_ refer to a named
> function by _name_. Never use random or implicit aliases
> (aka: "the function", "my function", etc...) in your prose.
> Both functions *ARE* functions, dontchaknow?
I do, but apparently everybody else was able to understand what I was
asking... :)
>> In a language like Lisp
>
> Python is nothing like Lisp, and for good reason!
I would disagree with this. Actually, it's the most Lisp-like language
I've encountered in my 25 years of writing software. It has closures,
functions are first-class objects, it's interactive, highly reflective,
variables are not strongly typed... The only big thing missing is
macros, which are a direct result of the code-is-data feature of Lisp.
> Sure, we
> have a few lispers and functional fanboys who hang around
> here, and sometimes Rustom can get a little preachy about
> FP, but mostly, we tolerate the fanboyism -- so long as it's
> not rabid fanboyism.
I don't consider myself a fanboy - I've used Lisp for most of my career,
and now I happily use Python for 80% of what I do, even if it means
putting up with the occasional limitation, as in this case.
>> this would be accomplished by defining checkKey as a macro
>> that expands into the code shown in my first example, so
>> that the return would be inside testFun and not insted
>> checkKey. Is there a way of doing something like this in
>> Python?
>
> Thankfully, no.
Why "thankfully"? Having macros in Python would be great. Of course it's
not possible because of the syntax, but it would be great nevertheless :)
>> Another way of phrasing my question is: is there a way to
>> cause a return from a function that is higher up in the
>> call stack, rather than the currently active one, without
>> using try/except?
>
> Possibly. But you've already poo-pooed the easy and obvious
> solution of using conditional logic.
Not really. It's obvious that I'm going to use a conditional, I was just
asking for a way to avoid having to type the same conditional over and
over. Computers are meant to do work for you, after all, aren't they? :)
Alberto
--
E-mail address:
((lambda (s a b c) (map 'string #'code-char (mapcar (lambda (v)
(setq s (+ a (mod (+ s v (- a)) b)))) (map 'list (lambda (v)
(- (char-code v) c)) " 1`-THUZ&+Wh1")))) 97 46 73 32)
More information about the Python-list
mailing list