[Tutor] __init__ Problem, self is not defined

BELSEY, Dylan dylan.belsey@baesystems.com
Wed, 31 Jul 2002 09:49:18 +0930


Matthew,
	Copied your code and ran it in idle.  I believe that the interpreter
is not actually falling over at 'self' but at
'scrolledlist_items=self.addresses'.  When declaring member functions (if
that is what they are called in Python), the 'self' as the first parameter
is standard. However the interpreter cannot find 'self.addresses' because it
has not been created yet (for that matter: self.getAddress as well).  These
can't be referred to in the __init__() args because they don't exist yet.
	Quoting Ivan Van Laningham in "SAMS Teach Yourself Python in 24
Hours", "Self...is just a simple, explicit way that a method can tell what
kind of object it is and that gives it a way to change its own state and/or
behaviour."
	In light of this, self is just being "defined" in the __init__()
method so the attributes such as 'address' and 'getAddress' are undefined.
	I put the following in a file (test.py) and ran it at the prompt
(the self. refs have been removed):


from Tkinter import *
import Pmw

class AddressComboBox(Pmw.ComboBox):
    def __init__(self, root=None, label_text='Address:',
                 label_pos='w', listbox_width=24, dropdown=1,
                 history=1,
                 unique=1,
                 addressfile=None):
        print "dasgd"

a = AddressComboBox()


C:\Python21>test.py
dasgd

C:\Python21>

	Unfortunately, not knowing how/why your class requires these input
parameters I can't be of much more assistance than to say you might have to
rethink your default values.
	Hope this helps,
		Dylan


-----Original Message-----
From: Matthew Navarre [mailto:mnavarre@anteon.com]
Sent: Wednesday, 31 July 2002 08:30
To: tutor@python.org
Subject: [Tutor] __init__ Problem, self is not defined



I'm attempting to subclass Pmw.ComboBox. when my class definition is run by 
the interpreter I get a NameError: name self is not defined. AFAICT, my 
syntax is correct, but I've obviously missed something.

here's the relevant part of my class definition:
from Tkinter import *
import Pmw

class AddressComboBox(Pmw.ComboBox):
    def __init__(self, root=None, label_text='Address:',
                label_pos='w', listbox_width=24, dropdown=1,
                scrolledlist_items=self.addresses, history=1,
                unique=1, selectioncommand=self.getAddress, 
                addressfile=None ):

The interpreter gets to line five and throws NameError on self:
Python 2.2.1 (#1, Jun 27 2002, 13:17:43) 
[GCC 2.95.3 20010315 (release) [FreeBSD]] on freebsd4
Type "help", "copyright", "credits" or "license" for more information.
>>> import AddressComboBox
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "AddressComboBox.py", line 5, in ?
    class AddressComboBox(Pmw.ComboBox):
  File "AddressComboBox.py", line 6, in AddressComboBox
    def __init__(self, root=None, label_text='Address:',
NameError: name 'self' is not defined

Does anyone have an idea what's going on here? every example I have seems
like 
this should work. 

thanks,
Matt 'Well, I'm stumped' Navarre
-- 
mnavarre@anteon.com           Matthew Navarre
It was a hard sell, since he's a database person, and as far as I've seen,
once those database worms eat into your brain, it's hard to ever get
anything practical done again. To a database person, every nail looks
like a thumb. Or something like that. - jwz


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor