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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:02:52+00:00 2026-06-14T09:02:52+00:00

I’ve read several answers about making variables global and sharing them between classes with

  • 0

I’ve read several answers about making variables global and sharing them between classes with the class.var functionality, but none of those seem to work in my example.

I only have one file: app.py which instantiates multiple classes. However, the variable that I want to make global isn’t in a class, it’s just in the main script. Then when I type global globvar in a function that is inside one of the classes, I get the error: `

NameError: global name 'globvar' is not defined.

How can I either pass this variable to the class by reference (I know, not pythonic language) so that the class can modify it but other classes can access it, or make it global so all the classes can access it?

EDIT bottom of file with no indentation:

app = MyApp(False)
logFile = open('C:/Python26/temp/test.txt', 'w')
globvar = 7
ser = serial.Serial('com5',115200,timeout=0.01)
app.MainLoop()

Then in my first class I have:

class MyApp(wx.App):
    def MainLoop(self):
        line = ser.readline()
        if len(line) > 2:
            global globvar
            globvar = line

And in my second class I have:

class MyFrame(wx.Frame):
    def OnMove(self, event):
        pos = event.GetPosition()
        global globvar
        self.posCtrl.SetValue("%s, %s" % (globvar, pos.y))

These are all in the same file. The error text is:

Traceback (most recent call last):
  File "C:\Program Files (x86)\wxPython2.8 Docs and Demos\samples\mainloop\mainloop.py", line 66, in OnMove
    self.posCtrl.SetValue("%s, %s" % (globvar, pos.y))
NameError: global name 'globvar' is not defined
  • 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-14T09:02:54+00:00Added an answer on June 14, 2026 at 9:02 am

    All parameters are passed by reference (in the C++ sense). The Python language doesn’t have pass by value (in the C++ sense) although it can be simulated by copying, and certain things are automatically copied on mutate like numbers, strings, and tuples such that they may give the appearance of having been passed by value. To be very specific python is call-by-object (aka call-by-sharing, call-by-object-reference) as pointed out here on stackoverflow previously.

    To make a variable global you need to make the first assignment to it outside of any function or create in a function with the global keyword. To modify it in a function you’ll also need to use the global keyword. See here for more details on the global keyword.

    For example:

    i = 0
    print i
    class A(object):
        global i
        i = 1
    print i
    class B(object):
        def Foo(self):
            global i
            i = 2
    b = B()
    b.Foo()
    print i
    

    yields output of:

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
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 don't have much knowledge about the IPv6 protocol, so sorry if the question
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm making a simple page using Google Maps API 3. My first. One marker
Let's say I'm outputting a post title and in our database, it's Hello Y’all

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.