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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:39:47+00:00 2026-06-08T18:39:47+00:00

Given below is a snippet from a class of which I am trying to

  • 0

Given below is a snippet from a class of which I am trying to create objects and getting error:

class FoF(object):
        def __init__(self,path):
                filepath=[]
                filepath.append(self.FileOrFolder(path))

Upon executing which I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PathOps.py", line 6, in __init__
    def __init__(self,path):
  NameError: global name 'filepath' is not defined

After which I tried:

filepath=[]
class FoF(object):
        def __init__(self,path):
                global filepath.append(self.FileOrFolder(path))

And again:

File "<stdin>", line 1, in <module>
  File "PathOps.py", line 6, in __init__
    global filepath.append(self.FileOrFolder(path))
NameError: global name 'filepath' is not defined

What is causing the error and how do I fix it?

  • 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-08T18:39:48+00:00Added an answer on June 8, 2026 at 6:39 pm

    Try using insted of global the special word self.

    So something like this

    class FoF(object):
        def __init__(self,path):
            self.filepath=[]
            self.filepath.append(self.FileOrFolder(path))
    

    The reason this error comes up is because what python thinks you’re trying to do is one of two things:

    1. Either you’re trying to reference a global variable called filepath — which is clear that’s not what you’re trying
    2. What’s not so clear is that you could also define a class attribute called filepath — the only problem with that is that you can’t define a class attribute with a function of that class. You can only do so within the class — outside a class function

    So in order to declare variables within a function you have to use the word self before it.

    Edit** if you want it to be an attribute of the class — as I’m assuming is what you meant you could do so like this:

    class FoF(object):
        filepath=[]
        def __init__(self,path):
            self.filepath.append(self.FileOrFolder(path))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to match the word first_name from the below snippet: <div class=field> <%=
I got code snippet from this site, which I used as shown below $data
I'm having the following error while trying to compile the snippet below (using g++):
I have found a code snippet as given below which is used to disable
I am creating a form in CakePHP.A snippet of the code is given below.
Which code snippet will give better performance? The below code segments were written in
Given below is program for encrypting a string. I had taken this code from
for the Given below code after int Input Value of 46348 i am getting
In the code given below, I am trying to modify it in such a
Hello friends i am running code given below which contains the setLogTimeEntery function and

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.