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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:30:11+00:00 2026-06-09T17:30:11+00:00

In the python example below, methods and attributes seem to be out of scope

  • 0

In the python example below, methods and attributes seem to be out of scope but they still work, what is happening?

for module in output:
    a = 1
    attributes=[]
    methods=[]
    for branch in module[2]:

        for leaf in branch[2]:
            if leaf[0]=="method":
                methods.append(leaf[1]) 
            if leaf[0]=="attribute":
                attributes.append(leaf[1])
print methods
print attributes
print module[0]
print a

but if I outdent one more level it stops working

for filename in os.listdir("."):
    print filename
    fName, fExtension = os.path.splitext(filename)
    print fName, fExtension
    if fExtension == ".idl":
        f = open(filename)
        idl = f.read()
        f.close()
        output = parse(idl)
        pprint.pprint(output)
        root={}
        for module in output:
            a = 1
            attributes=[]
            methods=[]
            for branch in module[2]:
                for leaf in branch[2]:
                    if leaf[0]=="method":
                        methods.append(leaf[1]) 
                    if leaf[0]=="attribute":
                        attributes.append(leaf[1])
    print methods
    print module[0]

it says: NameError: name ‘methods’ is not defined
I’m using python 2.7

  • 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-09T17:30:13+00:00Added an answer on June 9, 2026 at 5:30 pm

    As indicated in the comments, for loops, while loops, if statements, etc. do not create a new scope. In fact, the only things that create a new scopes are functions, classes, modules and methods. Therefore, when you create a new variable inside a for loop, it is available outside of that loop because they share the same scope.

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

Sidebar

Related Questions

The example below is from a REST database driver on Python 2.7. In the
I am trying out an example from the book Foundations of python network programming,
I try to use doctest from example from http://docs.python.org/library/doctest.html But when I run python
How do I implement an async method in Python DBus? An Example below: class
The example GUI in the imported TkTable module works fine. But when I try
What (if any) is the C# equivalent of Python's itertools.chain method? Python Example: l1
How to tell python for example, I made a program, which opens a specific
I cannot get the example Python programs to run. When executing the Python command
It's a fairly simple builtin in python for example: x = range(0,100) How can
Take for example the python built in pow() function. xs = [1,2,3,4,5,6,7,8] from functools

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.