Python Forum

Full Version: assignment embedded in an expression
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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?
Very unpythonish:

done = False
if test1():
    send1()
    done = True

if done == false and test2():
    send2()
    done = True

if done == false
    temp = makeit()
    if testit(temp):
         sendit(temp)
         done = True

...
   
or

while True:
    if test1():
         send1()
         break;

    ....

    temp= makeit()
    if testit(temp)
         sendit(temp)
         break
    ....
I can't think of any clever, elegant solution. What I would likely do is, when you reach the problem branch, make it an else and do everything else you need in there. Would love to know if anyone has a better solution to this problem though.
my "solution" is to suggest adding the capability to Python.  i had ass-u-me-d that since a = b = 6 would work, then assignment in an expression would work.  that is how C was able to stuff like a = b = 6.
Python special-cases that. They don't allow it in C because it was abused and made so much code unreadable.
it is allowed in C.  i do it a lot.

it has worked in Python.  i usually use it for initialization of two or more variables to the same value, typically None.  in C typically 0 or NULL.
Not sure how I screwed that up, meant, "They don't allow it in Python because in C it was abused and made so much code unreadable." What do you mean "it has worked" in Python? You can combine multiple assignments, as you showed, but it's not an expression. Which is what you're asking for here, and which GvR would never do.
Quote
    "it’s only newcomers who express a strong desire to add this to the language"
See
    http://effbot.org/pyfaq/why-can-t-i-use-...ession.htm
what i have done before is stuff like a = b = c = None.  but i was afraid of doing a = b = c = {} out of fear that a, b, and c would all be referencing the same dictionary.
(Nov-24-2017, 01:50 AM)Skaperen Wrote: [ -> ]but i was afraid of doing a = b = c = {} out of fear that a, b, and c would all be referencing the same dictionary
It would dot that:
>>> a = b = c = {}
>>> id(a)
8350192
>>> id(b)
8350192
>>> id(c)
8350192
Easy fix:
>>> a, b, c = [{} for i in range(3)]
>>> id(a)
8358720
>>> id(b)
52826352
>>> id(c)
8358672

>>> help(id)
Help on built-in function id in module builtins:

id(obj, /)
    Return the identity of an object.    
    This is guaranteed to be unique among simultaneously existing objects.
    (CPython uses the object's memory address.)
Pages: 1 2