0

Is there a way in python to create a data container with numeric field names?

A minimal example: I have variables called beta0, beta1, beta2 and would like to pack them into a container beta and access them by beta.0, beta.1, beta.2.

I checked recordclass, namedtuple, and dataclass, but none of them allows this.

EDIT: What is the best practice in python for treating a bunch of related parametres?

user64150
  • 59
  • 5

1 Answers1

1

you could simply use a dictionary and access them.

beta={0: beta0, 1: beta1, 2: beta2}

Access them with

beta[0]
beta[1]
...

If you need to use the numeric value as variable you can also do this by indexing: eg.

for num in range(3):
    print(beta[num])
Crysers
  • 455
  • 2
  • 13
  • I'd add that a number following a dot is not valid syntax, so a custom class with the exact semantics desired can not be created. But indeed, a dictionary is the obvious thing to use, unless all numeric keys exist in a contiguous range starting in 0 - in that case, just use a list. – jsbueno Mar 16 '21 at 17:37