Re: [Tutor] Bitmap editor and File-saving

2007-11-11 Thread Alan Gauld

Johnston Jiaa [EMAIL PROTECTED] wrote

 I am using Tkinter to create a program in which the user can draw a
 simple bitmap image.  What library would be best to use in this
 circumstance?  I have no idea how I would go about doing this except
 maybe I would use Tkinter's canvas component?

The canvas widget is probably adequate for simple drawing.
More advanvced libraries like PIL might be useful for converting
the format or doing large scale transformations.

 In order for the program to be useful, it should save the picture
 too.  I'd like for it to save the image with some strings of text.
 How can I implement such a file-saving feature?

You could either save two files one with the text and the other
the bitmap. Or you could convert the text to binary data and
write it to the same file as the bitmap. To read it back you will
need to write code to strip the text off again however. Or you
could use uuencode, or similar, to convert the image to text
and add your strings to that. Again you will need to write the
reverse process for retrieving it.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] Wrong version of Python being executed

2007-11-11 Thread Tony Cappellini
Martin Walsh mwalsh at groktech.org
Sun Nov 11 06:13:10 CET 2007

That is odd.

Try using the full path to python, just to be sure: c:\python25\python
script.py -- do you get the same behavior?
This works just fine- I would expect it to.

Also, if you haven't already, you can run python with the -E and/or -S
flags (ex. 'c:\python25\python -E -S script.py'). The -E flag will cause
the PYTHONPATH and PYTHONHOME environment variables to be ignored. And

This also works just fine. I've tried both switches independently, and
the scrip runs normally when I use either and both at the same time.
If I don't use them, then Python2.3 is being invoked somehow.

However, when I type set PYTHONPATH and
set PYTHONHOME

at the cmd prompt

SET PYTHONPATH
Environment variable PYTHONPATH not defined

SET PYTHONHOME
Environment variable PYTHONHOME not defined

Very strange indeed. It's starting to remind me of an episode from The
Twilight Zone ;-)

Is ti possible that my registry is corrupted?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Wrong version of Python being executed

2007-11-11 Thread Martin Walsh
Tony Cappellini wrote:
 Martin Walsh mwalsh at groktech.org
 Sun Nov 11 06:13:10 CET 2007
 
 That is odd.
 
 Try using the full path to python, just to be sure: c:\python25\python
 script.py -- do you get the same behavior?
 This works just fine- I would expect it to.

Actually, I would have expected the opposite. My initial thought based
on your description was that python2.5 is being invoked with PYTHON* env
vars from a previous install, or some site module weirdness. But, the
fact that running python.exe with it's full path corrects the issue,
seems to indicate a problem with your PATH, rather than any python
specific environment setting. What does 'set PATH' report?

Though this still doesn't explain why you get python2.5 interactively,
and python2.3 when running a script -- perhaps I'm still unclear what
you are seeing. Would the following be an accurate description of the
behavior?

assuming:
- you run inside a fresh 'cmd' console each time (typing 'cmd' at the
run dialog, or similar), to be sure there is no app environment kruft

- the current working directory doesn't contain any programs, scripts or
possibly links that could interfere (preferably an empty path)

- you don't have any PYTHON* environment vars set (including PYTHONSTARTUP)

you observe:
- when you type 'python' (only 'python') at the prompt, you get
python2.5 interactively

- when you use the form 'python script.py', the script is run with
python2.3 (can you verify with sys.version?) with sys.path appropriate
for 2.3

- when you use the form 'c:\python25\python.exe script.py', the script
is executed with python2.5 and you have the correct sys.path (for 2.5)

 
 Also, if you haven't already, you can run python with the -E and/or -S
 flags (ex. 'c:\python25\python -E -S script.py'). The -E flag will cause
 the PYTHONPATH and PYTHONHOME environment variables to be ignored. And
 
 This also works just fine. I've tried both switches independently, and
 the scrip runs normally when I use either and both at the same time.
 If I don't use them, then Python2.3 is being invoked somehow.

