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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:49:02+00:00 2026-05-29T18:49:02+00:00

I am creating class instances by running a text file and slicing the words

  • 0

I am creating class instances by running a text file and slicing the words into a list, I am using white space as the split point.

I am then creating objects of the class Word based on the names in the array using this bit of code.

exec("%s = Word(title)" % (title))

I know the objects are being created as I have the object print upon initialization.

My problem is that if I try to access these objects I get:

blue.getWordName()    
NameError: global name 'blue' is not defined

I am really confused and have tried searching for answers but I’m not sure I am using the correct terminology.

I have figured out that I can use a dictionary to achieve my aim but I really want to understand class instances more.

Is there a law against creating classes on the fly from list items?

I did try to include all of the code but it wouldn’t let me so I’ll include what I think is most relevant:

class Word():
def __init__(self, name):
    print 'You have just created me and I\'m called ' + name
    self.name = name


if count > 0 and wordArray[count - 1] == 'is':
        title = wordArray[count]
        if title not in checkKeyWords():
            exec("%s = Word(title)" % (title)) #set class from list item.


            if word in verbArray:
                exec("%s.setWordType('verb')" % (title))

Eventually I call the below function which has been created and is spelled correctly, I have also made sure that ‘blue’ is definitely in the list.

blue.getWordName() 
  • 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-29T18:49:03+00:00Added an answer on May 29, 2026 at 6:49 pm

    Let me start by saying that I totally agree with the other answer, use dictionaries instead. If you want an answer to your problem, please make your example reproducible. A slightly adapted version of your code:

    class Word():
      def __init__(self, name):
        print 'You have just created me and I\'m called ' + name
        self.name = name
        self.wordType = None
      def setWordType(self, type_string):
        print "Hey, and the method works!"
        self.wordType = type_string
    
    wordArray = ["spam","ni", "is","blue"]
    count = 3
    if count > 0 and wordArray[count - 1] == 'is':
      title = wordArray[count]
      exec("%s = Word(title)" % (title)) #set class from list item.
      exec("%s.setWordType('verb')" % (title))
    

    Works fine:

    > python bla.py 
    You have just created me and I'm called blue
    Hey, and the method works!
    

    It might be that you have your indentation messed up. You check that the title is no in a list of keywords (if title not in checkKeyWords()), and create the object only if that holds. Could be that you refer to the blue object somewhere in the code, while it was not created. But without a solid example, this is impossible to tell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Rails controller, I'm creating multiple instances of the same model class. I
I'm running a for loop inside a function which is creating instances of a
Let's say I have one thread running (I'm creating many instances of this thread),
After creating a instance of a class, can we invoke the constructor explicitly? For
I´m dynamically creating an instance of a class with reflection and this works fine,
When creating a class library in C++, you can choose between dynamic ( .dll
When creating a class that has internal private methods, usually to reduce code duplication,
I'm creating a class named TetraQueue that inherits System.Collections.Generic.Queue class overriding the Dequeue method,
I am creating a class that determines which of number of registered WCF client
I'm creating a class. What's the best practice for naming properties/methods when your preferred

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.