[Tutor] more newbie questions: variables without a value

Deirdre Saoirse Moen deirdre@deirdre.net
Thu, 28 Jun 2001 12:19:23 -0700


At 9:09 PM +0200 6/28/01, Brendon wrote:
>in the following code..
>----
>#!/usr/bin/python
>
>class Message:
>	def __init__(self, aString):
>		self.text = aString
>	def printIt(self):
>		print self.text
>
>m1 = Message("Hello World")
>m2 = Message("So long, it was short bu sweet.")
>
>note = [m1, m2]
>for msg in note:
>	msg.printIt()
>----
>
>the variable 'msg' was never given a value, yet msg.printIt() displays the
>list note. how is this?

It is. msg is declared in the "for msg in note"

Meaning that, for each value of note, set msg equal to that.

>the line: "for msg in note:" doesn't seem to make sense because "msg" has no
>value nor is it pointing to "note".

There's an implied assignment that the first variable is assigned to 
the current value of the second part. Like:

for i in xrange(25, 51, 5):
  print i

-- 
_Deirdre    Stash-o-Matic: http://weirdre.com    http://deirdre.net
"Cannot run out of time.... Is infinite time. You... are finite....
Zathrus... is finite. This... is wrong tool!" -- Zathrus