# Writing an equivalent Julia mutable struct that have similar functionality of a Python class

**URL:** <https://discourse.julialang.org/t/writing-an-equivalent-julia-mutable-struct-that-have-similar-functionality-of-a-python-class/115406>\
**Category:** General Usage\
**Created:** [June 10, 2024, 2:18am UTC](https://discourse.julialang.org/t/writing-an-equivalent-julia-mutable-struct-that-have-similar-functionality-of-a-python-class/115406 "2024-06-10T02:18:36Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [June 10, 2024, 4:19am UTC](https://discourse.julialang.org/t/writing-an-equivalent-julia-mutable-struct-that-have-similar-functionality-of-a-python-class/115406/4 "2024-06-10T04:19:44Z")

</div>

This seems very similar to your prior question:

> [@Equivalent Julia code for a Python class with counter variables](https://discourse.julialang.org/t/equivalent-julia-code-for-a-python-class-with-counter-variables/115385/2):
>
> This is one possibility: mutable struct Student gpa::Int record::Dict{Any, Any} name::String end let counter = 0 global function Student() counter += 1 return Student(0, Dict(), "Student $counter") end end

It would be great if you could attempt the Julia solution given the prior knowledge and show us your attempt in the future.

```julia-repl
julia> let counter = 0
           global Foo
           Base.@kwdef mutable struct Foo
               bar::Bool
               baz::Any
               counter::Union{Int, Nothing}
               dict::Dict
           end
               function Foo(;bar=true, dict=Dict{Foo,Int}())
                   self = Foo(bar, nothing, nothing, dict)
                   if bar
                       self.dict = Dict(self => 1)
                       self.counter = counter
                       counter += 1
                   end
                   return self
               end
       end
Foo

julia> Base.show(io::IO, f::Foo) =
           print(io, """
           Foo(
               bar=$(f.bar),
               baz=$(f.baz),
               dict=$(f.bar ?
                   "Dict(#=circular reference=# => 1)" :
                   f.dict
               ),
               counter=$(f.counter)
           )""")

julia> foo1 = Foo()
Foo(
    bar=true,
    baz=nothing,
    dict=Dict(#=circular reference=# => 1),
    counter=0
)

julia> custom_dict = Dict('a' => 1, 'b' => 2)
Dict{Char, Int64} with 2 entries:
  'a' => 1
  'b' => 2

julia> foo2 = Foo(bar=false, dict=custom_dict)
Foo(
    bar=false,
    baz=nothing,
    dict=Dict('a' => 1, 'b' => 2),
    counter=nothing
)

julia> foo3 = Foo(); @show foo3;
foo3 = Foo(
    bar=true,
    baz=nothing,
    dict=Dict(#=circular reference=# => 1),
    counter=1
)

```

---

_[View the full topic](https://discourse.julialang.org/t/writing-an-equivalent-julia-mutable-struct-that-have-similar-functionality-of-a-python-class/115406)._
