# How to define a set literal like Python?

**URL:** https://discourse.julialang.org/t/how-to-define-a-set-literal-like-python/25352
**Category:** General Usage
**Tags:** question
**Created:** [June 17, 2019, 1:21am UTC](https://discourse.julialang.org/t/how-to-define-a-set-literal-like-python/25352 "2019-06-17T01:21:24Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [June 17, 2019, 1:21am UTC](https://discourse.julialang.org/t/how-to-define-a-set-literal-like-python/25352/1 "2019-06-17T01:21:24Z")

</div>

In Python, we could write a set by `{1, 2, 3}`. In Julia, we have to write `Set([1, 2, 3])`. Is there a way to define a set literal the same way as Python?

---

<div class="post-metadata">

### Author: ![Dietmar\_Oelz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dietmar_oelz/32/8526_2.png) [@Dietmar\_Oelz](https://discourse.julialang.org/u/Dietmar_Oelz)
#### Post date: [June 17, 2019, 1:47am UTC](https://discourse.julialang.org/t/how-to-define-a-set-literal-like-python/25352/2 "2019-06-17T01:47:15Z")

</div>

If you define

`Set(c...)=Set([c...])`

then you can write  
`Set(1,2,3)`

---

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [June 17, 2019, 1:48am UTC](https://discourse.julialang.org/t/how-to-define-a-set-literal-like-python/25352/3 "2019-06-17T01:48:38Z")

</div>

Thanks, I have thought about

```julia
{}(args...) = Set([args...])

```

But that’s not a valid syntax.

---

<div class="post-metadata">

### Author: ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)
#### Post date: [June 17, 2019, 2:10am UTC](https://discourse.julialang.org/t/how-to-define-a-set-literal-like-python/25352/4 "2019-06-17T02:10:08Z")

</div>

Well you could use

```julia
macro s_str(x)
     @assert x[1] == '{' && x[end] == '}'
     :(Set([$(esc(Meta.parse(x[2:(end - 1)])))...]))
end

```

but `s"{1, 3}"` isn’t much of an improvement over `Set([1, 3])`

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [June 17, 2019, 4:24am UTC](https://discourse.julialang.org/t/how-to-define-a-set-literal-like-python/25352/5 "2019-06-17T04:24:42Z")

</div>

Note that redefining `Set()` in this way is type piracy and is likely to have unforeseen consequences (like silently breaking completely unrelated code!) For more information, see: [Style Guide · The Julia Language](https://docs.julialang.org/en/v1/manual/style-guide/index.html#Avoid-type-piracy-1)

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [June 17, 2019, 9:33am UTC](https://discourse.julialang.org/t/how-to-define-a-set-literal-like-python/25352/7 "2019-06-17T09:33:39Z")

</div>

You can probably overload `braces` and/or `bracescat`.  
They are the special-forms for those expressions, and they get translated to by the parser.  
See [https://github.com/JuliaLang/julia/pull/22942](https://github.com/JuliaLang/julia/pull/22942)

Not tested but I think:

```julia
Base.braces(args...) = Set([args...])

```

I would not do that, but you can do it.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [June 17, 2019, 2:30pm UTC](https://discourse.julialang.org/t/how-to-define-a-set-literal-like-python/25352/8 "2019-06-17T14:30:42Z")

</div>

> [@oxinabox](#):
>
> You can probably overload `braces` and/or `bracescat` .  
> They are the special-forms for those expressions, and they get translated to by the parser.

Those are expression heads in the AST, not functions that you can overload. You could define a macro (after installing [MacroTools.jl](https://github.com/MikeInnes/MacroTools.jl)):

```julia
import MacroTools
macro sets(expr)
    esc(MacroTools.postwalk(expr) do ex
        Meta.isexpr(ex, :braces) ? :(Set($(Expr(:tuple, ex.args...)))) : ex
    end)
end

```

and then you can do, for example:

```julia
julia> @sets {3,4,5} ∪ {3,5,6}
Set([4, 3, 5, 6])

```

Defining a macro like this to locally modify syntax is safe and can be convenient if you need domain-specific expressions. I wouldn’t do it merely to make Julia syntax look like Python syntax, however — if you are programming in Julia, you should use Julia idioms.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [June 17, 2019, 2:46pm UTC](https://discourse.julialang.org/t/how-to-define-a-set-literal-like-python/25352/9 "2019-06-17T14:46:47Z")

</div>

But to write an array in python, you need to write `array([1, 2, 3, 4])`. Since arrays are more commonly used than sets, have you asked them to change their syntax to be more like Julia’s?

---

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [June 17, 2019, 9:02pm UTC](https://discourse.julialang.org/t/how-to-define-a-set-literal-like-python/25352/10 "2019-06-17T21:02:31Z")

</div>

No, I haven’t. Let me clarify myself: When I am studying a language, I want to know whether I can do the same thing as the language I have learned. I just want to explore what Julia can do to understand it better, which does not mean I think Python is superior to Julia. I ask this question does not mean I really want to write a set literal. But I am still grateful to all the help you gave, thank you!

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [June 17, 2019, 9:51pm UTC](https://discourse.julialang.org/t/how-to-define-a-set-literal-like-python/25352/11 "2019-06-17T21:51:48Z")

</div>

> [@singularitti](#):
>
> When I am studying a language, I want to know whether I can do the same thing as the language I have learned.

You already knew that you could _do_ the same thing, because you knew you could write `Set([1, 2, 3])`. Asking to do the same thing with the _same syntax_ as your old language, on the other hand, has limited value in learning a new language — you should generally strive to learn the native idioms.
