IDLE mini-hack & ? (adding menu items, understanding *args, self)

Blake Winton bwinton at tor.dhs.org
Fri Jul 30 21:41:02 EDT 1999


On Thu, 29 Jul 1999 10:33:08 +1200, Greg Ewing wrote:
>Isidor wrote:
>> My guess is that insert is a method of text, and "insert" (first
>> argument) is the location of the insertion point
>That's right. To add text to the end of a text widget,
>you can use "end" instead of "insert", e.g.

Is there a list of these anywhere?

>> Can I attach zip to "self" somehow in funcx, e.g., line 3:
>> 'self.zip = "googoo"'?
>Yes, you can certainly do that. It's the usual way
>of assigning to an instance variable in Python.
>
>> Will it only work if self.zip is defined in the
>> __init__ func?
>No, there's no need to do that. You can add a new
>attribute to any instance of any class at any time
>just by assigning to it. There is no notion of
>"declaring" instance variables.

And you don't even need to be in the class definition!

>>> class test:
...     def spam( self ):
...             print self.eggs
...
>>> thistest = test()
>>> thistest.spam()
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in spam
AttributeError: eggs
>>> thistest.eggs = "I like eggs!"
>>> thistest.spam()
I like eggs!
>>>

So, if you added the requisite "self."s to your example, it would give
you similar behaviour.  (Especially if you called testy before testx.)

Later,
Blake.

-- 
One Will.  One Dream.  One Truth.  One Destiny.  One Love.




More information about the Python-list mailing list