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

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:

rank(x::General) = 1

You could use a const Ref to hold the data:

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