Yeah, very odd indeed.

  Very strange indeed. It's starting to remind me of an episode from The
 Twilight Zone ;-)
 
 Is ti possible that my registry is corrupted?

I wouldn't think so, but I suppose it is possible. I believe all the
pertinent registry keys are store under
HKLM\Software\Python\Pythoncore\version, so you could have a look.
There are settings stored elsewhere, but I think they are all related to
file associations, and enabling double-click launching etc. I hope
someone will correct or clarify, if I'm wrong.

If it is a registry issue, re-installing python2.5 *may* provide a quick
fix. BTW, are you using an alternate distribution of python (ex.
ActiveState), or the standard python.org version?

HTH,
Marty
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Problem with default arguments for function

2007-11-11 Thread Dick Moores
I just discovered mpmath 
(http://code.google.com/p/mpmath/wiki/Documentation) and am trying 
to write a useful function that uses it to compute factorials. Here's 
what I have:

def fact(n, precision=15, full=False):
 
 compute n!
 
 from mpmath import mpf
 if not isinstance(n, int):
 return None
 elif n  0:
 return None
 n = mpf(n)
 if full:
 precision = n * 10   # ensures that for n  1 billion, 
product will not be in scientific notation (i.e., no '+')
 mpf.dps = precision
 product = mpf(1)
 while n  mpf(1):
 product *= n
 n -= 1
 if '+' in str(product):
 return product
 else:
 return int(str(product)[:-2])  #  product is a float, 
ending in '.0', so this needs to be removed and string converted to int.

The code is also at http://python.pastebin.com/m33d52d54.

And a couple of example uses that illustrate my problem:

# 1 (precision default overridden and set to 20; full left at default of False)
  print fact(50, 20)
3.0414093201713378044e+64

# 2 (both precision and full left at their defaults)
  print fact(50)
3.04140932017134e+64

# 3 (full set to True, forcing precision to be specified--but 
irrelevant what it is set to)
  print fact(50, 3, True)
30414093201713378043612608166064768844377641568960512

# 4 (see # 3)
  print fact(50, 30, True)
30414093201713378043612608166064768844377641568960512

And if the function is rewritten as def fact(n, full=False, precision=15)
there would be the analogous problem involving full.

Is there a way to solve this problem of the unnecessary setting of 
the 2nd argument?

Thanks,

Dick Moores

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


Re: [Tutor] Problem with default arguments for function

2007-11-11 Thread Alan Gauld

Dick Moores [EMAIL PROTECTED] wrote

 And if the function is rewritten as def fact(n, full=False, 
 precision=15)
 there would be the analogous problem involving full.

 Is there a way to solve this problem of the unnecessary setting of
 the 2nd argument?

The most common solution I've seen is to write wrappers
around the basic function like:

def fullFact(n, precision=15):
return fact(n,True,precision)

def precisionFact(n, full=False):
return fact(n, full, 15)

Not terribly elegant but is at least short and simple.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] Wrong version of Python being executed

2007-11-11 Thread Alan Gauld
Martin Walsh [EMAIL PROTECTED] wrote

 Try using the full path to python, just to be sure: 
 c:\python25\python
 script.py -- do you get the same behavior?
 This works just fine- I would expect it to.

 Actually, I would have expected the opposite.

Me too.

 Though this still doesn't explain why you get python2.5 
 interactively,
 and python2.3 when running a script -- perhaps I'm still unclear 
 what
 you are seeing. Would the following be an accurate description of 
 the
 behavior?

 assuming:
 - you run inside a fresh 'cmd' console each time (typing 'cmd' at 
 the
 run dialog, or similar), to be sure there is no app environment 
 kruft

This is very important. If you just type python at the Run
dialog it uses a different algorithm to find the exe than if
you type python at a DOS prompt inside a CMD window.
Its the second algorithm that is used if you type
python foo.py at a cmd prompt but the first that
is used if you double click foo.py within explorer
(actually that could even be a different one again!)
or type python foo.py in the Run dialog.

 fix. BTW, are you using an alternate distribution of python (ex.
 ActiveState), or the standard python.org version?

