Why is the result after this function is called like this?

I posted the code to the second floor.sorry.

My confusion is that if with_if_function() is called in the end, the output will be**
42
47
instead of outputting
47

def if_function(condition, true_result, false_result):
“”“Return true_result if condition is a true value, and
false_result otherwise.
>>> if_function(True, 2, 3)
2
>>> if_function(False, 2, 3)
3
>>> if_function(3==2, 3+2, 3-2)
1
>>> if_function(3>2, 3+2, 3-2)
5
“””
if condition:
return true_result
else:
return false_result

def with_if_statement():
“”"
>>> result = with_if_statement()
47
>>> print(result)
None
“”"
if cond():
return true_func()
else:
return false_func()

def with_if_function():
“”"
>>> result = with_if_function()
42
47
>>> print(result)
None
“”"
return if_function(cond(), true_func(), false_func())

def cond():
return False

def true_func():
print(42)

def false_func():
print(47)

My confusion is that if with_if_function() is called in the end, the output will be**
42
47
instead of outputting
47

def if_function(condition, true_result, false_result):
    """Return true_result if condition is a true value, and
    false_result otherwise.
    >>> if_function(True, 2, 3)
    2
    >>> if_function(False, 2, 3)
    3
    >>> if_function(3==2, 3+2, 3-2)
    1
    >>> if_function(3>2, 3+2, 3-2)
    5
    """
    if condition:
        return true_result
    else:
        return false_result


def with_if_statement():
    """
    >>> result = with_if_statement()
    47
    >>> print(result)
    None
    """
    if cond():
        return true_func()
    else:
        return false_func()

def with_if_function():
    """
    >>> result = with_if_function()
    42
    47
    >>> print(result)
    None
    """
    return if_function(cond(), true_func(), false_func())

def cond():
    "*** YOUR CODE HERE ***"
    return False

def true_func():
    "*** YOUR CODE HERE ***"
    print(42)

def false_func():
    "*** YOUR CODE HERE ***"
    print(47)

Your with_if_function returns the return value from either the true or false function.

But neither true_func() nor false_func() have a return statement. That means both of them simply return None.

While they are executing, both will print a value to the console, but that value is not seen by the calling function.

Perhaps you just want to convert the print()s in those functions to returns.

2 Likes

thank you so much.actually, this topic requires me to print out the numbers in the order of 42 47 when calling the function with_if_function, the code I posted is the answer written by someone else, because it is stuck in return and print here so I came to ask, thank you again for your guidance, now I understand. (confused by the side effects of print XD)

Instead of

you can achieve the same result with just

return bool(condition)

In fact, this is exactly what bool() does, merely under another name, so you don’t need a function for it.

1 Like

return boot(condition)

Did you mean bool?

By the way, most of your replies – but not all of them – break threading as emails. They show up as an independent email rather than in the same thread as the rest of the responses.

I presume you are replying via the web forum, so it is hard to imagine what you are doing different from everyone else, but apparently you must be.

Actually most (but not all!) replies have broken threading, but yours are even more broken than other people.

(The most frustrating thing about Discuss’ broken email support is that it is inconsistantly broken.)

1 Like

thank you so much! :grinning:

Yes, sorry, I fat-fingered it. Thanks for the catch. I edited my original post to fix it (as I can, since I’m not a email user like you :P)

Oh, I’m just clicking the secret “Break threading for email users” checkbox in the web UI on any threads you participate in, at least whenever I remember. Apparently other people don’t have as strong a vendetta against troglodytes email users like yourself as I do).

In all seriousness, if I were to guess, it might be because I tend to frequently use the “Reply” button to reply to specific posts rather than to the topic as a whole, which your email client interprets as a separate thread. To attempt to test that hypothesis, this message is sent as a reply to your post, and I’ll send another as a reply to the thread.

This is a reply to the topic as a whole, rather than to your specific message.

By C.A.M. Gerlach via Discussions on Python.org at 29Mar2022 20:41:

In all seriousness, if I were to guess, it might be because I tend to
frequently use the “Reply” button to reply to specific posts rather
than to the topic as a whole, which your email client interprets as a
separate thread. To attempt to test that hypothesis, this message is
sent as a reply to your post, and I’ll send another as a reply to the
thread.

As an email user, what we see of late isn’t that the replies are not in
the email thread, but they are all replies to the original post (in
terms of the “In-Reply-To:” header).

Your direct-reply-to-the-specific-post is what I would like everyone to
do, and I’m quite disappointed that discourse gets the email-side
threading linkage wrong.

Especially galling in that a year ago(?) it worked correctly. Actually,
even earlier this month. Here’s a well(-ish) linked thread in my python
folder:

