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 6751683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:57:21+00:00 2026-05-26T12:57:21+00:00

I’m trying to store a file which encode an id of a class, read

  • 0

I’m trying to store a file which encode an id of a class, read the file and call the class so that ->
in the files the data will be stored like

id_class:(arguments)

than the read file will look up from a list of file the right class to invoque and pass the arguments.

something like this:

class foo:
        id = 1
    def __init__(self):
        self.attr = 10
    def __str__(self):
            return str(self.attr)


class bar:
        id = 2
    def __init__(self):
        self.attr = 20
    def __str__(self):
            return str(self.attr)


def create_foo():
    return foo

def create_bar():
    return bar

class_dict = {1:create_foo(),2:create_bar()}

class_index = [1,2,1,2,1,1,1,2,2,2,1] #data read from file

class_list = [] #output list containing the newly instanciated bar or foo

for index in class_index:
    c = class_dict[index]
    class_list.append(c)

but this code append in the class_list for example foo, but is only one class, because if I modify the attribute will be modified in the whole list.

for example:

for classe in class_list:
    print classe,

print "\n-------------"
class_list[0].attr = 15

for classe in class_list:
    print classe,

the output is:

10 20 10 20 10 10 10 20 20 20 10 
-------------
15 20 15 20 15 15 15 20 20 20 15

and should be:

10 20 10 20 10 10 10 20 20 20 10 
-------------
15 20 10 20 10 10 10 20 20 20 10
  • 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-26T12:57:22+00:00Added an answer on May 26, 2026 at 12:57 pm

    I changed both create methods – they were missing parenthesis, without them no new instances of the object were created. Also, I changed the class_dict so it won’t call the create methods, instead I defer the instantiation to the moment the class_dict is accessed: class_dict[index](). The modified code looks like this:

    class foo:
        id = 1
        def __init__(self):
            self.attr = 10
    
    class bar:
        id = 2
        def __init__(self):
            self.attr = 20
    
    def create_foo():
        return foo()
    
    def create_bar():
        return bar()
    
    class_dict = {1:create_foo,2:create_bar}
    
    class_index = [1,2,1,2,1,1,1,2,2,2,1] #data read from file
    
    class_list = [] #output list containing the newly instanciated bar or foo
    
    for index in class_index:
        c = class_dict[index]()
        class_list.append(c)
    
    for classe in class_list:
        print str(classe.attr),
    
    print "\n-------------"
    class_list[0].attr = 15
    
    for classe in class_list:
        print str(classe.attr),
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.