That *shouldn't* make any difference...but you can never be 100% sure!


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] manipulating data

2007-11-11 Thread Bryan Fodness
Using,

fields = {}
for line in open('data.txt') :
   if line :
   if line.split()[0] == 'field' :
   field = int(line.split()[-1])
   else :
   fields[field] = tuple(line.split())

I get,

fields[field] = tuple(line.split())
NameError: name 'field' is not defined




On Nov 8, 2007 7:34 AM, Ricardo Aráoz [EMAIL PROTECTED] wrote:

 Kent Johnson wrote:
  Bryan Fodness wrote:
  I would like to have my data in a format so that I can create a contour 
  plot.
 
  My data is in a file with a format, where there may be multiple fields
 
  field = 1
 
  1a   0
 
  If your data is really this regular, it is pretty easy to parse. A
  useful technique is to access a file's next method directly. Something
  like this (not tested!):
 
  f = open('data.txt')
  fields = {} # build a dict of fields
  try:
 while True:
   # Get the field line
   line = f.next()
   field = int(line.split()[-1]) # last part of the line as an int
 
   f.next() # skip blank line
 
   data = {} # for each field, map (row, col) to value
   for i in range(20): # read 20 data lines
 line = f.next()
 ix, value = f.split()
 row = int(ix[:-1])
 col = ix[-1]
 data[row, col] = int(value)
 
   fields[field] = data
 
   f.next()
  except StopIteration:
 pass
 

 Or maybe just (untested) :

 fields = {} # build a dict of fields
 for line in open('data.txt') :
if line :# skip blank lines
if line.split()[0] == 'field' :
field = int(line.split()[-1])
else :
fields[field] = tuple(line.split())

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

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


Re: [Tutor] Problem with default arguments for function

2007-11-11 Thread Kent Johnson
Dick Moores wrote:

 def fact(n, precision=15, full=False):
   ...

 # 3 (full set to True, forcing precision to be specified--but 
 irrelevant what it is set to)
   print fact(50, 3, True)
 30414093201713378043612608166064768844377641568960512

You don't have to specify precision if you pass full as a keyword argument:
fact(50, full=True)

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


Re: [Tutor] Problem with default arguments for function

2007-11-11 Thread Dick Moores
At 04:22 PM 11/11/2007, Alan Gauld wrote:

Dick Moores [EMAIL PROTECTED] wrote

  And if the function is rewritten as def fact(n, full=False,
  precision=15)
  there would be the analogous problem involving full.
 
  Is there a way to solve this problem of the unnecessary setting of
  the 2nd argument?

The most common solution I've seen is to write wrappers
around the basic function like:

def fullFact(n, precision=15):
 return fact(n,True,precision)

def precisionFact(n, full=False):
 return fact(n, full, 15)

Not terribly elegant but is at least short and simple.

Alan, I won't adopt this solution for my fact(), but I'm glad to know about it.

Thanks,

Dick


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


Re: [Tutor] Problem with default arguments for function

2007-11-11 Thread Dick Moores
At 05:38 PM 11/11/2007, Kent Johnson wrote:
Dick Moores wrote:

def fact(n, precision=15, full=False):
   ...

# 3 (full set to True, forcing precision to be specified--but 
irrelevant what it is set to)
   print fact(50, 3, True)
30414093201713378043612608166064768844377641568960512

You don't have to specify precision if you pass full as a keyword argument:
fact(50, full=True)

Ah, good to know. Thanks.

Dick


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


Re: [Tutor] Problem with default arguments for function

2007-11-11 Thread Dick Moores
At 05:10 PM 11/11/2007, Michael H. Goldwasser wrote:


Dick,

Another typical strategy is to use some prescribed special value for
the precision parameter to designate the desire for full precision.
For example, since precisions should presumably be positive, one could
design this function as:

def fact(n, precision=15):
 compute n!.

 precisionthe minimum desired precision.
  If -1 is specified, computed to full precision.
 
 # ...
 if precision == -1:
 precision = n * 10  # insures that for n  1 billion, ...
 # ...