21Mar2022 10:04 Cameron Simpson - ┌>[Py]  discourse-feedback, discuss.python.org 8.7K
21Mar2022 09:42 Cameron Simpson r ├>[Py]  discourse-feedback, discuss.python.org 6.5K
20Mar2022 15:40 Cameron Simpson - │ ┌>[Py discourse-feedback, discuss.python.org 9.7K
20Mar2022 14:57 C.A.M. Gerlach  - │┌┴>[Py discourse-feedback, discuss.python.org  12K
20Mar2022 15:35 Cameron Simpson r │├>[Py] discourse-feedback, discuss.python.org 6.3K
20Mar2022 12:26 fungi via Discu r ├┴>[Py] discourse-feedback, discuss.python.org 7.0K
20Mar2022 12:11 fungi via Discu - ├>[Py]  discourse-feedback, discuss.python.org 6.7K
20Mar2022 10:08 C.A.M. Gerlach  - ├>[Py]  discourse-feedback, discuss.python.org 5.7K

That’s the “What I miss here coming from users.rust-lang.org” topic from
a week ago. Your own post in the middle is definitely written from the
forum, and seems suitably threaded.

Whereas this is the threading in the current topic:

30Mar2022 07:41 C.A.M. Gerlach  - ┌>[Py] [Users] Why is the result discuss-users 4.3K
30Mar2022 07:41 C.A.M. Gerlach  r ├>[Py] [Users] Why is the result discuss-users 8.3K
29Mar2022 20:59 Proby626 via Di - ├>[Py] [Users] Why is the result discuss-users 4.6K
29Mar2022 19:49 Steven D'Aprano - ├>[Py] [Users] Why is the result discuss-users 6.1K
29Mar2022 18:29 C.A.M. Gerlach  - ├>[Py] [Users] Why is the result discuss-users 6.3K
29Mar2022 18:19 Proby626 via Di - ├>[Py] [Users] Why is the result discuss-users 4.7K
29Mar2022 17:54 BowlOfRed via D - ├>[Py] [Users] Why is the result discuss-users 5.2K
29Mar2022 17:10 Proby626 via Di - ├>[Py] [Users] Why is the result discuss-users 6.1K
29Mar2022 17:04 Proby626 via Di - ┴>[Py] [Users] Why is the result discuss-users 7.6K

(The subject section looks awful because I narrowed my window to make
the listing fit, and it sems I prioritise the label over the subject in
the index format.)

So I’m not sure what’s going on, only that of late, most posts look
unthreaded (as replies to the root topic message) instead of well
threaded (as replies to the antecedant post). And I’m pretty sure it
used to be better in the past.

Cheers,
Cameron Simpson cs@cskk.id.au

I might be wrong, but from what you and @steven.daprano have said, and on my strong habit of clicking the reply button on individual messages rather than the thread, it sounds like combined with whatever Discourse is doing, it seems either your email clients are configured/behaving differently, or your expectations on what is the “right” behavior differ. It seems like Steven is saying that he’s seeing my replies as separate threads,

Whereas you’re just seeing them in one big thread versus individual sub-threads.

So it would seem that perhaps some combination of the following might be occurring:

  • Your mail client is arranging them into nested sub-threads within the same thread, and/or one big thread, whereas Steven’s is breaking them out into seperate threads
  • You want to have replies in nested sub-threads, whereas Steven wants to see them in one flat thread

As for what could be changing thread to thread, aside from Discourse updates and different patterns of users replying on different threads, it seems at least possible that it could relate to how email users’ mail clients are formatting (or mangling) the headers and message body that affects whether Discourse detects it as a threaded reply or not.

As a possibly related sidenote, I’ve noticed in the past week-ish or so, your messages replying to others have been prefixed with

whereas normally Discourse knows to strip it, and I can’t recall seeing it on other people’s messages (including @steven.daprano ). Not sure if its related, and not sure if its something that changed on your end or Discourse’s, but wanted to point that out.

By C.A.M. Gerlach via Discussions on Python.org at 29Mar2022 22:54:

I might be wrong, but from what you and @steven.daprano have said, and
on my strong habit of clicking the reply button on individual messages
rather than the thread,

Interestingly, this reply of yours is shown as a reply to my message.

it sounds like combined with whatever Discourse is doing, it seems
either your email clients are configured/behaving differently, or your
expectations on what is the “right” behavior differ. It seems like
Steven is saying that he’s seeing my replies as separate threads,

Interesting. I use mutt. It isn’t apparent to me what Steven is using.

Whereas you’re just seeing them in one big thread versus individual sub-threads.

Let’s take this message of yours to which I’m replying. Amongst its
headers are these:

Indeed, as I clicked “reply” on it.

This is the end of your post for me; I imagine Discourse interpreted the headers literally, as a new message. Maybe you could enclose them within a verbatim block to avoid that.

By C.A.M. Gerlach via Discussions on Python.org at 30Mar2022 00:39:

Indeed, as I clicked “reply” on it.

This is the end of your post for me; I imagine Discourse interpreted
the headers literally, as a new message. Maybe you could enclose them
within a verbatim block to avoid that.

Indeed. I had a heap of stuff in there. I usually use the markdown
indent-4-spaces for this, but Discourse is extremely agressive in
mangling inbound email posts. You can see I’ve altered my attribution
line above from the normal convention because Discourse was eating those
(and below) also.

Let’s try that again, using backticks…

Let’s take this message of yours to which I’m replying. Amongst its
headers are these:

And again Discourse eats my email :frowning: I am reduced to using the web forum, how crude.

Let’s try for a third time; here’s my second attempt from the email:

Indeed. I had a heap of stuff in there. I usually use the markdown
indent-4-spaces for this, but Discourse is extremely agressive in
mangling inbound email posts. You can see I’ve altered my attribution
line above from the normal convention because Discourse was eating those (and below) also.

Let’s try that again, using backticks…

Let’s take this message of yours to which I’m replying. Amongst its
headers are these:

Date: Tue, 29 Mar 2022 22:54:16 +0000
From: "C.A.M. Gerlach via Discussions on Python.org" <notifications@python1.discoursemail.com>
Subject: [Py] [Users] Why is the result after this function is called like this?
To: cs@cskk.id.au
Message-ID: <topic/14680/51080.c10d6dc3ade4bb10054c3983@discuss.python.org>
In-Reply-To: <YkOHlzi270l8fQMv@cskk.homeip.net>
References: <topic/14680@discuss.python.org> <YkOHlzi270l8fQMv@cskk.homeip.net> <20220329083928.GX8276@ando.pearwood.info>
Delivered-To: cs@cskk.id.au
Reply-To: "Discussions on Python.org" <incoming+f96b28c86ef0bc2a400f14b8379d85a8@python1.discoursemail.com>

The To: comes from discourse’s delivery to me. The Message-ID: comes
from discourse. The In-Reply-To: comes from my original message to
which you’re replying. And the References: contains message-ids for:

  • the topic (the root post probably)
  • my message (because you’re replying to me?)
  • Steven’s message (because you’re quoting him?)

Now, mutt can thread based on either Message-ID: or References: and
what I see may be a result of its decisions here about who the right
ancestor is.

Scrolling back through the thread, all the Message-ID: headers have
discourse style message-of-topic ids in them, examples:

  • C.A.M. Gerlach Message-ID: <topic/14680/51080.c10d6dc3ade4bb10054c3983@discuss.python.org>
  • C.A.M. Gerlach Message-ID: <topic/14680/51042.722d935c35c6628c1698dca5@discuss.python.org>
  • Cameron Simpson Message-ID: <topic/14680/51078.4cf65126fd51434cae3be032@discuss.python.org>
  • C.A.M. Gerlach Message-ID: <topic/14680/51043.f1a2612a93416948402ce94e@discuss.python.org>
  • Proby626 Message-ID: <topic/14680/50979.dae0abe13a6e660527fa3cd3@discuss.python.org>
  • Steven D’Aprano Message-ID: <topic/14680/50971.20e6177fd66ebc10cf95a7f0@discuss.python.org>

and yet the In-Reply-To: cites the message-id of the source message,
which for a forum user may match up but for an email user is their home
domain based message-id such as pearwood for Steven and cskk for me.
And those messages are not present with those message-ids here - they
have discourse generated message-ids.

Anyway, IMO the bug may be that the primary In-Reply-To: head cites
the source (author’s) message-id instead of the discourse based
message-id (which is the only one the end user receives). So messages
are not matched up.

If Steven’s mailer does not peer at the References: header (which is
for USENET, not email) then he might well (and legitimately) consider
these unthreaded because the in-reply-to does not cite a message-id in
his mail folder.

This strikes me as a discourse bug. Whom do I pester about it?

Gateways such as email->discourse should preserve the message-id
specificly so that this works. There’s no technical reason discourse
can’t do this.

Cheers,
Cameron Simpson cs@cskk.id.au

Here you go:

By C.A.M. Gerlach via Discussions on Python.org at 30Mar2022 07:02:

Here you go:
Create Bug Reports for Discourse on Meta - users - Discourse Meta

Thanks. - Cameron

Thanks for doing that digging Cameron.

I too am using mutt, as far as I know using the default settings for threading. When I say they appear unthreaded, I mean that in thread view all messages appear as second-level replies to a non-existent first level message. For example:

┬─>[Py] [Users] Sieve of Eratosthenes in Python
├─>[Py] [Users] Sieve of Eratosthenes in Python
├─┬─>[Py] [Users] Sieve of Eratosthenes in Python
│ └─>[Py] [Users] Sieve of Eratosthenes in Python
├─>[Py] [Users] Sieve of Eratosthenes in Python
└─>[Py] [Users] Sieve of Eratosthenes in Python

I have no idea why the two middle messages (in my inbox, from Mark Dickinson and myself) are linked. Some artifact of mutt’s threading algorithm, I presume.