0

I have a simple class with 50+ attributes. All of them will be set up as empty string when the class is created. What is the best pythonic way of doing it?

Should it be like that?

att1 = att2 = att3 ... = att100 = ''

Or:

att1 = ''
att2 = ''
...
att100 = ''
Matheus Oliveira
  • 587
  • 3
  • 10
  • 33
  • 3
    `for x in range(1, 101): setattr(self, f"att{x}", "")`, maybe? But a class with 50-100 attributes seems a bit much and [variable variables](https://stackoverflow.com/q/1373164/3001761) are generally a bad idea - can you give some context as to why you need this? – jonrsharpe Oct 12 '20 at 09:59
  • @ jonrsharpe I am making a parser of a custom text file. But actually now that you commented, it migth be better to create one field with a dictionary to map this values, rigth? – Matheus Oliveira Oct 12 '20 at 10:16
  • Definitely using a dictionary or a list is the way to go – Cameron Chandler Oct 12 '20 at 10:26

0 Answers0