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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:35:10+00:00 2026-06-13T07:35:10+00:00

So I have a class, which I’m using as a local namespace. I have

  • 0

So I have a class, which I’m using as a local namespace. I have some static functions in the class, but they can’t access the class scope variables. Why is this?

class Foo:
    foo_string = "I am a foo"

    @staticmethod
    def foo():
        print foo_string

>>> Foo.foo()
  [Stack Trace]
  NameError: global name 'foo_string' is not defined

Any thoughts?

  • 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-13T07:35:10+00:00Added an answer on June 13, 2026 at 7:35 am

    Python doesn’t let class variables fall into scope this way, there are two ways to do this, the first is to use a class method:

    @classmethod
    def foo(cls):
        print(cls.foo_string)
    

    Which I would argue is the best solution.

    The second is to access by name:

    @staticmethod
    def foo():
        print(Foo.foo_string)
    

    Do note that in general, using a class as a namespace isn’t the best way to do it, simply use a module with top-level functions instead, as this acts more as you want to.

    The reason for the lack of scoping like this is mainly due to Python’s dynamic nature, how would it work when you insert a function into the class? It would have to have special behaviour added to it conditionally, which would be extremely awkward to implement and potentially fragile. It also helps keep things explicit rather than implicit – it’s clear what is a class variable as opposed to a local variable.

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

Sidebar

Related Questions

I have a class which represents exam,it can start/reset/pause exam, so the methods look
I have a class which the programmer can use to dynamically add new properties.
I have class which calls a static class, the static class basically is wrapper
I have class which has Map, but when I try to presist this entity,
I have class(Customer) which holds more than 200 string variables as property. I'm using
I have a class which contains some items. I want to serialize an instance
I have a class which has UIImagePickerController and extends UIViewController . But viewWillAppear/viewDidAppear are
*I have class which load some files and update the UI...It takes some times
I have class which have one public method Start , one private method and
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a

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.