# Is there a Julia equivalent for this Python syntax

**URL:** <https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 23, 2021, 1:24pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439 "2021-06-23T13:24:19Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![james-garfield](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/james-garfield/32/26409_2.png) [@james-garfield](https://discourse.julialang.org/u/james-garfield)\
**Post date:** [June 23, 2021, 1:24pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/1 "2021-06-23T13:24:19Z")

</div>

I like how in python I can set a variable in a function as None if I don’t always need to use it. And if the function does end up being called with that variable passed in I can check in the function body by doing:

```julia
id = id or str(uuid.uuid4())[0:8]

```

In Julia however I do:

```julia
if id === nothing
     id = string(UUIDs.uuid4())[1:8] 
else
     id = id
end

```

Is there a better way to do this?

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [June 23, 2021, 1:28pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/2 "2021-06-23T13:28:48Z")

</div>

```julia
id = (id === nothing) ? string(UUIDs.uuid4())[1:8] : id

```

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [June 23, 2021, 1:29pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/3 "2021-06-23T13:29:21Z")

</div>

This one?

```julia
(id === nothing) ? string(UUIDs.uuid4())[1:8] : id

```

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [June 23, 2021, 1:29pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/4 "2021-06-23T13:29:31Z")

</div>

`id = (id === nothing ? string(UUIDs.uuid4())[1:8] : id)`

Edit: You don’t even need the parantheses, they’re just for readability.

---

<div class="post-metadata">

**Author:** ![james-garfield](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/james-garfield/32/26409_2.png) [@james-garfield](https://discourse.julialang.org/u/james-garfield)\
**Post date:** [June 23, 2021, 1:30pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/5 "2021-06-23T13:30:54Z")

</div>

That works thanks!

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [June 23, 2021, 1:32pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/6 "2021-06-23T13:32:50Z")

</div>

A (less clear) alternative would be to use short-circuit evaluation:

```julia
(id === nothing) && (id = string(UUIDs.uuid4())[1:8])

```

Edit: added parenthesis - thanks @GunnarFarneback

---

<div class="post-metadata">

**Author:** ![fonsp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fonsp/32/222349_2.png) [@fonsp](https://discourse.julialang.org/u/fonsp)\
**Post date:** [June 23, 2021, 2:28pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/7 "2021-06-23T14:28:06Z")

</div>

There is also `something`

```julia
id = something(id, string(UUIDs.uuid4())[1:8])

```

though it can have worse performance: it will also generate a uuid when `id` is not `nothing` ☹

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 23, 2021, 2:36pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/8 "2021-06-23T14:36:45Z")

</div>

> [@lungben](#):
>
> A (less clear) alternative would be to use short-circuit evaluation:
> 
> ```julia
> (id === nothing) && id = string(UUIDs.uuid4())[1:8]
> 
> ```

That one requires parenthesis to the right of `&&` though:

```julia
id === nothing && (id = string(UUIDs.uuid4())[1:8])

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [June 23, 2021, 3:41pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/9 "2021-06-23T15:41:24Z")

</div>

> [@fonsp](#):
>
> though it can have worse performance: it will also generate a uuid when `id` is not `nothing` ☹

In Julia 1.7, there is an `@something` macro, which does not have these problems, so you can write this as

```julia-auto
@something(id, string(UUIDs.uuid4())[1:8])

```

without worrying about performance or side-effects.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [June 23, 2021, 3:51pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/10 "2021-06-23T15:51:33Z")

</div>

It is probably safer to use the `isnothing(id)` method, in case the behavior of `===` or `nothing` ever changes.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [June 23, 2021, 3:57pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/11 "2021-06-23T15:57:31Z")

</div>

Going back to the code in the question, you can just do

```julia
if id === nothing
     id = string(UUIDs.uuid4())[1:8] 
end

```

(no need for `else id = id`).

But depending on your use case you might want to do simply

```julia
function f(id = string(UUIDs.uuid4())[1:8])
   ...
end

```

In Python this doesn’t work because the same UUID would be used as default for all calls to `f`.

In Julia it works because the default expression is evaluated each time `f` is called without a value.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [June 23, 2021, 4:07pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/12 "2021-06-23T16:07:37Z")

</div>

That seems overly defensive, there is definitely no chance that will change within 1.x. Using `===` is actually preferable prior to Julia 1.7, because it let the compiler reason better about type constraints.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [June 23, 2021, 4:16pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/13 "2021-06-23T16:16:06Z")

</div>

Intersting. That begs the question, why `isnothing()` exists.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [June 23, 2021, 4:44pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/14 "2021-06-23T16:44:36Z")

</div>

That’s a reasonable question. It’s mostly useful for higher-order functions like in `filter(isnothing, x)`, but now that we have `==(nothing)`, it might not be that useful anymore.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [June 23, 2021, 7:02pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/15 "2021-06-23T19:02:17Z")

</div>

> [@sijo](#):
>
> you might want to do simply
> 
> ```julia
> function f(id = string(UUIDs.uuid4())[1:8])
> ...
> end
> 
> ```
> 
> In Python this doesn’t work because the same UUID would be used as default for all calls to `f` .

That one must be marked as the solution, I guess.

The usual python `x = x or something` / `if x is None` idiom is an ugly workaround existing only because the default argument for a function is a value in Python (while Julia inherits and improves upon Lisp behavior where a default written as a function call is an expression).

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 23, 2021, 7:18pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/16 "2021-06-23T19:18:43Z")

</div>

> [@Vasily\_Pisarev](#):
>
> improves upon Lisp behavior where a default written as a function call is an expression

Out of curiosity, what is the improvement that Julia adds to the Lisp behavior?

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [June 23, 2021, 8:46pm UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/17 "2021-06-23T20:46:15Z")

</div>

In Common Lisp, the behavior of mutable default value written as literals is implementation dependent.

```nohighlight
(defun foo-literal (x &optional (y #(0)))
  (incf (aref y 0) x)
  (aref y 0))

```

in SBCL, when called with one argument, mutates the array set as literal (and gives a warning about that on compilation).

```nohighlight
(defun foo-literal (x &optional (y (make-array 1 :initial-element 0)))
  (incf (aref y 0) x)
  (aref y 0))

```

when called with one argument, creates a new array each time.

In Julia, the expression for the default value is always treated as if it is a function call which is a plus IMO.

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [June 25, 2021, 12:47am UTC](https://discourse.julialang.org/t/is-there-a-julia-equivalent-for-this-python-syntax/63439/18 "2021-06-25T00:47:09Z")

</div>

Lots of answers here, but unless I’m misunderstanding this is a classic situation for multiple dispatch:

```julia
check_id(id::Nothing) = nothing
check_id(id) = string(UUIDs.uuid4())[1:8]

```

Now just use `check_id` wherever you want. If `id` is nothing it returns `nothing`. If `id` is anything else it returns your `UUIDs` construct. This will run _really_ fast and is type-stable.

The `condition ? (x = x) : (x = y)` construct on the other hand may result in the compiler being unable to infer the type of `x` until run-time which depending on how your code is structured could really slow things down.
