A question of scope

I am still a bit confused by issues of scope. I have a class and a logging function outside that class. When I try to call that logging function, I get an error which says:

Traceback (most recent call last):
  File "F:\wxComTool10.py", line 2336, in CommandCallback
    self.SendCommand(self.selection, "")
  File "F:\wxComTool10.py", line 277, in SendCommand
    self.ProcessCommand(command, arguments)
  File "F:\wxComTool10.py", line 288, in ProcessCommand
    LogMessage("Doing it",'111')
  File "F:\wxComTool10.py", line 39, in LogMessage
    self.outputbox.SetLabel(message)
NameError: global name 'self' is not defined

The relevant code looks like this:

import stuff

def LogMessage(message, whichfiles):
    '''This function logs a message which is passed into it. The whichfiles
    variable determines to where the message gets logged: doswindow, outputfile, outputwin.'''
    if whichfiles == '000':
        LogMessage("Why did you call this function if you weren't going to log to anywhere?",101)
    elif whichfiles == '100':
        print message
    elif whichfiles == '110':
        print message
        outfile.write(message)
    elif whichfiles == '111':
  print message
        outfile.write(message)
        self.outputbox.SetLabel(message)

class ComboBoxPanel(wxPanel):
    
    def __init__(self, parent, ID = -1, size = wxDefaultSize, style= -1):
        
        wxPanel.__init__(self, parent, ID, wxDefaultPosition,size=(480,180),style=wxSUNKEN_BORDER)
        self.frame=parent
        EVT_CLOSE(self, parent.OnExit)

[bunch of wxPython which create the gui]

    def SendCommand(self, command, arguments):
        
        self.ProcessCommand(command, arguments)

    def ProcessCommand(self, command, parameters):
        returnedval = ""
        i=0
        args = parameters.split()
        for count in args:
            i+=1

        self.numparams = i

        LogMessage("Doing it",'111')

[snip]

I hope I gave you enough code to help me figure this out. How do I call a function that is outside the class inside a function that is inside a class?

--vicki

P.S. I really want to do bitwise ops to determine which places to log to. That is log to the places that are represented by the bits that are on, but I haven't learned how to do that in Python yet. I googled, but I haven't found it yet. If someone would give me an idea of where to find an example of this, I'd appreciate it.

I am still a bit confused by issues of scope.

The problem is here:

def LogMessage(message, whichfiles):

[...]

    elif whichfiles == '111':
  print message
        outfile.write(message)
        self.outputbox.SetLabel(message)

The call to LogMessage is succeeding, but you're using the variable
'self', which isn't defined anywhere in this function. Either add a
third parameter (in which you would pass 'self.outputbox', or provide
a function to set the destination for GUI-based logging separately
(the latter is usually a bit cleaner).

P.S. I really want to do bitwise ops to determine which places to
log to. That is log to the places that are represented by the bits
that are on, but I haven't learned how to do that in Python yet. I
googled, but I haven't found it yet. If someone would give me an
idea of where to find an example of this, I'd appreciate it.

Bitwise 'and' uses &; bitwise 'or' uses |:

LOG_STDIO, LOG_OUTFILE, LOG_GUI = (4, 2, 1)

LogMessage('spam and eggs', LOG_STDIO | LOG_GUI

[in LogMessage]:

if whichfiles & LOG_STDIO:
  print message
if whichfiles & LOG_OUTFILE:
  outfile.write(message)
if whichfiles & LOG_GUI:
  guitarget.SetLabel(message)

···

On Fri, Apr 16, 2004 at 04:04:35PM -0500, Vicki Stanfield wrote:

--
Tim Lesher <tim@lesher.ws>
http://www.lesher.ws

Vicki Stanfield wrote:

I am still a bit confused by issues of scope. I have a class and a logging function outside that class. When I try to call that logging function, I get an error which says:

looking in the traceback, I see:

  File "F:\wxComTool10.py", line 39, in LogMessage
    self.outputbox.SetLabel(message)

You've tried to use self in the LogMessage function.

NameError: global name 'self' is not defined

clearly se3lf is not defioned here.

def LogMessage(message, whichfiles):
    '''This function logs a message which is passed into it. The whichfiles
    variable determines to where the message gets logged: doswindow, outputfile, outputwin.'''
    if whichfiles == '000':
        LogMessage("Why did you call this function if you weren't going to log to anywhere?",101)
    elif whichfiles == '100':
        print message
    elif whichfiles == '110':
        print message
        outfile.write(message)
    elif whichfiles == '111':
  print message
        outfile.write(message)
        self.outputbox.SetLabel(message)

you've got an output box that is a member of sself...what is self suppposed to be? If it's your ComboBoxPanel, then you need to pass that in to your LogMessage function:

def LogMessage(message, whichfiles, ComboPanel):

then :

    def ProcessCommand(self, command, parameters):
        returnedval = ""
        i=0
        args = parameters.split()
        for count in args:
            i+=1

        self.numparams = i

          LogMessage("Doing it",'111', self)
                                       ^^^^^^

That should do it, but if you are really wanting to log a message to the outputbox of the combopanel, you may just want LogMessage (or that part of it) to be a member of your custom Panel.

P.S. I really want to do bitwise ops to determine which places to log to. That is log to the places that are represented by the bits that are on, but I haven't learned how to do that in Python yet. I googled, but I haven't found it yet. If someone would give me an idea of where to find an example of this, I'd appreciate it.

>>> # first bit:
>>> m = 1
>>> #second bit
>>> m = 2**1
>>> #third bit
>>> m = 3**3
>>> #etc
...
>>> Mask = 1 + 2**4 + 2**6 # 00000000101001
>>>
>>> Mask & 1
1
>>> Mask & 2
0
>>> Mask & 2**2
0
>>> Mask & 2**3
0
>>> Mask & 2**4
16
>>> Mask & 2**5
0
>>> Mask & 2**6
64

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

I think you mean 2**2... 3**3 is 27. :slight_smile:

···

On Fri, Apr 16, 2004 at 02:22:32PM -0700, Chris Barker wrote:

>>> # first bit:
>>> m = 1
>>> #second bit
>>> m = 2**1
>>> #third bit
>>> m = 3**3

--
Tim Lesher <tim@lesher.ws>
http://www.lesher.ws

Tim Lesher wrote:

···

On Fri, Apr 16, 2004 at 02:22:32PM -0700, Chris Barker wrote:

# first bit:
m = 1
#second bit
m = 2**1
#third bit
m = 3**3

I think you mean 2**2... 3**3 is 27. :slight_smile:

I sure did...sorry if I caused confusion!

Isn't your computer base 3?

-Chris

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Chris Barker wrote:

Isn't your computer base 3?

Hmmm if the smallest unit of information on a binary computer is a
"bit," then what is the smallest unit on a trinary computer? An old
joke told once by DEK.

-Scott

Isn't your computer base 3?

Hmmm if the smallest unit of information on a binary computer is a
"bit," then what is the smallest unit on a trinary computer? An old
joke told once by DEK.

Heh. A trit? A twit? A tit?

Scott David Daniels wrote:

Isn't your computer base 3?

Hmmm if the smallest unit of information on a binary computer is a
"bit," then what is the smallest unit on a trinary computer? An old
joke told once by DEK.

A Trit, of course.

.

Why is everybody laughing?