0

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.

4
  • that code looks good. Show us the code where Customer is defined Commented Dec 30, 2011 at 16:50
  • At what point do you reference the objectname object? If you do after your loop is finished, naturally object name will have the value set in the last loop iteration. Commented Dec 30, 2011 at 16:56
  • Added the customer class, thanks for your quick response Commented Dec 30, 2011 at 16:56
  • I tried referencing the created objects after the loop and inside it, but where I print the objectname variable I get the correct output "Customer1" and "Customer2", but the object is called "objectname", NOT "Customer1" Which is whats actually in the variable objectname. I can objectname.output(), but NOT Customer1.output(). I hope that makes more sense. Commented Dec 30, 2011 at 17:00

1 Answer 1

1

Each objectname should be added to a namespace so that the objects they refer to can be easily accessed later.

The simplest way to do this is to use a dictionary:

customers = {}
for record in result: 
    objectname = 'Customer' + str(record[0])
    customers[objectname] = Customer(str(record[1]))
customercount = len(customers)
...
customers['Customer1'].output()

In fact, you could make things even simpler by using the Customer ID itself as the dictionary keys:

customers = {}
for record in result: 
    customers[record[0]] = Customer(str(record[1]))
customercount = len(customers)
...
customers[1].output()

Note that if all the customer objects had a separate objectname variable, it would be much harder to process them as a group.

But once they're in a dictionary, they can can be iterated over whenever necessary:

for identifier, customer in customers.iteritems():
    print 'Customer%d:' % identifier, customer.name
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.