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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:57:57+00:00 2026-06-12T10:57:57+00:00

I have a python application which is as follows: global_counter = 0 connections =

  • 0

I have a python application which is as follows:

global_counter = 0
connections = {}

class SocketHandler():
    currentid = 0
    def open(self):
        global global_counter
        global connections
        currentid = global_counter
        global_counter += 1
        connections[currentid] = self
        print "WebSocket " + str(currentid) + " opened"

    def on_close(self):
        global connections
        print "WebSocket " + str(currentid) + " closed"
        del connections[currentid]

I’m getting the error:

NameError: global name 'currentid' is not defined

on the lines of “open” and “on_close” where I print that I am opening/closing the connection. I defined it in the class, why is it not in scope. Also, I have read that using global variables is bad, but I don’t see a way around this. Can someone point out what I should do? Thanks.

  • 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-12T10:57:58+00:00Added an answer on June 12, 2026 at 10:57 am

    You don’t have implicit access to attributes inside methods, in Python.

    A bare name like currentid in the line:

    del connections[currentid]
    

    always looks up a name in the local function scope, then in each enclosing function scope, before trying the global module scope (and then looks at built-ins as a last resort). currentid is a class attribute, which won’t be found in any of those scopes.

    To look up an attribute in Python you always need to specify an object in which to look. Though the lookup protocol means the object need not necessarily have the attribute itself; attribute lookup will fall back to the class of the object you specified (and the base classes, if inheritance is involved).

    So this would work:

    del connections[self.currentid]
    

    However, I don’t think the rest of your code is doing what you think it is either. This line in the open method:

    currentid = global_counter
    

    doesn’t set the currentid attribute of your SocketHandler object. Assigning to a bare name always assigns to a local variable, unless you explicitly declare it global (you appear to be aware of this, since you’ve used the global keyword). So in the open method, currentid is a local function variable; its value is lost at the end of the open method.

    In fact, your SocketHandler objects do not have a currentid attribute at all (unless there’s more code you haven’t shown us). Putting currentid = 0 in the class block doesn’t give all the SocketHandler instances a currentid attribute. It gives the SocketHandler class itself an attribute currentid; this is just as the def open(self): block creates an open attribute (storing a function) on the class object, not on each individual instance.

    Reading self.currentid in the on_close method will fail to find a currentid attribute in the object self, so Python will look at the class of self which is SocketHandler. That object does have a currentid value, so the result of reading self.currentid will be 0, whether or not you’ve previously run open on that SocketHandler.

    If you meant to store the currentid as an instance variable in each SocketHandler, then the line in open would need to be:

    self.currentid = global_counter
    

    This assigns to the currentid attribute of the object referred to by self. You would also then need to change all the other references to currentid in your methods to self.currentid.

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

Sidebar

Related Questions

I have a Python application in which I would like to monitor the number
I have a python (pygtk) application which starts in different modes depending on arguments.
I have a python application which need a gui HTML editor, I know FCKeditor
I have a Python application which sends 556 bytes of data across the network
I have a regular desktop application which is written in Python/GTK and SQLObject as
With my python application, I have 40 modules (classes) which contain a parser for
I have a python based application which works like a feed aggregator and needs
So I have a 3rd party application which exposes a Python API. I have
I have a python application which uses a compiled shared library. This library for
I have some few classes which are widespread in my Python application and which

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.