On 2014-11-30, at 12:30 , ThomasTheDjangoFan 
<[email protected]> wrote:

> Hi guys,
> 
> coming from php I am wondering if there is a way to do something like this in 
> Python/Django:
> 
> if variable = get_a_value_from_function():
>   new_stuff = variable
> 
> Of course I can use
> 
> variable = get_a_value_from_function()
> if variable:
>   new_stuff = variable
> 
> But is there a shortcut similar to php?

Not as such. Python intentionally didn't include assignment within
statements (mostly conditionals) to avoid the common issue of
assignments-instead-of-equality bugs.

If `new_stuff` has a default value, you could always write.

    new_stuff = some_function() or new_stuff

which will reassign `new_stuff` to itself if `some_function()` returns a
falsy value. If you need a more complex conditional body than just an
assignment, you'll need the long one.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/E49110AF-E68A-43C6-8E29-11618328B2BD%40masklinn.net.
For more options, visit https://groups.google.com/d/optout.

Reply via email to