Python Forum
assignment embedded in an expression
Thread Rating:
  • 3 Vote(s) - 2.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
assignment embedded in an expression
#1
i want to call a function and assign the returned value to a variable, while this value is being tested.  if a condition is met, then that value will be used in the clause the condition causes to be executed.  if this were a simple if statement, then the function could be called before the if statement like:
    temp = makeit()
    if testit(temp):
        sendit(temp)
but here is the catchone, the makeit() function is non-idempotent, which means i cannot make extra calls to it (it has side effects). and, two, this test is part of a sequence of elif statements where the previous tests must be done before this one.  if things worked like they do in C then i would be inclined to do it like this:
    if test1():
        send1()
    elif test2():
        send2()
    elif testit( (temp=makeit()) ):
        sendit(temp)
    elif test4():
        send4()
    else:
        send5()
but python does not like the assignment embedded in an expression, as is needed in the third test and code clause above.  any suggestions?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
assignment embedded in an expression - by Skaperen - Nov-22-2017, 08:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pass results of expression to another expression cmdr_eggplant 2 2,382 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  assignment: not an operator nor expression, but x=y=z=3 works fine? jefdaels 1 2,268 Jan-29-2019, 02:19 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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