0

I want to see if there is a better way. I am kinda new to python. It is a way for me to try and use classes. Any help or suggestions would be nice.

class Employee:

    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.pay = pay
        self.email = first + '.' + last + '@company.com'

for Number in range(30):
        globals()[f"E{Number}"] = Employee('First', 'Last', 0)

print(E5.__dict__)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Using a `list` is probably a better idea. – Asocia Jul 28 '20 at 16:01
  • FWIW, you can write a *lot* of Python code without ever using the `globals` function. It should not be part of your everyday toolbox, so to speak. – chepner Jul 28 '20 at 16:03

1 Answers1

2

If you want a collection of instances, don't use individually named variables; use a container like a list

Es = [Employee('First', 'Last', 0) for _ in range(30)]

or a dictionary

Es = {k: Employee('First', 'Last', 0) for k in range(30)}

In both cases, you can index Es with an int; a dict gives you more flexibility in terms of what kinds of indices to use instead.

chepner
  • 497,756
  • 71
  • 530
  • 681