If you are not happy with the oddity of -1 (or in cases where -1 might
be a legitimate parameter value), you can pick the flag from a
different data type.  In this case, perhaps None would be a more
natural way to say that you do not want any limit on the precision.
So this could be coded as

def fact(n, precision=15):
 compute n!.

 precisionthe minimum desired precision.
  If None is specified, computed to full precision.
 
 # ...
 if precision is None:
 precision = n * 10  # insures that for n  1 billion, ...
 # ...

Looking at your examples, this should (unteste) behave as:

# 1 (precision default overridden and set to 20)
   print fact(50, 20)
3.0414093201713378044e+64

# 2 (without explicit value, precision defaults to 15)
   print fact(50)
3.04140932017134e+64

# 3 (explicitly says not to limit precision)
   print fact(50, None)
30414093201713378043612608166064768844377641568960512

Beautiful! Thanks!

Dick


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


[Tutor] Problem with default arguments for function

2007-11-11 Thread Michael H. Goldwasser


Dick,

Another typical strategy is to use some prescribed special value for
the precision parameter to designate the desire for full precision.
For example, since precisions should presumably be positive, one could
design this function as:

def fact(n, precision=15):
compute n!.

precisionthe minimum desired precision.
 If -1 is specified, computed to full precision.

# ...
if precision == -1:
precision = n * 10  # insures that for n  1 billion, ...
# ...


If you are not happy with the oddity of -1 (or in cases where -1 might
be a legitimate parameter value), you can pick the flag from a
different data type.  In this case, perhaps None would be a more
natural way to say that you do not want any limit on the precision.
So this could be coded as

def fact(n, precision=15):
compute n!.

precisionthe minimum desired precision.
 If None is specified, computed to full precision.

# ...
if precision is None:
precision = n * 10  # insures that for n  1 billion, ...
# ...

Looking at your examples, this should (unteste) behave as:

# 1 (precision default overridden and set to 20)
  print fact(50, 20)
3.0414093201713378044e+64

# 2 (without explicit value, precision defaults to 15)
  print fact(50)
3.04140932017134e+64

# 3 (explicitly says not to limit precision)
  print fact(50, None)
30414093201713378043612608166064768844377641568960512


With regard,
Michael

On Sunday November 11, 2007, Dick Moores wrote: 

def fact(n, precision=15, full=False):
 
 compute n!
 


And a couple of example uses that illustrate my problem:

# 1 (precision default overridden and set to 20; full left at default of 
 False)
  print fact(50, 20)
3.0414093201713378044e+64

# 2 (both precision and full left at their defaults)
  print fact(50)
3.04140932017134e+64

# 3 (full set to True, forcing precision to be specified--but 
irrelevant what it is set to)
  print fact(50, 3, True)
30414093201713378043612608166064768844377641568960512

# 4 (see # 3)
  print fact(50, 30, True)
30414093201713378043612608166064768844377641568960512

And if the function is rewritten as def fact(n, full=False, precision=15)
there would be the analogous problem involving full.

Is there a way to solve this problem of the unnecessary setting of 
the 2nd argument?

Thanks,

Dick Moores

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

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


[Tutor] [tutor] File format conversion

2007-11-11 Thread Varsha Purohit
Hello All,
   In one application i want to convert format of ascii file to
binary file. And using that binary file in a function of PIL i can
convert it to an image file. So i wanted to know how to convert the
file format in python... is it possible by Numpy ?? And is there any
other alternative to convert an ascii into an bitmap image directly in
PIL without Numpy??

thanks,

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


[Tutor] assignment question

2007-11-11 Thread Ryan Hughes
Hello,

Why does the following not return [1,2,3,4] ?

 x = [1,2,3].append(4)
 print x
None
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] assignment question

2007-11-11 Thread John Fouhy
On 12/11/2007, Ryan Hughes [EMAIL PROTECTED] wrote:
 Why does the following not return [1,2,3,4] ?

  x = [1,2,3].append(4)
  print x
 None

