Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7003883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:07:37+00:00 2026-05-27T21:07:37+00:00

Tried the following, where objectname contains a string name, to be assigned on creation

  • 0

Tried the following, where “objectname” contains a string name, to be assigned on creation of an object.

for record in result:
    objectname = 'Customer' + str(record[0])
    print objectname
    customername = str(record[1])
    objectname = Customer(customername)

Where Customer is a class.

In my test, this loop runs twice printing “objectname” as Customer1 and Customer2, yet creates 2 objects, but the objects are called “objectname” (it overwrites each loop), opposed to the 2 unique objects Customer1 or Customer2.

Its simply not assigning strings(customer1,2) inside the variable, but purely the variables name.

I’ve tried assigning strings to the object name, but that gives a syntax error

Surely this must be done all the time, thanks for your help in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:07:37+00:00Added an answer on May 27, 2026 at 9:07 pm

    Instead of using a new variable for each customer you could store your object in a Python dictionary:

    d = dict()
    
    for record in result:
        objectname = 'Customer' + str(record[0])
        customername = str(record[1])
        d[objectname] = Customer(customername)
    
    print d
    

    An example of objects stored in dictionaries

    I just could’nt help my self writting some code (more than I set out to do). It’s like addictive. Anyway, I would’nt use objects for this kind of work. I probably would use a sqlite database (could be saved in memory if you want). But this piece of code show you (hopefully) how you can use dictionaries to save objects with customer data in:

    # Initiate customer dictionary
    customers = dict()
    
    class Customer:
        def __init__(self, fname, lname):
            self.fname = fname
            self.lname = lname
            self.address = None
            self.zip = None
            self.state = None
            self.city = None
            self.phone = None
    
        def add_address(self, address, zp, state, city):
            self.address = address
            self.zip = zp
            self.state = state
            self.city = city
    
        def add_phone(self, number):
            self.phone = number
    
    
    # Observe that these functions are not belonging to the class.    
    def _print_layout(object):
            print object.fname, object.lname
            print '==========================='
            print 'ADDRESS:'
            print object.address
            print object.zip
            print object.state
            print object.city
            print '\nPHONE:'
            print object.phone
            print '\n'
    
    def print_customer(customer_name):
        _print_layout(customers[customer_name])
    
    def print_customers():
        for customer_name in customers.iterkeys():
            _print_layout(customers[customer_name])
    
    if __name__ == '__main__':
        # Add some customers to dictionary:
        customers['Steve'] = Customer('Steve', 'Jobs')
        customers['Niclas'] = Customer('Niclas', 'Nilsson')
        # Add some more data
        customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
        customers['Steve'].add_phone('123-543 234')
    
        # Search one customer and print him
        print 'Here are one customer searched:'
        print 'ooooooooooooooooooooooooooooooo'
        print_customer('Niclas')
    
        # Print all the customers nicely
        print '\n\nHere are all customers'
        print 'oooooooooooooooooooooo'
        print_customers()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried the following code in LINQPad and got the results given below: List<string>
I tried following code : #include<iostream> #include<string> using namespace std; string f1(string s) {
I need TextBox which will reflect changes in databound string. I tried following code:
I've tried following the advice found @ https://wikis.oracle.com/display/HotSpotInternals/PrintAssembly and http://alexshabanov.com/2011/12/29/print-assembly-for-java/ , but it wasn't
Tried following the instructions here: How to use Google app engine with my own
I tried following the answer to this question , but could not get xsd.exe
I tried following the README file in Ruby 1.9.1 but I can't compile it
Have you managed to get Aptana Studio debugging to work? I tried following this,
I want all the lines with assert_equal and without amazon. I tried following but
I'm trying to open a pop-up menu from a NSToolbarItem. I tried following this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.