# How would you implement or replace Python-like class attributes?

**URL:** <https://discourse.julialang.org/t/how-would-you-implement-or-replace-python-like-class-attributes/34324>\
**Category:** General Usage\
**Tags:** classes\
**Created:** [February 7, 2020, 9:35pm UTC](https://discourse.julialang.org/t/how-would-you-implement-or-replace-python-like-class-attributes/34324 "2020-02-07T21:35:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 7, 2020, 9:35pm UTC](https://discourse.julialang.org/t/how-would-you-implement-or-replace-python-like-class-attributes/34324/1 "2020-02-07T21:35:25Z")

</div>

I don’t expect this to be useful in practice, but what would be the more elegant way to associate _mutable_ values with a type, analogous to class attributes in Python? I thought of this question after reading an example method that could replace _constant_ class attributes by returning a value based on argument type:

```julia
rank(x::General) = 1

```

---

<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:** [February 7, 2020, 9:49pm UTC](https://discourse.julialang.org/t/how-would-you-implement-or-replace-python-like-class-attributes/34324/2 "2020-02-07T21:49:50Z")

</div>

You could use a const `Ref` to hold the data:

```julia
const GeneralRank = Ref(1)
rank(::Type{General}) = GeneralRank[]
rank(x::General) = rank(typeof(x))
set_rank!(::Type{General}, value) = GeneralRank[] = value
etc...

```
