# The is "Expando objects" in Julia?

**URL:** <https://discourse.julialang.org/t/the-is-expando-objects-in-julia/5957>\
**Category:** General Usage\
**Created:** [September 18, 2017, 11:46pm UTC](https://discourse.julialang.org/t/the-is-expando-objects-in-julia/5957 "2017-09-18T23:46:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabomgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabomgp/32/1918_2.png) [@gabomgp](https://discourse.julialang.org/u/gabomgp)\
**Post date:** [September 18, 2017, 11:46pm UTC](https://discourse.julialang.org/t/the-is-expando-objects-in-julia/5957/1 "2017-09-18T23:46:35Z")

</div>

In C# is possible to have a dynamic object with fields defined at runtime using expando objects. Example:

```
dynamic contact = new ExpandoObject();
contact.Name = “Patrick Hines”;
contact.Phone = “206-555-0144”;

```

As you can see, the contact object is dynamic, in the sense we can set fields in the object, without declaring previously the structure of the object. I understand in Julia we can have a parameter/variable without declared type, as in:

```
function setProp1(obj) obj.prop1=1 end

```

But, as i understand it, the obj object must be created from a type with a field prop1. If prop1 is not declared somewhere as a field, setProp1 can’t set dynamically the field prop1. If i want full dynamism, and i want not to worry for performance, there is a way to create “expando objects” in Julia?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 18, 2017, 11:59pm UTC](https://discourse.julialang.org/t/the-is-expando-objects-in-julia/5957/2 "2017-09-18T23:59:29Z")

</div>

Use `Dict`.

---

<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:** [September 19, 2017, 12:02am UTC](https://discourse.julialang.org/t/the-is-expando-objects-in-julia/5957/3 "2017-09-19T00:02:30Z")

</div>

No, this isn’t possible with Julia types. The closest thing would be a dictionary from symbols to values, like:

```julia
contact = Dict{Symbol, String}()
contact[:Name] = "Patrick"
contact[:Phone] = "123-456"

```

If you really, really, really want to access the fields with dot notation, you could write a macro that transforms:

```julia
@expando contact.Name = "Patrick"

```

into the appropriate dictionary syntax:

```julia
contact[:Name] = "Patrick"

```

but this will make your code harder to follow (and won’t improve performance).

---

<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:** [September 19, 2017, 12:07am UTC](https://discourse.julialang.org/t/the-is-expando-objects-in-julia/5957/4 "2017-09-19T00:07:05Z")

</div>

Also, I can’t find much information about benchmarking ExpandoObject, but [this blog](http://spiritofdev.blogspot.com/2011/12/performance-of-c-40-dynamic-vs.html) suggests that their performance is similar to a dictionary anyway. That suggests that you won’t lose performance by switching to using a `Dict` in julia (except to the extent that Julia and C# may implement their dictionaries differently).

---

<div class="post-metadata">

**Author:** ![gabomgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabomgp/32/1918_2.png) [@gabomgp](https://discourse.julialang.org/u/gabomgp)\
**Post date:** [September 19, 2017, 1:56am UTC](https://discourse.julialang.org/t/the-is-expando-objects-in-julia/5957/5 "2017-09-19T01:56:55Z")

</div>

Thanks for the detailed answer. Yes, i’m thinking in dot notation. And sorry for my English, i’m thinking in Dict semantic using dot notation, where i’m not worried by the performance.

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [January 3, 2026, 1:14pm UTC](https://discourse.julialang.org/t/the-is-expando-objects-in-julia/5957/6 "2026-01-03T13:14:39Z")

</div>

I played with the “expando object” idea and found a tiny way (≈30 lines) to get a _Python-like instance experience_ in Julia: dynamic attributes + dynamic “methods” with `obj.f(args...)` syntax, by storing everything in an `EasyConfig.Config` (nested dict) and auto-binding `self` via `getproperty`.

```julia
using EasyConfig

@kwdef mutable struct DynObj
    cfg::Config = Config()
end

_wrap(c::Config) = DynObj(c)
_wrap(x) = x

function Base.getproperty(o::DynObj, name::Symbol)
    name === :cfg && return getfield(o, :cfg)
    c = getfield(o, :cfg)

    if haskey(c, name)
        v = c[name]
        return if v isa Function
            (args...) -> v(o, args...)
        else
            _wrap(v)
        end
    else # create intermediate nodes to allow o.a.b.c = 1
        child = Config()
        c[name] = child
        return DynObj(child)
    end
end

function Base.setproperty!(o::DynObj, name::Symbol, v)
    name === :cfg && throw(ArgumentError("field :cfg is reserved"))
    getfield(o, :cfg)[name] = v isa DynObj ? v.cfg : v
    return v
end

Base.propertynames(o::DynObj; private=false) = private ? (:cfg, keys(o.cfg)...) : (keys(o.cfg)...,)
Base.delete!(o::DynObj, k::Symbol) = (delete!(o.cfg, k); o)

```

Quick demo:

```julia
o = DynObj()
o.x = 1
o.msg = "hello"

o.add = (self, y) -> self.x + y
o.greet = self -> "greet: $(self.msg)"

o.add(5) # 6
o.greet() # "greet: hello"

o.sub.name = "child"
o.sub.say = self -> "I am $(self.name)"
o.sub.say() # "I am child"

```

Caveats:

- performance is _not_ the goal here: this is fully dynamic (Dict + Any + closure binding), so it’s mostly for experimentation / prototyping.
- reading a missing property creates an empty node (nice for `o.a.b.c = ...`, but can hide typos).
- “methods” here are just function values stored in a dict; the auto-binding returns a closure, so it bypasses Julia’s normal multiple dispatch / method tables and may allocate.

Ref: [ANN: EasyConfig.jl: An easy-to-write JSON-ish data structure](https://discourse.julialang.org/t/ann-easyconfig-jl-an-easy-to-write-json-ish-data-structure/50104)
