why does self. work for some and then not

At the risk of beating a dead horse, I would like to clarify something
about this, which I'm sure Mike already knows.

The word "self" is not magic. Putting "self" at the beginning of a
variable does not make it a member of the class. The only MAGIC happens
when you make a call to a member function like this:
    gridSizer.Add( input, 0, wx.EXPAND )
The "magic" is that the object ("gridSizer" in this case) is passed to
the function ("Add") as the first parameter. If gridSizer is an object
of type wx.GridSizer, as I assume, then that line is exactly equivalent
to this:
    wx.GridSizer.Add( gridSizer, input, 0, wx.EXPAND )
(assuming no one has overridden that function.)

Now, in the definition of wx.GridSizer, we might find something like this:

    class GridSizer( wx.FlexGridSizer ):
        ...
        def Add( self, item, ... ):
            self.itemList.append( item )

In that last statement, "self" refers to the "gridSizer" object in the
calling code, NOT because "self" is magic, but because "gridSizer" was
passed as the first parameter, and the first parameter is called
"self". "self" is merely a convention. It is perfectly valid Python to
use another name:

    class GridSizer( wx.FlexGridSizer ):
        ...
        def Add( this, item, ... ):
            this.itemList.append( item )

However, the people who have to maintain your code will likely hunt you
down.

···

On Mon, 12 Jan 2009 09:31:47 -0600, Mike Driscoll <mike@pythonlibrary.org> wrote:

Michael Hipp wrote:

      self.inputTxtOne = wx.TextCtrl(self.panel, wx.ID_ANY,'2')

      gridSizer.Add(inputTxtOne, 0, wx.EXPAND)

I haven't tried to run your code, but that last line needs a 'self.'
in front. Like this:

      gridSizer.Add(self.inputTxtOne, 0, wx.EXPAND)

This is basic python. When you add self. where the variable is
defined (1st line above) it then becomes an attribute of the object
instance. Otherwise its scope is local to __init__(). Adding self
makes it "global" to all of MyForm and must always be referred to
with self.inputTxtOne

Just to be doubly clear on what Hipp's said, the "self" only makes the
variable "global" to the class in which it is defined in. The variable
will not be accessible in other classes in your program unless you
make it so. Here's an example:
...
When you have the "self" at the beginning of a variable, you make that
variable a property of the class in which it resides.

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Tim Roberts wrote:

···

On Mon, 12 Jan 2009 09:31:47 -0600, Mike Driscoll > <mike@pythonlibrary.org> wrote:
  

Michael Hipp wrote:
    

      self.inputTxtOne = wx.TextCtrl(self.panel, wx.ID_ANY,'2')

      gridSizer.Add(inputTxtOne, 0, wx.EXPAND)

I haven't tried to run your code, but that last line needs a 'self.'
in front. Like this:

      gridSizer.Add(self.inputTxtOne, 0, wx.EXPAND)

This is basic python. When you add self. where the variable is
defined (1st line above) it then becomes an attribute of the object
instance. Otherwise its scope is local to __init__(). Adding self
makes it "global" to all of MyForm and must always be referred to
with self.inputTxtOne
      

Just to be doubly clear on what Hipp's said, the "self" only makes the
variable "global" to the class in which it is defined in. The variable
will not be accessible in other classes in your program unless you
make it so. Here's an example:
...
When you have the "self" at the beginning of a variable, you make that
variable a property of the class in which it resides.
    
At the risk of beating a dead horse, I would like to clarify something
about this, which I'm sure Mike already knows.

The word "self" is not magic. Putting "self" at the beginning of a
variable does not make it a member of the class. The only MAGIC happens
when you make a call to a member function like this:
    gridSizer.Add( input, 0, wx.EXPAND )
The "magic" is that the object ("gridSizer" in this case) is passed to
the function ("Add") as the first parameter. If gridSizer is an object
of type wx.GridSizer, as I assume, then that line is exactly equivalent
to this:
    wx.GridSizer.Add( gridSizer, input, 0, wx.EXPAND )
(assuming no one has overridden that function.)

Now, in the definition of wx.GridSizer, we might find something like this:

    class GridSizer( wx.FlexGridSizer ):
        ...
        def Add( self, item, ... ):
            self.itemList.append( item )

In that last statement, "self" refers to the "gridSizer" object in the
calling code, NOT because "self" is magic, but because "gridSizer" was
passed as the first parameter, and the first parameter is called
"self". "self" is merely a convention. It is perfectly valid Python to
use another name:

    class GridSizer( wx.FlexGridSizer ):
        ...
        def Add( this, item, ... ):
            this.itemList.append( item )

However, the people who have to maintain your code will likely hunt you
down.

Good point Tim...I probably should have mentioned that myself, but I tend to think that "everyone" know that even when they most certainly don't.

Mike