1

been searching in vain for many hours. This should be simple. I'm doing a select from a SQL database and want to create a class instance for each row that is returned. I also need to iterate through each object later, so I'm guessing I'll need to stow the variables in a list or dict.

Here's a mock up of what I'm trying to accomplish. Didn't want to make everyone suffer through the original code so I'm pared it down to just what I want to accomplish.

class Dynamic_Class:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname


name_list = []
for i in range(1,6):
    obj_name = ("person" + str(i))
    name_list.append(obj_name)
    obj_name = Dynamic_Class("Joel", "Robinson")

    print(person1.fname)

I think I've seen some brute force methods out there that I could get to work, but I think I should use something more pythonic. Thanks.

EDIT: I've been pointed to a solution "How do I create variable variables?" (How do I create variable variables?) but that still doesn't accomplish what I want. Here are the class instantiations when creating a dictionary and instatianting with variables from that dict:

class Dynamic_Class:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname


heddy = Dynamic_Class("Tom", "Servo")
print(heddy.fname)

name_dict = {}
for i in range(1,6):
    obj_name = ("person" + str(i))
    obj_name2 = ("person" + str(i))
    print(("obj name is ",obj_name))
    name_dict[obj_name] = obj_name2
    name_dict[obj_name] = Dynamic_Class("Joel", "Robinson")
    print(name_dict["person1"].fname)

    print(person1.fname)

The output:

C:\Users\bucky\OneDrive\Python_Installs\PycharmProjects\ReadSQLWireNFIRS\Scripts\python.exe C:\Users\bucky\OneDrive\Python_Installs\PycharmProjects\ReadSQLWireNFIRS\scratch.py 
Tom
('obj name is ', 'person1')
Joel
Traceback (most recent call last):
  File "C:\Users\bucky\OneDrive\Python_Installs\PycharmProjects\ReadSQLWireNFIRS\scratch.py", line 20, in <module>
    print(person1.fname)
          ^^^^^^^
NameError: name 'person1' is not defined

Process finished with exit code 1

All of the solutions in that linked question start with constants: i.e. making a dictionary from a list of contants...which sort of defeats the purpose. In english, to boil it down:

I want to read 200,000 rows from a sql database and create 200,000 class objects referenceable by "Person1" through Person200000. I would think this is basic, but I just don't know how to ask this question on teh google.

  • 3
    Short answer: Don't do that. Use a list or a dictionary instead. – Selcuk Dec 16 '22 at 03:48
  • 1
    Right. ANY TIME you find yourself using variables like `xxx1`, `xxx2`, `xxx3` etc, you need to use a list or dictionary instead. – Tim Roberts Dec 16 '22 at 04:51
  • I'm not being intentionally obtuse, I swear- but I need the functionality off the Class with each row. I see the value of the list to keep track of each object name in the class, but I can't figure out how to get all of the objects instantiated. I've got to have SOME way to name the objects, right? – buckyswider Dec 16 '22 at 04:58

0 Answers0