Python Forum
assignment embedded in an expression - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: assignment embedded in an expression (/thread-6434.html)

Pages: 1 2


assignment embedded in an expression - Skaperen - Nov-22-2017

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?


RE: assignment embedded in an expression - heiner55 - Nov-22-2017

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
    ....



RE: assignment embedded in an expression - micseydel - Nov-22-2017

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.


RE: assignment embedded in an expression - Skaperen - Nov-23-2017

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.


RE: assignment embedded in an expression - micseydel - Nov-23-2017

Python special-cases that. They don't allow it in C because it was abused and made so much code unreadable.


RE: assignment embedded in an expression - Skaperen - Nov-23-2017

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.


RE: assignment embedded in an expression - micseydel - Nov-23-2017

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.


RE: assignment embedded in an expression - heiner55 - Nov-23-2017

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-an-assignment-in-an-expression.htm


RE: assignment embedded in an expression - Skaperen - Nov-24-2017

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.


RE: assignment embedded in an expression - snippsat - Nov-24-2017

(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.)