List methods like .append() and .sort() modify lists in-place, as
opposed to creating new lists.  To remind you of this, those methods
return None instead of returning the modified list.

Otherwise (if .append() returned the modified list), you might be
tempted to write code like this:

  x = getSomeList()
  y = x.append(3)

and forget that x and y are the same list.

If you want to join two lists together to make a new one, you can use +:

 x = [1, 2, 3] + [4]
 x
[1, 2, 3, 4]

Hope this helps,

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


Re: [Tutor] Tutor Digest, Vol 45, Issue 30

2007-11-11 Thread Tony Cappellini
 Message: 4
 Date: Mon, 12 Nov 2007 00:16:35 -
 From: Alan Gauld [EMAIL PROTECTED]
 Subject: Re: [Tutor] Wrong version of Python being executed
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; format=flowed; charset=iso-8859-1;
 reply-type=original


 This is very important. If you just type python at the Run
 dialog it uses a different algorithm to find the exe than if
 you type python at a DOS prompt inside a CMD window.

I never type anything in the Run dialog other than Regedit, or CMD.
I dont want that to sound defensive, but usually when I need a CMD
prompt, I'm need to be there for a long time, and that's why I open
it.
It never even occurred to me to type Python at the Run prompt.

Most of the time, I open a cmd prompt IN the directory where I want to
run a script, by using Command Prompt Here, from Microsoft.
(why isn't this built into the OS after all this time ??)

But while troubleshooting this problem, I've not been using it, and
have been CD'ing down to where my sources are.

 Its the second algorithm that is used if you type
 python foo.py at a cmd prompt but the first that
 is used if you double click foo.py within explorer
I typically dont double click my scripts, unless they are guis, or exes'

 That *shouldn't* make any difference...but you can never be 100% sure!

I think a this point, I'll just re-install 2.3 and 2.5 to see if that
fixes it, with a reboot in-between.
I dont know what else to do.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] assignment question

2007-11-11 Thread Michael H. Goldwasser

On Sunday November 11, 2007, Ryan Hughes wrote: 

Hello,

Why does the following not return [1,2,3,4] ?

 x = [1,2,3].append(4)
 print x
None


The reason is that the append method does not return anything.  In
effect, the expresison [1,2,3].append(4) temporarily causes 4 to be
appended to the (unnamed) list.  However the assignment

x = blah

sets the variable x equal to the result of the expression blah (which
in this case is None, since append does not return anything).

In contrast, consider the following interaction:

 x = [1,2,3]
 x.append(4)
 print x
[1, 2, 3, 4]




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


Re: [Tutor] Wrong version of Python being executed

2007-11-11 Thread Tony Cappellini
Message: 2
Date: Sun, 11 Nov 2007 16:57:01 -0600
From: Martin Walsh [EMAIL PROTECTED]
Subject: Re: [Tutor] Wrong version of Python being executed
To: Tutor Python tutor@python.org
Message-ID: [EMAIL PROTECTED]
Content-Type: text/plain; charset=ISO-8859-1


My initial thought based on your description was that python2.5 is
being invoked with PYTHON* env
vars from a previous install, or some site module weirdness.

Not sure how the previous install surfaced, but I installed all
python installations and packages on my computer.

I have to switch between 2.3 and 2.5, so to make it easy, I use an
environment variable called CURRENT_PYTHON.
(someone on this list or the wxPython list told me I should NOT use
PYTHONPATH and modify it the way I am using CURRENT_PYTHON)

