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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:54:18+00:00 2026-06-08T05:54:18+00:00

So I’m in the process of making a class in Python that creates a

  • 0

So I’m in the process of making a class in Python that creates a network (with pybrain) using solely the numeric input it’s given {just a little process to get my feet wet in Pybrain’s API}.

My problem is, I’m rather unfamiliar with how scopes work in classes, and while I basically have the program set up properly, it keeps returning a keyerror.

All the variables needed to be acted upon are created in the init function; the method I’m working on for the class is trying to call upon one of the variables, declared in the init function using the vars()[] method in Python. (you can actually see a portion of the code’s…rough draft here:
Matching Binary operators in Tuples to Dictionary Items

Anyways, the method is:

    def constructnetwork(self):
    """constructs network using gathered data in __init__"""
       if self.recurrent:
            self.network = pybrain.RecurrentNetwork  
            #add modules/connections here
       elif self.forward:
            self.network = pybrain.FeedForwardNetwork
       else:
            self.network = pybrain.network

       print vars()[self.CONNECT+str(1)]
       print vars()[self.CONNECT+str(2)]
       print self.network

(pardon the bad spacing, it didn’t copy and paste over well.) The part that’s raising the KeyError is the “print vars()[self.CONNECT+str(1)], which should retreive the value of the variable “Connection1” (self.CONNECT = ‘Connection’), but calls a keyerror.

How do I get the variables to transfer over? If you need more information to help just ask, I’m trying to keep the quesiton as short as possible.

  • 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-06-08T05:54:21+00:00Added an answer on June 8, 2026 at 5:54 am

    vars() returns a reference to the dictionary of local variables. If you used vars() in your __init__ (as the code in the post you linked to suggests), then you just created a local variable in that method, which isn’t accessible from anywhere outside that method.

    What is it that you think vars() does, and what are you trying to do? I have a hunch that what you want is getattr and setattr, or just a dictionary, and not vars.

    Edit: Based on your comment, it sounds like, indeed, you shouldn’t use vars. You would be better off, in __init__, doing something like:

     self.connections = {}
     self.connections[1] = "This is connection 1"
    

    then in your method, do:

     self.connections[1]
    

    This is just a vague guess based on your code, though. I can’t really tell what you are intending for this “connection”. Do you want it to be some data associated with your object?

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

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.