I am trying to create objects with variable names, when I print out my objectname variable the correct name is assigned to it. But when i try and create an object using the objectname variable, the object created is literally called "objectname", not using the string assigned to the variable. My code is below:
class Customer:
# Initiliaise method, creating a customer object
def __init__(self,name):
self.name = name
print "Customer %s Added" % (self.name)
# Print out details
def output(self):
print "This is the customer object called %s" % (self.name)
## Create the Customer objects, from the Customer table
# Pull the Customers out of the Customer table
# SQL
cursor.execute("SELECT * FROM Customer")
result = cursor.fetchall()
for record in result:
objectname = 'Customer' + str(record[0])
print objectname # This prints "Customer1..2" etc
# customername is the exact name as in the database
customername = str(record[1])
# Use the above variables pulled from the database to create a customer object
objectname=Customer(customername)
# We need to count the number of customer objects we create
customercount = customercount + 1
So all that will create is a single object called objectname, opposed to multiple objects "Customer1,2,3" etc, based on the number in the Customer DB table. The variable name is based on the string "Customer" and the row ID in the database.
I assume I am referencing the variable incorrectly,
Thanks for your help.
Customeris defined