CURRENT_PYTHON=C:\PYTHON2X
path=%CURRENT_PYTHON%
(The existing path isn't shown, only for brevity)

Note, these are entered in Ctrl Panel, System environment variables,
NOT at the command line.


But, the fact that running python.exe with it's full path corrects the issue,
seems to indicate a problem with your PATH, rather than any python
Yes, my conclusion also, but what is wrong with the path?
I've posted the contents of PATH in the original email or one of the
subsequent ones.

Would the following be an accurate description of the behavior?

assuming:
- you run inside a fresh 'cmd' console each time (typing 'cmd' at the
run dialog, or similar), to be sure there is no app environment kruft

correct-

- the current working directory doesn't contain any programs, scripts or
possibly links that could interfere (preferably an empty path)

Not that I can see

- you don't have any PYTHON* environment vars set (including PYTHONSTARTUP)

No- see for yourself. No python anything variables.
C:\Documents and Settings\z30032asset python
Environment variable python not defined

you observe:
- when you type 'python' (only 'python') at the prompt, you get
python2.5 interactively

Correct

- when you use the form 'python script.py', the script is run with
python2.3 (can you verify with sys.version?) with sys.path appropriate
for 2.3

Correct

- when you use the form 'c:\python25\python.exe script.py', the script
is executed with python2.5 and you have the correct sys.path (for 2.5)
Correct

I wouldn't think so, but I suppose it is possible. I believe all the
pertinent registry keys are store under
HKLM\Software\Python\Pythoncore\version, so you could have a look.

Already did. There IS a PYTHONPATH entry for each of the 3 versions of
Python I have installed.
They are all identical, with the exception of the last 2 digits for
the Python version.
This looks ok to me. No other rogue entries of PYTHONPATH were found
in the registry.

BTW, are you using an alternate distribution of python (ex.
ActiveState), or the standard python.org version?

No. I always use the regular Python.Org distributions. I never
understood what was so special about ActiveState anyway.
I;d rather have full control of my installs, and all the packages.
Although, at this point, I'm not in control over what is happening
with my path ;-)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] assignment question

2007-11-11 Thread Steve Willoughby
Ryan Hughes wrote:
 Hello,
 
 Why does the following not return [1,2,3,4] ?
 
 x = [1,2,3].append(4)
 print x
 None

because the append() method doesn't return a copy of the list object; it 
just modifies the list itself.

so your code constructs a list object with 3 elements, appends a fourth 
element to it, and throws that list object away, keeping only the return 
value of append().

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


Re: [Tutor] assignment question

2007-11-11 Thread Jeff Younker

Append modifies the array as a side effect.  It has no
meaningful return value.

 x = [1, 2, 3]
 x.append(4)
 print x
[1, 2, 3, 4]

- Jeff Younker - [EMAIL PROTECTED] - 510.798.5804 -


On Nov 11, 2007, at 8:21 PM, Ryan Hughes wrote:



Hello,

Why does the following not return [1,2,3,4] ?

 x = [1,2,3].append(4)
 print x
None
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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


Re: [Tutor] Swapping variables ...

2007-11-11 Thread wesley chun
On 11/10/07, O.R.Senthil Kumaran [EMAIL PROTECTED] wrote:
   After quizzing newbies in C on swapping without 3rd variable, I found this
  to be really *cool* construct to swap :)
  x = 10
  y = 20
  x,y = y,x

 Keep in mind that, this is actually a tuple assignment.
 A new tuple x,y is created with the older one y,x, and with the side effect 
 thatthe variables are swapped as we see it.

you can dig even *deeper* within the tuple assignment.  keep in mind
that the right-hand side (RHS) is always evaluated 1st, whenever there
is an assignment operation.  an alias or reference mapping is made
using the variable name (in the current or designated namespace) to
whatever object appears on the RHS.

this means that 'x' on the LHS gets whatever object 'y' was
referencing during evaluation time, and likewise, 'y' becomes an alias
to whatever object 'x' was pointing to.  it's just a coincidence
that the new mapping variables are just reversed to what they were
previously. the interpreter makes no special distinction just because
this is the case.

if you are still thinking of variables as data structures which
contain values, you need to unlearn what you have learned. :-)
variables in Python are merely references to objects and such
references can be manipulated at will. for example, a variable 'a' as
in 'a = 123' can be immediately reassigned to a string, i.e., a =
'foo'. getting comfortable with objects, references, and the mapping
of names to objects are some ways of understanding Python more fully
and with greater success.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor