On Sunday, 30 November 2014 18:23:22 UTC+2, Masklinn wrote:
>
>
> On 2014-11-30, at 12:30 , ThomasTheDjangoFan <
> stefan.eich...@googlemail.com <javascript:>> 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.
>
 
Or, if new_stuff does not already exist, and you want to create a 
fallback/default 
value for it, you could use:

    new_stuff = some_function() or "foo"

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/3fd58f03-7d37-403f-b186-af57c35f38b0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to