<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<id>tag:old.nabble.com,2006:forum-2981</id>
	<title>Nabble - Python - tutor</title>
	<updated>2009-11-30T17:04:10Z</updated>
	<link rel="self" type="application/atom+xml" href="http://old.nabble.com/Python---tutor-f2981.xml" />
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Python---tutor-f2981.html" />
	<subtitle type="html">Discussion for learning programming with Python</subtitle>
	
<entry>
	<id>tag:old.nabble.com,2006:post-26584956</id>
	<title>Re: x is a global variable</title>
	<published>2009-11-30T17:04:10Z</published>
	<updated>2009-11-30T17:04:10Z</updated>
	<author>
		<name>Dave Angel</name>
	</author>
	<content type="html">spir wrote:
&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; Hello Eike!
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; Eike Welk &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26584956&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;eike.welk@...&lt;/a&gt;&amp;gt; dixit:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp; 
&lt;br&gt;&amp;gt; &amp;lt;snip&amp;gt;
&lt;br&gt;&amp;gt; Well, this is certainly not specific to closures.
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; i = 0
&lt;br&gt;&amp;gt; def f():
&lt;br&gt;&amp;gt; &amp;nbsp; i = i+1
&lt;br&gt;&amp;gt; &amp;nbsp; print i
&lt;br&gt;&amp;gt; f()
&lt;br&gt;&amp;gt; ==&amp;gt; UnboundLocalError
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; Imo, in this case, &amp;quot;i = i+1&amp;quot; is a kind of &amp;quot;paradoxal injonction&amp;quot; (lol! not sure of the exact idiom in english). You tell python both to create a local i (thus ignore any other scope to lookup for variables called 'i') and to use global i to define the local one.
&lt;br&gt;&amp;gt; If I were the victim of such a &amp;quot;paradoxal injonction&amp;quot; I would reply with a naughty word!
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp; 
&lt;br&gt;&amp;gt;
&lt;/div&gt;I believe the easiest model to understand the behavior is:
&lt;br&gt;&lt;br&gt;The compiler scans the entire function to find which variables are 
&lt;br&gt;assigned (via =, as, or other syntax) anywhere in the function. &amp;nbsp;Then 
&lt;br&gt;for those variables, all references are done without any dictionary 
&lt;br&gt;accesses. &amp;nbsp;Thus an assignment anywhere (not just on the same line) cause 
&lt;br&gt;all references to be to the (unbound) local.
&lt;br&gt;&lt;br&gt;DaveA
&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26584956&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/python-closures-tp26572340p26584956.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26584864</id>
	<title>Re: python closures</title>
	<published>2009-11-30T16:50:53Z</published>
	<updated>2009-11-30T16:50:53Z</updated>
	<author>
		<name>Dave Angel</name>
	</author>
	<content type="html">Alan Gauld wrote:
&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; &amp;lt;div class=&amp;quot;moz-text-flowed&amp;quot; style=&amp;quot;font-family: -moz-fixed&amp;quot;&amp;gt;
&lt;br&gt;&amp;gt; &amp;quot;spir&amp;quot; &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26584864&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;denis.spir@...&lt;/a&gt;&amp;gt; wrote
&lt;br&gt;&amp;gt; &amp;lt;snip&amp;gt;
&lt;br&gt;&amp;gt; I did wonder if you would need n=n but I didn't think you would need x=x.
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; Its an interesting example and I confess I don't fully understand how 
&lt;br&gt;&amp;gt; Python's
&lt;br&gt;&amp;gt; naming/reference rules are working here.
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt;&amp;gt; to let the inner func g0 &amp;quot;remember&amp;quot; outer values.
&lt;br&gt;&amp;gt;&amp;gt; Why is this idiom used, then? Has something changed, or do I miss
&lt;br&gt;&amp;gt;&amp;gt; a relevant point?
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; I thought you might need to do it if n had been a parameter of f()... but
&lt;br&gt;&amp;gt; having tried it no, it works as above.
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; I look forward to the explanation.
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; Alan G.
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt;
&lt;/div&gt;Maybe a more complex example might show the various linkages.
&lt;br&gt;&lt;br&gt;glob = 42
&lt;br&gt;&lt;br&gt;def outer(parm1):
&lt;br&gt;&amp;nbsp; &amp;nbsp; free = 12
&lt;br&gt;&amp;nbsp; &amp;nbsp; free3 = 19
&lt;br&gt;&amp;nbsp; &amp;nbsp; def inner(parm2, parm3=free3):
&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print &amp;quot;global&amp;quot;, glob, &amp;quot;, free vars&amp;quot;, parm1, free, free3, &amp;quot;, 
&lt;br&gt;locals&amp;quot;, parm2, parm3
&lt;br&gt;&amp;nbsp; &amp;nbsp; free = 49
&lt;br&gt;&amp;nbsp; &amp;nbsp; free3 = 48
&lt;br&gt;&amp;nbsp; &amp;nbsp; return inner
&lt;br&gt;&lt;br&gt;newfunc = outer(10)
&lt;br&gt;newfunc(45)
&lt;br&gt;&lt;br&gt;&lt;br&gt;produces output:
&lt;br&gt;&amp;nbsp; &amp;nbsp;global 42 , free vars 10 49 48 , locals 45 19
&lt;br&gt;&lt;br&gt;So when the inner() function is actually called, glob is just a global. &amp;nbsp;
&lt;br&gt;parm1, fre, and free3 hold the values they ended up with when outer() 
&lt;br&gt;returned, and local parm2 is passed by top-level code, while local parm3 
&lt;br&gt;gets its default value assigned when &amp;quot;def inner(...) was executed.
&lt;br&gt;&lt;br&gt;Notice that the free variables free, free3, and parm1 are referring to 
&lt;br&gt;the function's ending state, not to the state when the function was 
&lt;br&gt;defined. &amp;nbsp;This has an impact when you've got inner being defined in a 
&lt;br&gt;loop. &amp;nbsp;And this example could be made more complex if outer() is a 
&lt;br&gt;generator, in which case it may not have actually ended when inner gets 
&lt;br&gt;called.
&lt;br&gt;&lt;br&gt;HTH
&lt;br&gt;DaveA
&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26584864&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/python-closures-tp26572340p26584864.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26584427</id>
	<title>read in ascii and plot</title>
	<published>2009-11-30T15:55:10Z</published>
	<updated>2009-11-30T15:55:10Z</updated>
	<author>
		<name>questions anon</name>
	</author>
	<content type="html">I would like to read in two columns of data from a *.txt file&lt;br&gt;&lt;br&gt;I type&lt;br&gt;&lt;br&gt;f=open(&amp;quot;e:/testascii.txt&amp;quot;)&lt;br&gt;import pylab&lt;br&gt;pylab.scatter(f)&lt;br&gt;&lt;br&gt;and then receive an error.&lt;br&gt;How do I point it to each column and do I need to do anything about the space gap between the two columns?&lt;br&gt;
Thanks in advance. &lt;br&gt;
&lt;br /&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26584427&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/read-in-ascii-and-plot-tp26584427p26584427.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26584199</id>
	<title>Re: numerical simulation + SQLite</title>
	<published>2009-11-30T15:37:11Z</published>
	<updated>2009-11-30T15:37:11Z</updated>
	<author>
		<name>Eike Welk-2</name>
	</author>
	<content type="html">Hello Faisal!
&lt;br&gt;&lt;br&gt;Just in case you don't know it, maybe Pytables is the right solution 
&lt;br&gt;for you. It is a disk storage library specially for scientific 
&lt;br&gt;applications:
&lt;br&gt;&lt;a href=&quot;http://www.pytables.org/moin&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://www.pytables.org/moin&lt;/a&gt;&lt;br&gt;&lt;br&gt;The makers claim, that it is fast. It has on the fly data compression 
&lt;br&gt;which allegedly makes the library faster because fewer data has to be 
&lt;br&gt;written to disk. 
&lt;br&gt;&lt;br&gt;The lead developer gave a talk about the topic with interesting 
&lt;br&gt;slides. He proposes to compress not only data that is stored on 
&lt;br&gt;disks, but also data resides in RAM; because even RAM is very slow 
&lt;br&gt;compared to the speed of modern processors. 
&lt;br&gt;(The slides are near the top of the page.)
&lt;br&gt;&lt;a href=&quot;http://www.euroscipy.org/presentations/abstracts/abstract_alted.html&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://www.euroscipy.org/presentations/abstracts/abstract_alted.html&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;Eike.
&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26584199&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/numerical-simulation-%2B-SQLite-tp26580995p26584199.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26583197</id>
	<title>Re: numerical simulation + SQLite</title>
	<published>2009-11-30T14:05:50Z</published>
	<updated>2009-11-30T14:05:50Z</updated>
	<author>
		<name>Alan Gauld</name>
	</author>
	<content type="html">&amp;quot;Faisal Moledina&amp;quot; &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26583197&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;faisal.moledina@...&lt;/a&gt;&amp;gt; wrote 
&lt;br&gt;.
&lt;br&gt;&amp;gt; At first, I used a list to store a bunch of class instances, each of
&lt;br&gt;&amp;gt; which contained a bunch of data calculated at each time step. This
&lt;br&gt;&amp;gt; resulted in whopping memory usage (2.75 GB RAM, 3.75 GB VM).
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; So then I decided instead to use SQLite to store that information at
&lt;br&gt;&amp;gt; each timestep. This seems to work well, but it gets slow over time as
&lt;br&gt;&amp;gt; well. 
&lt;br&gt;&lt;br&gt;You may need to be realistic in your expectations.
&lt;br&gt;A database is writing to disk which will be slower than working in 
&lt;br&gt;memory. And a 3GB file takes a while to read/traverse, even with 
&lt;br&gt;indexes. It depends a lot on exactly what you are doing. If its mainly 
&lt;br&gt;writing it should not be much slower than writing to a flat file. If you 
&lt;br&gt;are doing a lot of reading - and you have used indexes - then it 
&lt;br&gt;should be a lot faster than a file.
&lt;br&gt;&lt;br&gt;But RAM - if you have enough - will always be fastest, by about 100 times.
&lt;br&gt;The problem is when you run out, you revert to using files and that's 
&lt;br&gt;usually slower than a database...
&lt;br&gt;&lt;br&gt;But without details of your usage pattern and database schema 
&lt;br&gt;and SQL code etc it is, as you say, impossible to be specific.
&lt;br&gt;&lt;br&gt;-- 
&lt;br&gt;Alan Gauld
&lt;br&gt;Author of the Learn to Program web site
&lt;br&gt;&lt;a href=&quot;http://www.alan-g.me.uk/&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://www.alan-g.me.uk/&lt;/a&gt;&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26583197&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/numerical-simulation-%2B-SQLite-tp26580995p26583197.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26583116</id>
	<title>Re: Equivalent exception of os.path.exists()</title>
	<published>2009-11-30T13:59:53Z</published>
	<updated>2009-11-30T13:59:53Z</updated>
	<author>
		<name>Alan Gauld</name>
	</author>
	<content type="html">&lt;br&gt;&amp;quot;Dave Angel&amp;quot; &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26583116&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;davea@...&lt;/a&gt;&amp;gt; wrote
&lt;br&gt;&lt;br&gt;&amp;gt; My suspicion is that the author just wants you to try to do an open() 
&lt;br&gt;&amp;gt; (readonly), inside a try/except block, and to successfully handle the 
&lt;br&gt;&amp;gt; exception. &amp;nbsp;
&lt;br&gt;&lt;br&gt;And the critical thing here is that it must be an open in read mode. 
&lt;br&gt;As others pointed out if you use write mode it will (nearly) always 
&lt;br&gt;succeed, even if that means overwriting an existing file.
&lt;br&gt;&lt;br&gt;Alan G.
&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26583116&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Equivalent-exception-of-os.path.exists%28%29-tp26576284p26583116.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26583009</id>
	<title>Re: Equivalent exception of os.path.exists()</title>
	<published>2009-11-30T13:43:00Z</published>
	<updated>2009-11-30T13:43:00Z</updated>
	<author>
		<name>christopher.henk-2</name>
	</author>
	<content type="html">
&lt;br&gt;
&lt;br&gt;&lt;font size=2&gt;&lt;tt&gt;biboy mendz &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26583009&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;bibsmendez@...&lt;/a&gt;&amp;gt; wrote on
11/30/2009 03:04:52 PM:&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; spir wrote:&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; What is your question?&lt;br&gt;
&amp;gt; &amp;gt; If it's about the type of exception raised when os.path.exists
fails, well, sure it's hard to find:&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;nbsp; print os.path.exists(&amp;quot;foo&amp;quot;.bar)&lt;br&gt;
&amp;gt; &amp;gt; &amp;nbsp; ==&amp;gt; False&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;nbsp; &lt;br&gt;
&amp;gt; My question is: i'm looking for type of exception that more or less
&lt;br&gt;
&amp;gt; equivalent to os.path.exists attribute. I know for a fact that this
&lt;br&gt;
&amp;gt; method yields true or false. The exercise in the book says use &lt;br&gt;
&amp;gt; try-except in place of os.path.exists(). That sure is (to me) quite
&lt;br&gt;
&amp;gt; difficult task. Instead of using the available function you're are
&lt;br&gt;
&amp;gt; tasked to do the alternative.&lt;br&gt;
&amp;gt; &lt;/tt&gt;&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;&lt;font size=2&gt;&lt;tt&gt;Looking at my copy of Core Python the exercise says
to follow the example 3.2 (readTextFile.py). &amp;nbsp;There he uses the try
except block around the open function call. &amp;nbsp;&lt;/tt&gt;&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;&lt;font size=2&gt;&lt;tt&gt;This as mentioned could raise an IOError. &amp;nbsp;It
will be triggered in a different manner then os.path.exist since you are
now trying to open a file to write, instead of checking if it exist.&lt;/tt&gt;&lt;/font&gt;
&lt;br&gt;&lt;br /&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26583009&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Equivalent-exception-of-os.path.exists%28%29-tp26576284p26583009.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26582679</id>
	<title>Re: Equivalent exception of os.path.exists()</title>
	<published>2009-11-30T13:25:33Z</published>
	<updated>2009-11-30T13:25:33Z</updated>
	<author>
		<name>spir</name>
	</author>
	<content type="html">biboy mendz &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26582679&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;bibsmendez@...&lt;/a&gt;&amp;gt; dixit:
&lt;br&gt;&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; spir wrote:
&lt;br&gt;&amp;gt; &amp;gt;
&lt;br&gt;&amp;gt; &amp;gt; What is your question?
&lt;br&gt;&amp;gt; &amp;gt; If it's about the type of exception raised when os.path.exists fails, well, sure it's hard to find:
&lt;br&gt;&amp;gt; &amp;gt;
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp; print os.path.exists(&amp;quot;foo&amp;quot;.bar)
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp; ==&amp;gt; False
&lt;br&gt;&amp;gt; &amp;gt;
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp; 
&lt;br&gt;&amp;gt; My question is: i'm looking for type of exception that more or less 
&lt;br&gt;&amp;gt; equivalent to os.path.exists attribute. I know for a fact that this 
&lt;br&gt;&amp;gt; method yields true or false. The exercise in the book says use 
&lt;br&gt;&amp;gt; try-except in place of os.path.exists(). That sure is (to me) quite 
&lt;br&gt;&amp;gt; difficult task. Instead of using the available function you're are 
&lt;br&gt;&amp;gt; tasked to do the alternative.
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; Lie and Tim's input are true that raw_input doesnt do anything or you 
&lt;br&gt;&amp;gt; cant catch exception error from it. And i'm wrong in placing the 
&lt;br&gt;&amp;gt; try-except clause, it should be on the fobj-open line. But im still 
&lt;br&gt;&amp;gt; looking for the exception that will be raised when i input a filename 
&lt;br&gt;&amp;gt; and that file already exists. I hope you get what i mean :-)
&lt;/div&gt;&lt;br&gt;Right. So, the exercise is about replacing an explicit check by try...except. But unlike what you seem to think above, you won't get any error from file *writing*.
&lt;br&gt;Think at variables: python does not make any distinction between creation or change, so that you you won't get any error by &amp;quot;writing&amp;quot; a variable that already exist, even if your intention was to create a new one (and you wrote a wrong name). Because there is no syntactic difference between creation and change. Conversely, if you create a new variable when you intended to change an existing one, you won't get any nice error, it will be silently processed. Right?
&lt;br&gt;The same with files. So you cannot have such errors at file writing, if the file exists, it will be overwritten. So, you can only use try...except for file *reading*.
&lt;br&gt;See my previous post.
&lt;br&gt;&lt;br&gt;&lt;br&gt;(*) Except in the case when you try to overwrite you don't have (as user) the right to change, or if you try to create a file in a folder you don't have writing right in.
&lt;br&gt;&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; &amp;gt; The output beeing a logical value, there is no failure.
&lt;br&gt;&amp;gt; &amp;gt; A general method to get a type of exception matching a particuliar kind of error is to provoke an error of this given kind, eg:
&lt;br&gt;&amp;gt; &amp;gt;
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp; f = file(&amp;quot;foo.bar&amp;quot;)
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp; ==&amp;gt;
&lt;br&gt;&amp;gt; &amp;gt; Traceback (most recent call last):
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp; File &amp;quot;__essai__.py&amp;quot;, line 10, in &amp;lt;module&amp;gt;
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp; &amp;nbsp; f = file(&amp;quot;foo.bar&amp;quot;)
&lt;br&gt;&amp;gt; &amp;gt; IOError: [Errno 2] No such file or directory: 'foo.bar'
&lt;br&gt;&amp;gt; &amp;gt;
&lt;br&gt;&amp;gt; &amp;gt; In this case, the type is IOError.
&lt;br&gt;&amp;gt; &amp;gt;
&lt;br&gt;&amp;gt; &amp;gt; Denis
&lt;/div&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;________________________________
&lt;br&gt;&lt;br&gt;la vita e estrany
&lt;br&gt;&lt;br&gt;&lt;a href=&quot;http://spir.wikidot.com/&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://spir.wikidot.com/&lt;/a&gt;&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26582679&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Equivalent-exception-of-os.path.exists%28%29-tp26576284p26582679.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26582481</id>
	<title>Re: Equivalent exception of os.path.exists()</title>
	<published>2009-11-30T13:14:54Z</published>
	<updated>2009-11-30T13:14:54Z</updated>
	<author>
		<name>Sander Sweers</name>
	</author>
	<content type="html">2009/11/30 biboy mendz &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26582481&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;bibsmendez@...&lt;/a&gt;&amp;gt;:
&lt;br&gt;&amp;gt; clause, it should be on the fobj-open line. But im still looking for the
&lt;br&gt;&amp;gt; exception that will be raised when i input a filename and that file already
&lt;br&gt;&amp;gt; exists. I hope you get what i mean :-)
&lt;br&gt;&lt;br&gt;There is no exception to alert you a file already exists. Depending on
&lt;br&gt;how you open the file (mode) it will either read, write or append to
&lt;br&gt;the file. If the file exists and you open it in write mode it *will*
&lt;br&gt;overwrite the file. See [1] for more info.
&lt;br&gt;&lt;br&gt;So what you need to do is check yourself if the file exists and make
&lt;br&gt;the python script take appriate action. For example:
&lt;br&gt;&lt;br&gt;import os
&lt;br&gt;fname = &amp;quot;C:\\testfile.txt&amp;quot;
&lt;br&gt;if os.path.exists(fname):
&lt;br&gt;&amp;nbsp; &amp;nbsp; do something if exists
&lt;br&gt;else:
&lt;br&gt;&amp;nbsp; &amp;nbsp;do something else if not exists
&lt;br&gt;&lt;br&gt;Greets
&lt;br&gt;Sander
&lt;br&gt;&lt;br&gt;[1] &lt;a href=&quot;http://docs.python.org/library/functions.html#open&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://docs.python.org/library/functions.html#open&lt;/a&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26582481&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Equivalent-exception-of-os.path.exists%28%29-tp26576284p26582481.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26582382</id>
	<title>Re: python closures</title>
	<published>2009-11-30T13:00:12Z</published>
	<updated>2009-11-30T13:00:12Z</updated>
	<author>
		<name>spir</name>
	</author>
	<content type="html">Eike Welk &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26582382&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;eike.welk@...&lt;/a&gt;&amp;gt; dixit:
&lt;br&gt;&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; On Monday 30 November 2009, Hugo Arts wrote:
&lt;br&gt;&amp;gt; &amp;gt; Consider this python session: &amp;nbsp;
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp;&amp;gt;&amp;gt;&amp;gt; x = 0
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp;&amp;gt;&amp;gt;&amp;gt; def f(): &amp;nbsp;
&lt;br&gt;&amp;gt; &amp;gt;
&lt;br&gt;&amp;gt; &amp;gt; 		x = x + 1
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp;
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp;&amp;gt;&amp;gt;&amp;gt; f() &amp;nbsp;
&lt;br&gt;&amp;gt; &amp;gt;
&lt;br&gt;&amp;gt; &amp;gt; Traceback (most recent call last):
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp; File &amp;quot;&amp;lt;pyshell#27&amp;gt;&amp;quot;, line 1, in &amp;lt;module&amp;gt;
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp; &amp;nbsp; f()
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp; File &amp;quot;&amp;lt;pyshell#26&amp;gt;&amp;quot;, line 2, in f
&lt;br&gt;&amp;gt; &amp;gt; &amp;nbsp; &amp;nbsp; x = x + 1
&lt;br&gt;&amp;gt; &amp;gt; UnboundLocalError: local variable 'x' referenced before assignment &amp;nbsp;
&lt;br&gt;&amp;gt; Ah... what a pity I didn't try this. I used similar code in my 
&lt;br&gt;&amp;gt; response to Spir and thought it would be somehow connected to 
&lt;br&gt;&amp;gt; closures. Sending nonsense statements to the list again... 
&lt;/div&gt;&lt;br&gt;My answer was stupid as well. Actually, guess I hadn't understood the real sense of Hugo's comment... until I tried to answer your post, Eike.
&lt;br&gt;&lt;br&gt;Denis
&lt;br&gt;&lt;br&gt;PS:
&lt;br&gt;I just found by chance an article (actually a pair of) by Paul Graham (Lisp advocate) that really show how pitifully helpless python is (I'm half joking) compared to &amp;quot;Lisp/Perl/Smalltalk/Javascript&amp;quot;.
&lt;br&gt;&lt;a href=&quot;http://www.paulgraham.com/icad.html&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://www.paulgraham.com/icad.html&lt;/a&gt;&amp;nbsp;(see end of article)
&lt;br&gt;&lt;a href=&quot;http://www.paulgraham.com/icadmore.html&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://www.paulgraham.com/icadmore.html&lt;/a&gt;&amp;nbsp;(search Paul Prescod's reply)
&lt;br&gt;&lt;br&gt;Quote:
&lt;br&gt;&amp;lt;&amp;lt; I was actually surprised at how badly Python did. I had never realized, for example, that a Python lambda-expression &amp;nbsp;couldn't contain the same things as a named function, or that *variables from enclosing scopes are visible but not modifiable*. Neither Lisp nor Perl nor Smalltalk nor Javascript impose either restriction. &amp;gt;&amp;gt;
&lt;br&gt;(I highlight)
&lt;br&gt;&lt;br&gt;Please don't take my quoting as an occasion for flamewar -- I'm just quoting to bring an external point of view into the thread. 
&lt;br&gt;&lt;br&gt;An interesting thing he points earlier is that (provided anonymous funcs were real funcs and changing external vars were possible) python,'s idiom for his example would not be:
&lt;br&gt;&lt;br&gt;def accum(n):
&lt;br&gt;&amp;nbsp; &amp;nbsp;lambda i: n += i
&lt;br&gt;&lt;br&gt;but instead:
&lt;br&gt;&lt;br&gt;def accum(n):
&lt;br&gt;&amp;nbsp; &amp;nbsp;return lambda i: return n += i
&lt;br&gt;&lt;br&gt;But actually he's wrong, I guess, it would even be:
&lt;br&gt;&lt;br&gt;def accum(n):
&lt;br&gt;&amp;nbsp; &amp;nbsp;return lambda i: n += i ; return n
&lt;br&gt;&lt;br&gt;(cannot return a statement) 
&lt;br&gt;&lt;br&gt;Denis
&lt;br&gt;________________________________
&lt;br&gt;&lt;br&gt;la vita e estrany
&lt;br&gt;&lt;br&gt;&lt;a href=&quot;http://spir.wikidot.com/&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://spir.wikidot.com/&lt;/a&gt;&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26582382&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/python-closures-tp26572340p26582382.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26582054</id>
	<title>Re: Equivalent exception of os.path.exists()</title>
	<published>2009-11-30T12:46:46Z</published>
	<updated>2009-11-30T12:46:46Z</updated>
	<author>
		<name>Dave Angel</name>
	</author>
	<content type="html">spir wrote:
&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; biboy mendz &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26582054&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;bibsmendez@...&lt;/a&gt;&amp;gt; dixit:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp; 
&lt;br&gt;&amp;gt;&amp;gt; &lt;a href=&quot;http://pastebin.ca/1693849&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://pastebin.ca/1693849&lt;/a&gt;&lt;br&gt;&amp;gt;&amp;gt;
&lt;br&gt;&amp;gt;&amp;gt; This is end-of-chapter3 exercise of the book Core Python Programming.
&lt;br&gt;&amp;gt;&amp;gt;
&lt;br&gt;&amp;gt;&amp;gt; I'm reading/searching in the book and other materials but is 
&lt;br&gt;&amp;gt;&amp;gt; unsuccessful. There are at least 50 exceptions listed but I can't find 
&lt;br&gt;&amp;gt;&amp;gt; anything close.
&lt;br&gt;&amp;gt;&amp;gt;
&lt;br&gt;&amp;gt;&amp;gt; I commented out my modified script to just what *should* be applicable. 
&lt;br&gt;&amp;gt;&amp;gt; Can you please point me to the right direction? TIA.
&lt;br&gt;&amp;gt;&amp;gt;
&lt;br&gt;&amp;gt;&amp;gt;
&lt;br&gt;&amp;gt;&amp;gt; &amp;nbsp; &amp;nbsp; 
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; What is your question?
&lt;br&gt;&amp;gt; If it's about the type of exception raised when os.path.exists fails, well, sure it's hard to find:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp; print os.path.exists(&amp;quot;foo&amp;quot;.bar)
&lt;br&gt;&amp;gt; &amp;nbsp; ==&amp;gt; False
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; The output beeing a logical value, there is no failure.
&lt;br&gt;&amp;gt; A general method to get a type of exception matching a particuliar kind of error is to provoke an error of this given kind, eg:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp; f = file(&amp;quot;foo.bar&amp;quot;)
&lt;br&gt;&amp;gt; &amp;nbsp; ==&amp;gt;
&lt;br&gt;&amp;gt; Traceback (most recent call last):
&lt;br&gt;&amp;gt; &amp;nbsp; File &amp;quot;__essai__.py&amp;quot;, line 10, in &amp;lt;module&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp; &amp;nbsp; f = file(&amp;quot;foo.bar&amp;quot;)
&lt;br&gt;&amp;gt; IOError: [Errno 2] No such file or directory: 'foo.bar'
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; In this case, the type is IOError.
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; Denis
&lt;br&gt;&amp;gt; ________________________________
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; la vita e estrany
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; &lt;a href=&quot;http://spir.wikidot.com/&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://spir.wikidot.com/&lt;/a&gt;&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp; 
&lt;/div&gt;As others have pointed out, you need to spell out the question you're 
&lt;br&gt;trying to answer. &amp;nbsp;Not all of us have a copy of that book, or have it 
&lt;br&gt;handy in any case.
&lt;br&gt;&lt;br&gt;If you're trying to write a function that produces equivalent results to 
&lt;br&gt;os.path.exists(), but uses exceptions to do so, you'll need to know 
&lt;br&gt;first what that function does. &amp;nbsp;It checks if a path is valid, and refers 
&lt;br&gt;to an existing file *or* *directory*. &amp;nbsp; &amp;nbsp;So if you decide that the path 
&lt;br&gt;doesn't specify a file, then you also have to check if it specifies a 
&lt;br&gt;directory.
&lt;br&gt;&lt;br&gt;It's not actually correct to just use open(), even for the file portion, 
&lt;br&gt;because open() may fail if the file is already open exclusively by 
&lt;br&gt;another program. &amp;nbsp;For a little while, the file exists, but you may not 
&lt;br&gt;open it.
&lt;br&gt;&lt;br&gt;I think if I couldn't use any of the os.path functions, I'd resort to 
&lt;br&gt;using os.walk(). &amp;nbsp;Once again, I wouldn't expect any exceptions. &amp;nbsp;I'd 
&lt;br&gt;just walk through the whole tree, and see if the desired file was on any 
&lt;br&gt;of the returned lists. &amp;nbsp;Don't forget to use case-insensitive comparison 
&lt;br&gt;on Windows.
&lt;br&gt;&lt;br&gt;The problem with a poorly reworded question is there are answers that 
&lt;br&gt;are perhaps useless to the original problem. &amp;nbsp;If I couldn't use 
&lt;br&gt;os.path.exists(), then I'd substitute os.path.isfile() and 
&lt;br&gt;os.path.isdir(), checking the one if the other fails. &amp;nbsp;But this wouldn't 
&lt;br&gt;raise any exceptions either, so it wouldn't be the learning experience 
&lt;br&gt;the author wants.
&lt;br&gt;&lt;br&gt;My suspicion is that the author just wants you to try to do an open() 
&lt;br&gt;(readonly), inside a try/except block, and to successfully handle the 
&lt;br&gt;exception. &amp;nbsp;Among other things, this requires you to find out which 
&lt;br&gt;exception type to use.
&lt;br&gt;&lt;br&gt;DaveA
&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26582054&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Equivalent-exception-of-os.path.exists%28%29-tp26576284p26582054.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26581826</id>
	<title>Re: Equivalent exception of os.path.exists()</title>
	<published>2009-11-30T12:11:32Z</published>
	<updated>2009-11-30T12:11:32Z</updated>
	<author>
		<name>Tim Golden-4</name>
	</author>
	<content type="html">biboy mendz wrote:
&lt;br&gt;&amp;gt; Lie and Tim's input are true that raw_input doesnt do anything or you 
&lt;br&gt;&amp;gt; cant catch exception error from it. And i'm wrong in placing the 
&lt;br&gt;&amp;gt; try-except clause, it should be on the fobj-open line. But im still 
&lt;br&gt;&amp;gt; looking for the exception that will be raised when i input a filename 
&lt;br&gt;&amp;gt; and that file already exists. I hope you get what i mean :-)
&lt;br&gt;&lt;br&gt;There isn't one. The string you input which is a filename is just
&lt;br&gt;a string. There's no reason for Python to check whether it is a
&lt;br&gt;valid filename, any more than to check it is a valid integer or
&lt;br&gt;a valid recipe on goodrecipes.com. You have to decide what you
&lt;br&gt;want to do with the string.
&lt;br&gt;&lt;br&gt;Then you do that (try to open it as a file; try to convert it
&lt;br&gt;to a number; try to use it as a URL to goodrecipes.com) and
&lt;br&gt;deal with exceptions appropriately.
&lt;br&gt;&lt;br&gt;TJG
&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26581826&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Equivalent-exception-of-os.path.exists%28%29-tp26576284p26581826.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26581786</id>
	<title>Re: Equivalent exception of os.path.exists()</title>
	<published>2009-11-30T12:04:52Z</published>
	<updated>2009-11-30T12:04:52Z</updated>
	<author>
		<name>bibi midi</name>
	</author>
	<content type="html">&lt;br&gt;&lt;br&gt;spir wrote:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; What is your question?
&lt;br&gt;&amp;gt; If it's about the type of exception raised when os.path.exists fails, well, sure it's hard to find:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp; print os.path.exists(&amp;quot;foo&amp;quot;.bar)
&lt;br&gt;&amp;gt; &amp;nbsp; ==&amp;gt; False
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp; 
&lt;br&gt;My question is: i'm looking for type of exception that more or less 
&lt;br&gt;equivalent to os.path.exists attribute. I know for a fact that this 
&lt;br&gt;method yields true or false. The exercise in the book says use 
&lt;br&gt;try-except in place of os.path.exists(). That sure is (to me) quite 
&lt;br&gt;difficult task. Instead of using the available function you're are 
&lt;br&gt;tasked to do the alternative.
&lt;br&gt;&lt;br&gt;Lie and Tim's input are true that raw_input doesnt do anything or you 
&lt;br&gt;cant catch exception error from it. And i'm wrong in placing the 
&lt;br&gt;try-except clause, it should be on the fobj-open line. But im still 
&lt;br&gt;looking for the exception that will be raised when i input a filename 
&lt;br&gt;and that file already exists. I hope you get what i mean :-)
&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; The output beeing a logical value, there is no failure.
&lt;br&gt;&amp;gt; A general method to get a type of exception matching a particuliar kind of error is to provoke an error of this given kind, eg:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp; f = file(&amp;quot;foo.bar&amp;quot;)
&lt;br&gt;&amp;gt; &amp;nbsp; ==&amp;gt;
&lt;br&gt;&amp;gt; Traceback (most recent call last):
&lt;br&gt;&amp;gt; &amp;nbsp; File &amp;quot;__essai__.py&amp;quot;, line 10, in &amp;lt;module&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp; &amp;nbsp; f = file(&amp;quot;foo.bar&amp;quot;)
&lt;br&gt;&amp;gt; IOError: [Errno 2] No such file or directory: 'foo.bar'
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; In this case, the type is IOError.
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; Denis
&lt;br&gt;&amp;gt; ________________________________
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; la vita e estrany
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; &lt;a href=&quot;http://spir.wikidot.com/&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://spir.wikidot.com/&lt;/a&gt;&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; _______________________________________________
&lt;br&gt;&amp;gt; Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26581786&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;&amp;gt; To unsubscribe or change subscription options:
&lt;br&gt;&amp;gt; &lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp; 
&lt;/div&gt;&lt;br&gt;-- 
&lt;br&gt;Regards,
&lt;br&gt;bibs M.
&lt;br&gt;&lt;br&gt;Host/Kernel/OS &amp;nbsp;&amp;quot;cc000002695&amp;quot; running Linux 2.6.31-5.slh.4-sidux-686 
&lt;br&gt;[sidux 2009-02 Αιθήρ - kde-full - (200907141427) ]
&lt;br&gt;www.sidux.com
&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26581786&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Equivalent-exception-of-os.path.exists%28%29-tp26576284p26581786.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26580995</id>
	<title>numerical simulation + SQLite</title>
	<published>2009-11-30T10:47:32Z</published>
	<updated>2009-11-30T10:47:32Z</updated>
	<author>
		<name>Faisal Moledina-2</name>
	</author>
	<content type="html">Hey everyone,
&lt;br&gt;&lt;br&gt;I have a general issue that I'd like to discuss. I'm using Python to
&lt;br&gt;run a numerical simulation where at each time step, I run a number of
&lt;br&gt;operations and store the results before moving to the next timestep.
&lt;br&gt;At first, I used a list to store a bunch of class instances, each of
&lt;br&gt;which contained a bunch of data calculated at each time step. This
&lt;br&gt;resulted in whopping memory usage (2.75 GB RAM, 3.75 GB VM).
&lt;br&gt;&lt;br&gt;So then I decided instead to use SQLite to store that information at
&lt;br&gt;each timestep. This seems to work well, but it gets slow over time as
&lt;br&gt;well. I've never used SQLite or Python before and I come from a
&lt;br&gt;MATLAB-based engineering background rather than a programming one. I
&lt;br&gt;was wondering if anyone had any tips for using SQLite efficiently.
&lt;br&gt;Maybe a list of dos and don'ts.
&lt;br&gt;&lt;br&gt;I understand that specific help is impossible without a reduced sample
&lt;br&gt;of code. Currently I'm looking for general guidelines and will come
&lt;br&gt;back to this list for specific hangups. Thank you.
&lt;br&gt;&lt;br&gt;Faisal
&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26580995&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/numerical-simulation-%2B-SQLite-tp26580995p26580995.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26581614</id>
	<title>Re: x is a global variable</title>
	<published>2009-11-30T10:02:08Z</published>
	<updated>2009-11-30T10:02:08Z</updated>
	<author>
		<name>spir</name>
	</author>
	<content type="html">Hello Eike!
&lt;br&gt;&lt;br&gt;Eike Welk &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26581614&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;eike.welk@...&lt;/a&gt;&amp;gt; dixit:
&lt;br&gt;&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; Hello Spir!
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; On Monday 30 November 2009, spir wrote:
&lt;br&gt;&amp;gt; &amp;gt; which seems to indicate python really embeds &amp;quot;symbolic references&amp;quot;
&lt;br&gt;&amp;gt; &amp;gt; (*) to outer *variables*, when creating a closure for g0. Not
&lt;br&gt;&amp;gt; &amp;gt; &amp;quot;pointer references&amp;quot; (**), otherwise the replacement of x would not
&lt;br&gt;&amp;gt; &amp;gt; be seen by the closure --like in the case of default-parameter.
&lt;br&gt;&amp;gt; &amp;gt; Actually, I find this _Bad_. Obviously, the func's behaviour and
&lt;br&gt;&amp;gt; &amp;gt; result depend on arbitrary external values (referentially opaque).
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; If I understand you right, you are proposing that the inner function 
&lt;br&gt;&amp;gt; g0 should get a separate copy of the global namespace. The copy should 
&lt;br&gt;&amp;gt; be done when the function is defined. 
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; When this would be implemented, inner functions (g0) would be treated 
&lt;br&gt;&amp;gt; very differently from outer functions (f), with respect to global 
&lt;br&gt;&amp;gt; variables. I would not like this different treatment. When a global 
&lt;br&gt;&amp;gt; variable is changed all functions should see the change. 
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; I think currently there are three 'containers' where functions can 
&lt;br&gt;&amp;gt; find variables in Python:
&lt;br&gt;&amp;gt; - local variables 
&lt;br&gt;&amp;gt; - the closure, which is empty in outer functions (f.func_closure)
&lt;br&gt;&amp;gt; - global variables, this is probably f.__module__.__dict__.
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; The exact semantics of closures seem to be a bit tricky. Below is some 
&lt;br&gt;&amp;gt; behavior I find surprising. I had expected that a local variable 'i' 
&lt;br&gt;&amp;gt; is created in function 'inc'. Instead the function fails with an 
&lt;br&gt;&amp;gt; error. (Producing the error is IMHO surprising, but sane behavior.):
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; In [25]:def clos():
&lt;br&gt;&amp;gt; &amp;nbsp; i = 0
&lt;br&gt;&amp;gt; &amp;nbsp; def inc():
&lt;br&gt;&amp;gt; &amp;nbsp; &amp;nbsp; i = i + 1
&lt;br&gt;&amp;gt; &amp;nbsp; &amp;nbsp; print i
&lt;br&gt;&amp;gt; &amp;nbsp; def pri():
&lt;br&gt;&amp;gt; &amp;nbsp; &amp;nbsp; print i
&lt;br&gt;&amp;gt; &amp;nbsp; return inc, pri
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; In [33]:inc, pri = clos()
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; In [34]:pri()
&lt;br&gt;&amp;gt; 0
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; In [35]:inc()
&lt;br&gt;&amp;gt; -------------------------------------------------------------------
&lt;br&gt;&amp;gt; UnboundLocalError &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Traceback (most recent call last)
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; /home/eike/&amp;lt;ipython console&amp;gt; in &amp;lt;module&amp;gt;()
&lt;br&gt;&amp;gt; /home/eike/&amp;lt;ipython console&amp;gt; in inc()
&lt;br&gt;&amp;gt; UnboundLocalError: local variable 'i' referenced before assignment
&lt;/div&gt;&lt;br&gt;Well, this is certainly not specific to closures.
&lt;br&gt;&lt;br&gt;i = 0
&lt;br&gt;def f():
&lt;br&gt;&amp;nbsp; i = i+1
&lt;br&gt;&amp;nbsp; print i
&lt;br&gt;f()
&lt;br&gt;==&amp;gt; UnboundLocalError
&lt;br&gt;&lt;br&gt;Imo, in this case, &amp;quot;i = i+1&amp;quot; is a kind of &amp;quot;paradoxal injonction&amp;quot; (lol! not sure of the exact idiom in english). You tell python both to create a local i (thus ignore any other scope to lookup for variables called 'i') and to use global i to define the local one.
&lt;br&gt;If I were the victim of such a &amp;quot;paradoxal injonction&amp;quot; I would reply with a naughty word!
&lt;br&gt;&lt;br&gt;&amp;gt; ---
&lt;br&gt;&amp;gt; Eike.
&lt;br&gt;&amp;gt; 
&lt;br&gt;&lt;br&gt;Denis
&lt;br&gt;________________________________
&lt;br&gt;&lt;br&gt;la vita e estrany
&lt;br&gt;&lt;br&gt;&lt;a href=&quot;http://spir.wikidot.com/&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://spir.wikidot.com/&lt;/a&gt;&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26581614&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/python-closures-tp26572340p26581614.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26579517</id>
	<title>Re: python closures</title>
	<published>2009-11-30T09:38:12Z</published>
	<updated>2009-11-30T09:38:12Z</updated>
	<author>
		<name>Alan Gauld</name>
	</author>
	<content type="html">&lt;br&gt;&amp;quot;spir&amp;quot; &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26579517&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;denis.spir@...&lt;/a&gt;&amp;gt; wrote
&lt;br&gt;&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; x = 1
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; def f():
&lt;br&gt;&amp;gt; &amp;nbsp;n = 1
&lt;br&gt;&amp;gt; &amp;nbsp;def g0(a):
&lt;br&gt;&amp;gt; &amp;nbsp; &amp;nbsp;print (x + n + a)
&lt;br&gt;&amp;gt; &amp;nbsp;return g0
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; I'm surprised the snippet below works as expected (py 2.6) without any 
&lt;br&gt;&amp;gt; trick:
&lt;/div&gt;&lt;br&gt;I'm not sure how else it could work.
&lt;br&gt;x is a global name so the function must reference it.
&lt;br&gt;n is a local name so it musdt evaluate it (it will disappear otherwise)
&lt;br&gt;a is a parameter delivered at run time.
&lt;br&gt;&lt;br&gt;&amp;gt; This means a (real) closure is built for g0, or what?
&lt;br&gt;&amp;gt; Thought I would need instead to use the old trick of pseudo 
&lt;br&gt;&amp;gt; default-parameters:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; def f():
&lt;br&gt;&amp;gt; &amp;nbsp;n = 1
&lt;br&gt;&amp;gt; &amp;nbsp;def g0(a, n=n, x=x):
&lt;br&gt;&amp;gt; &amp;nbsp; &amp;nbsp;print (x + n + a)
&lt;br&gt;&amp;gt; &amp;nbsp;return g0
&lt;br&gt;&lt;br&gt;I did wonder if you would need n=n but I didn't think you would need x=x.
&lt;br&gt;&lt;br&gt;Its an interesting example and I confess I don't fully understand how 
&lt;br&gt;Python's
&lt;br&gt;naming/reference rules are working here.
&lt;br&gt;&lt;br&gt;&amp;gt; to let the inner func g0 &amp;quot;remember&amp;quot; outer values.
&lt;br&gt;&amp;gt; Why is this idiom used, then? Has something changed, or do I miss
&lt;br&gt;&amp;gt; a relevant point?
&lt;br&gt;&lt;br&gt;I thought you might need to do it if n had been a parameter of f()... but
&lt;br&gt;having tried it no, it works as above.
&lt;br&gt;&lt;br&gt;I look forward to the explanation.
&lt;br&gt;&lt;br&gt;Alan G. 
&lt;br&gt;&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26579517&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/python-closures-tp26572340p26579517.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26579486</id>
	<title>Re: python closures</title>
	<published>2009-11-30T09:36:52Z</published>
	<updated>2009-11-30T09:36:52Z</updated>
	<author>
		<name>Eike Welk-2</name>
	</author>
	<content type="html">On Monday 30 November 2009, Hugo Arts wrote:
&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; Consider this python session:
&lt;br&gt;&amp;gt; &amp;nbsp;&amp;gt;&amp;gt;&amp;gt; x = 0
&lt;br&gt;&amp;gt; &amp;nbsp;&amp;gt;&amp;gt;&amp;gt; def f():
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; 		x = x + 1
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp;&amp;gt;&amp;gt;&amp;gt; f()
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; Traceback (most recent call last):
&lt;br&gt;&amp;gt; &amp;nbsp; File &amp;quot;&amp;lt;pyshell#27&amp;gt;&amp;quot;, line 1, in &amp;lt;module&amp;gt;
&lt;br&gt;&amp;gt; &amp;nbsp; &amp;nbsp; f()
&lt;br&gt;&amp;gt; &amp;nbsp; File &amp;quot;&amp;lt;pyshell#26&amp;gt;&amp;quot;, line 2, in f
&lt;br&gt;&amp;gt; &amp;nbsp; &amp;nbsp; x = x + 1
&lt;br&gt;&amp;gt; UnboundLocalError: local variable 'x' referenced before assignment
&lt;/div&gt;Ah... what a pity I didn't try this. I used similar code in my 
&lt;br&gt;response to Spir and thought it would be somehow connected to 
&lt;br&gt;closures. Sending nonsense statements to the list again... 
&lt;br&gt;&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; The python docs offers some insight:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; The execution of a function introduces a new symbol table used for
&lt;br&gt;&amp;gt; the local variables of the function. More precisely, all variable
&lt;br&gt;&amp;gt; assignments in a function store the value in the local symbol
&lt;br&gt;&amp;gt; table; whereas variable references first look in the local symbol
&lt;br&gt;&amp;gt; table, then in the local symbol tables of enclosing functions, then
&lt;br&gt;&amp;gt; in the global symbol table, and finally in the table of built-in
&lt;br&gt;&amp;gt; names. Thus, global variables cannot be directly assigned a value
&lt;br&gt;&amp;gt; within a function (unless named in a global statement), although
&lt;br&gt;&amp;gt; they may be referenced.
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; ( from
&lt;br&gt;&amp;gt; &lt;a href=&quot;http://docs.python.org/tutorial/controlflow.html#defining-functions&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://docs.python.org/tutorial/controlflow.html#defining-functions&lt;/a&gt;&lt;br&gt;&amp;gt; )
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; In short, the problem is that writing &amp;quot;x =&amp;quot; will create a new
&lt;br&gt;&amp;gt; (unbound) local name for x, hiding the global one. The following
&lt;br&gt;&amp;gt; reference to x will find the local unbound variable and start
&lt;br&gt;&amp;gt; complaining.
&lt;/div&gt;I find that behavior quite counterintuitive. I expect:
&lt;br&gt;1. 'x' is looked up, global variable 'x' is found; 
&lt;br&gt;2. the addition is performed; 
&lt;br&gt;3. a local name 'x' is created and bound to the result of the 
&lt;br&gt;addition.
&lt;br&gt;&lt;br&gt;Producing the error is not insane. Because referencing a global 
&lt;br&gt;variable and shadowing it in the same statement is bad style. 
&lt;br&gt;&lt;br&gt;&lt;br&gt;Eike.
&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26579486&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/python-closures-tp26572340p26579486.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26578543</id>
	<title>Re: x is a global variable</title>
	<published>2009-11-30T09:09:38Z</published>
	<updated>2009-11-30T09:09:38Z</updated>
	<author>
		<name>Eike Welk-2</name>
	</author>
	<content type="html">Hello Spir!
&lt;br&gt;&lt;br&gt;On Monday 30 November 2009, spir wrote:
&lt;br&gt;&amp;gt; which seems to indicate python really embeds &amp;quot;symbolic references&amp;quot;
&lt;br&gt;&amp;gt; (*) to outer *variables*, when creating a closure for g0. Not
&lt;br&gt;&amp;gt; &amp;quot;pointer references&amp;quot; (**), otherwise the replacement of x would not
&lt;br&gt;&amp;gt; be seen by the closure --like in the case of default-parameter.
&lt;br&gt;&amp;gt; Actually, I find this _Bad_. Obviously, the func's behaviour and
&lt;br&gt;&amp;gt; result depend on arbitrary external values (referentially opaque).
&lt;br&gt;&lt;br&gt;If I understand you right, you are proposing that the inner function 
&lt;br&gt;g0 should get a separate copy of the global namespace. The copy should 
&lt;br&gt;be done when the function is defined. 
&lt;br&gt;&lt;br&gt;When this would be implemented, inner functions (g0) would be treated 
&lt;br&gt;very differently from outer functions (f), with respect to global 
&lt;br&gt;variables. I would not like this different treatment. When a global 
&lt;br&gt;variable is changed all functions should see the change. 
&lt;br&gt;&lt;br&gt;I think currently there are three 'containers' where functions can 
&lt;br&gt;find variables in Python:
&lt;br&gt;- local variables 
&lt;br&gt;- the closure, which is empty in outer functions (f.func_closure)
&lt;br&gt;- global variables, this is probably f.__module__.__dict__.
&lt;br&gt;&lt;br&gt;The exact semantics of closures seem to be a bit tricky. Below is some 
&lt;br&gt;behavior I find surprising. I had expected that a local variable 'i' 
&lt;br&gt;is created in function 'inc'. Instead the function fails with an 
&lt;br&gt;error. (Producing the error is IMHO surprising, but sane behavior.):
&lt;br&gt;&lt;br&gt;In [25]:def clos():
&lt;br&gt;&amp;nbsp; i = 0
&lt;br&gt;&amp;nbsp; def inc():
&lt;br&gt;&amp;nbsp; &amp;nbsp; i = i + 1
&lt;br&gt;&amp;nbsp; &amp;nbsp; print i
&lt;br&gt;&amp;nbsp; def pri():
&lt;br&gt;&amp;nbsp; &amp;nbsp; print i
&lt;br&gt;&amp;nbsp; return inc, pri
&lt;br&gt;&lt;br&gt;In [33]:inc, pri = clos()
&lt;br&gt;&lt;br&gt;In [34]:pri()
&lt;br&gt;0
&lt;br&gt;&lt;br&gt;In [35]:inc()
&lt;br&gt;-------------------------------------------------------------------
&lt;br&gt;UnboundLocalError &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Traceback (most recent call last)
&lt;br&gt;&lt;br&gt;/home/eike/&amp;lt;ipython console&amp;gt; in &amp;lt;module&amp;gt;()
&lt;br&gt;/home/eike/&amp;lt;ipython console&amp;gt; in inc()
&lt;br&gt;UnboundLocalError: local variable 'i' referenced before assignment
&lt;br&gt;&lt;br&gt;&lt;br&gt;---
&lt;br&gt;Eike.
&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26578543&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/python-closures-tp26572340p26578543.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26578792</id>
	<title>Re: Equivalent exception of os.path.exists()</title>
	<published>2009-11-30T09:08:18Z</published>
	<updated>2009-11-30T09:08:18Z</updated>
	<author>
		<name>spir</name>
	</author>
	<content type="html">biboy mendz &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26578792&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;bibsmendez@...&lt;/a&gt;&amp;gt; dixit:
&lt;br&gt;&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; &lt;a href=&quot;http://pastebin.ca/1693849&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://pastebin.ca/1693849&lt;/a&gt;&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; This is end-of-chapter3 exercise of the book Core Python Programming.
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; I'm reading/searching in the book and other materials but is 
&lt;br&gt;&amp;gt; unsuccessful. There are at least 50 exceptions listed but I can't find 
&lt;br&gt;&amp;gt; anything close.
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; I commented out my modified script to just what *should* be applicable. 
&lt;br&gt;&amp;gt; Can you please point me to the right direction? TIA.
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; 
&lt;/div&gt;&lt;br&gt;What is your question?
&lt;br&gt;If it's about the type of exception raised when os.path.exists fails, well, sure it's hard to find:
&lt;br&gt;&lt;br&gt;&amp;nbsp; print os.path.exists(&amp;quot;foo&amp;quot;.bar)
&lt;br&gt;&amp;nbsp; ==&amp;gt; False
&lt;br&gt;&lt;br&gt;The output beeing a logical value, there is no failure.
&lt;br&gt;A general method to get a type of exception matching a particuliar kind of error is to provoke an error of this given kind, eg:
&lt;br&gt;&lt;br&gt;&amp;nbsp; f = file(&amp;quot;foo.bar&amp;quot;)
&lt;br&gt;&amp;nbsp; ==&amp;gt;
&lt;br&gt;Traceback (most recent call last):
&lt;br&gt;&amp;nbsp; File &amp;quot;__essai__.py&amp;quot;, line 10, in &amp;lt;module&amp;gt;
&lt;br&gt;&amp;nbsp; &amp;nbsp; f = file(&amp;quot;foo.bar&amp;quot;)
&lt;br&gt;IOError: [Errno 2] No such file or directory: 'foo.bar'
&lt;br&gt;&lt;br&gt;In this case, the type is IOError.
&lt;br&gt;&lt;br&gt;Denis
&lt;br&gt;________________________________
&lt;br&gt;&lt;br&gt;la vita e estrany
&lt;br&gt;&lt;br&gt;&lt;a href=&quot;http://spir.wikidot.com/&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://spir.wikidot.com/&lt;/a&gt;&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26578792&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Equivalent-exception-of-os.path.exists%28%29-tp26576284p26578792.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26578213</id>
	<title>Re: python closures</title>
	<published>2009-11-30T08:38:57Z</published>
	<updated>2009-11-30T08:38:57Z</updated>
	<author>
		<name>Hugo Arts</name>
	</author>
	<content type="html">On Mon, Nov 30, 2009 at 4:16 PM, Kent Johnson &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26578213&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;kent37@...&lt;/a&gt;&amp;gt; wrote:
&lt;br&gt;&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; That has not been needed since 2.1 though it is still useful when
&lt;br&gt;&amp;gt; closures are created in a loop (because closures are kind of late
&lt;br&gt;&amp;gt; bound - I'm not sure the exact technical explanation):
&lt;br&gt;&amp;gt; In [13]: def f():
&lt;br&gt;&amp;gt;   ....:     l = []
&lt;br&gt;&amp;gt;   ....:     for i in range(3):
&lt;br&gt;&amp;gt;   ....:         def g():
&lt;br&gt;&amp;gt;   ....:             print i
&lt;br&gt;&amp;gt;   ....:         l.append(g)
&lt;br&gt;&amp;gt;   ....:     return l
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; In [14]: for g in f(): g()
&lt;br&gt;&amp;gt;   ....:
&lt;br&gt;&amp;gt; 2
&lt;br&gt;&amp;gt; 2
&lt;br&gt;&amp;gt; 2
&lt;br&gt;&amp;gt;
&lt;/div&gt;&lt;br&gt;This doesn't really have anything to do with closures specifically.
&lt;br&gt;Variable lookup is done at runtime, not definition time. So when these
&lt;br&gt;lookups for i are performed the value of i is indeed 2.
&lt;br&gt;&lt;br&gt;This wouldn't happen if closures used pointer references (as spir
&lt;br&gt;called them). Then again, even with pointer references modifications
&lt;br&gt;to mutable variables are still visible inside a closure.
&lt;br&gt;&lt;br&gt;Hugo
&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26578213&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/python-closures-tp26572340p26578213.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26578308</id>
	<title>Re: Python best practices</title>
	<published>2009-11-30T08:34:34Z</published>
	<updated>2009-11-30T08:34:34Z</updated>
	<author>
		<name>spir</name>
	</author>
	<content type="html">ALAN GAULD &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26578308&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;alan.gauld@...&lt;/a&gt;&amp;gt; dixit:
&lt;br&gt;&lt;br&gt;&amp;gt; But as I said earlier the concept of multiple return 
&lt;br&gt;&amp;gt; values in Python is similar to returning a struct in C.
&lt;br&gt;&amp;gt; The key thing is that the function has a single purpose 
&lt;br&gt;&amp;gt; so if returning multiple values or a struct the values 
&lt;br&gt;&amp;gt; within that group should be related to the single purpose
&lt;br&gt;&lt;br&gt;It's not enough I guess that returned values are &amp;quot;related to the [func's] single purpose&amp;quot;.
&lt;br&gt;&lt;br&gt;The cases when I do not mind returning multiple values are precisely the ones when I could build the equivalent of a struct / record / (named) tuple / object. One that makes sense. In other words, when the set of values together describe *one single thing*. In some cases it's not worth building a &amp;quot;struct&amp;quot; if the caller will then just unpack it for further process.
&lt;br&gt;Eg it can makes sense to 
&lt;br&gt;&amp;nbsp; &amp;nbsp;return (position, color)
&lt;br&gt;instead of:
&lt;br&gt;&amp;nbsp; &amp;nbsp;return Point(position, color)
&lt;br&gt;(provided there is a Point type defined)
&lt;br&gt;&lt;br&gt;But there are cases when I feel uneasy returning multiple values even if each one is clearly related to the (single) purpose the the func. The typical case for me is a match func that needs together:
&lt;br&gt;1. tell about match outcome (success/failure)
&lt;br&gt;2. move ahead the pointer position in source
&lt;br&gt;3. return node (match result)
&lt;br&gt;There are indeed numerous ways to do this, even to avoid returning more than one thing (have tried several). My latest method is to (1) raise exception in case of failure (2) have a source object that itself holds its pointer (3) then the node only is returned.
&lt;br&gt;But these means are tricks (esp. unsing exception, which is _always_ a trick) to hide multiple actions and returns. 
&lt;br&gt;Also note that a stuct composed of outcome+position+node does not make any sense (try to name it! ;-).
&lt;br&gt;I guess the only proper solution is to build a real state machine (on which parse data is stored), but this makes parse tree construction extremely complicated in the general case, compared to having nodes directly returned by match funcs.
&lt;br&gt;&lt;br&gt;Denis
&lt;br&gt;________________________________
&lt;br&gt;&lt;br&gt;la vita e estrany
&lt;br&gt;&lt;br&gt;&lt;a href=&quot;http://spir.wikidot.com/&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://spir.wikidot.com/&lt;/a&gt;&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26578308&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Python-best-practices-tp26539015p26578308.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26577927</id>
	<title>Re: python closures</title>
	<published>2009-11-30T08:19:20Z</published>
	<updated>2009-11-30T08:19:20Z</updated>
	<author>
		<name>Hugo Arts</name>
	</author>
	<content type="html">On Mon, Nov 30, 2009 at 11:24 AM, spir &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26577927&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;denis.spir@...&lt;/a&gt;&amp;gt; wrote:
&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; Hello,
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; Below startup definitions:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; x = 1
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; def f():
&lt;br&gt;&amp;gt;  n = 1
&lt;br&gt;&amp;gt;  def g0(a):
&lt;br&gt;&amp;gt;    print (x + n + a)
&lt;br&gt;&amp;gt;  return g0
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; I'm surprised the snippet below works as expected (py 2.6) without any trick:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; g = f()
&lt;br&gt;&amp;gt; g(1)    # --&amp;gt; 3
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; This means a (real) closure is built for g0, or what?
&lt;br&gt;&amp;gt; Thought I would need instead to use the old trick of pseudo default-parameters:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; def f():
&lt;br&gt;&amp;gt;  n = 1
&lt;br&gt;&amp;gt;  def g0(a, n=n, x=x):
&lt;br&gt;&amp;gt;    print (x + n + a)
&lt;br&gt;&amp;gt;  return g0
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; to let the inner func g0 &amp;quot;remember&amp;quot; outer values. Why is this idiom used, then? Has something changed, or do I miss a relevant point?
&lt;/div&gt;&lt;br&gt;A real closure is indeed created, but you are missing something.
&lt;br&gt;Consider this python session:
&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;gt;&amp;gt;&amp;gt; x = 0
&lt;br&gt;&amp;nbsp;&amp;gt;&amp;gt;&amp;gt; def f():
&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; x = x + 1
&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;gt;&amp;gt;&amp;gt; f()
&lt;br&gt;&lt;br&gt;Traceback (most recent call last):
&lt;br&gt;&amp;nbsp; File &amp;quot;&amp;lt;pyshell#27&amp;gt;&amp;quot;, line 1, in &amp;lt;module&amp;gt;
&lt;br&gt;&amp;nbsp; &amp;nbsp; f()
&lt;br&gt;&amp;nbsp; File &amp;quot;&amp;lt;pyshell#26&amp;gt;&amp;quot;, line 2, in f
&lt;br&gt;&amp;nbsp; &amp;nbsp; x = x + 1
&lt;br&gt;UnboundLocalError: local variable 'x' referenced before assignment
&lt;br&gt;&lt;br&gt;The python docs offers some insight:
&lt;br&gt;&lt;br&gt;The execution of a function introduces a new symbol table used for the
&lt;br&gt;local variables of the function. More precisely, all variable
&lt;br&gt;assignments in a function store the value in the local symbol table;
&lt;br&gt;whereas variable references first look in the local symbol table, then
&lt;br&gt;in the local symbol tables of enclosing functions, then in the global
&lt;br&gt;symbol table, and finally in the table of built-in names. Thus, global
&lt;br&gt;variables cannot be directly assigned a value within a function
&lt;br&gt;(unless named in a global statement), although they may be referenced.
&lt;br&gt;&lt;br&gt;( from &lt;a href=&quot;http://docs.python.org/tutorial/controlflow.html#defining-functions&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://docs.python.org/tutorial/controlflow.html#defining-functions&lt;/a&gt;&amp;nbsp;)
&lt;br&gt;&lt;br&gt;In short, the problem is that writing &amp;quot;x =&amp;quot; will create a new
&lt;br&gt;(unbound) local name for x, hiding the global one. The following
&lt;br&gt;reference to x will find the local unbound variable and start
&lt;br&gt;complaining.
&lt;br&gt;&lt;br&gt;If you wanted to create a local variable, your idiom is the fix you
&lt;br&gt;want. If you want to modify the global one, use the 'global x'
&lt;br&gt;statement to tell the interpreter that explicitly.
&lt;br&gt;&lt;br&gt;&amp;gt; The bit below also works:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; x = 2
&lt;br&gt;&amp;gt; ...
&lt;br&gt;&amp;gt; g(1)    # --&amp;gt; 4
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; which seems to indicate python really embeds &amp;quot;symbolic references&amp;quot; (*) to outer *variables*, when creating a closure for g0. Not &amp;quot;pointer references&amp;quot; (**), otherwise the replacement of x would not be seen by the closure --like in the case of default-parameter.
&lt;br&gt;&amp;gt; Actually, I find this _Bad_. Obviously, the func's behaviour and result depend on arbitrary external values (referentially opaque). What do you think?
&lt;br&gt;&lt;br&gt;I don't quite understand the point you're trying to make. The code
&lt;br&gt;you're showing above seems like what is 'proper' behaviour to me. Can
&lt;br&gt;you show an example demonstrating why it is bad?
&lt;br&gt;&lt;br&gt;Hugo
&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26577927&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/python-closures-tp26572340p26577927.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26577479</id>
	<title>Re: Equivalent exception of os.path.exists()</title>
	<published>2009-11-30T07:27:48Z</published>
	<updated>2009-11-30T07:27:48Z</updated>
	<author>
		<name>Tim Golden-4</name>
	</author>
	<content type="html">biboy mendz wrote:
&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; &lt;a href=&quot;http://pastebin.ca/1693849&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://pastebin.ca/1693849&lt;/a&gt;&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; This is end-of-chapter3 exercise of the book Core Python Programming.
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; I'm reading/searching in the book and other materials but is 
&lt;br&gt;&amp;gt; unsuccessful. There are at least 50 exceptions listed but I can't find 
&lt;br&gt;&amp;gt; anything close.
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; I commented out my modified script to just what *should* be applicable. 
&lt;br&gt;&amp;gt; Can you please point me to the right direction? TIA.
&lt;/div&gt;&lt;br&gt;&lt;br&gt;Ummm.. You're not actually trying to open the file.
&lt;br&gt;raw_input just asks the user for a string. But it's
&lt;br&gt;just a string. You could be going to use it for
&lt;br&gt;anything. Try something like:
&lt;br&gt;&lt;br&gt;try:
&lt;br&gt;&amp;nbsp; open (fname).close ()
&lt;br&gt;except IOError, e:
&lt;br&gt;&amp;nbsp; print &amp;quot;blah&amp;quot;
&lt;br&gt;&lt;br&gt;&lt;br&gt;TJG
&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26577479&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Equivalent-exception-of-os.path.exists%28%29-tp26576284p26577479.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26577541</id>
	<title>Re: Equivalent exception of os.path.exists()</title>
	<published>2009-11-30T07:24:33Z</published>
	<updated>2009-11-30T07:24:33Z</updated>
	<author>
		<name>Lie Ryan</name>
	</author>
	<content type="html">On 12/1/2009 12:00 AM, biboy mendz wrote:
&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; &lt;a href=&quot;http://pastebin.ca/1693849&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://pastebin.ca/1693849&lt;/a&gt;&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; This is end-of-chapter3 exercise of the book Core Python Programming.
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; I'm reading/searching in the book and other materials but is
&lt;br&gt;&amp;gt; unsuccessful. There are at least 50 exceptions listed but I can't find
&lt;br&gt;&amp;gt; anything close.
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; I commented out my modified script to just what *should* be applicable.
&lt;br&gt;&amp;gt; Can you please point me to the right direction? TIA.
&lt;/div&gt;&lt;br&gt;You're trying to take exception from raw_input(); raw_input() merely 
&lt;br&gt;receive input from the stdin. raw_input() doesn't have anything to do 
&lt;br&gt;with the file. You should expect the exception to come from the open() 
&lt;br&gt;function instead.
&lt;br&gt;&lt;br&gt;However, on a little bit of note; open('...', 'w') will (almost) always 
&lt;br&gt;work since if the file doesn't exists, python will simply create it. The 
&lt;br&gt;only time it wouldn't work is if you've have file edit permission but 
&lt;br&gt;not create new file permission.
&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26577541&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Equivalent-exception-of-os.path.exists%28%29-tp26576284p26577541.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26577389</id>
	<title>Re: python closures</title>
	<published>2009-11-30T07:17:26Z</published>
	<updated>2009-11-30T07:17:26Z</updated>
	<author>
		<name>Stefan Behnel-3</name>
	</author>
	<content type="html">spir, 30.11.2009 11:24:
&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; Below startup definitions:
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; x = 1
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; def f():
&lt;br&gt;&amp;gt; &amp;nbsp; n = 1
&lt;br&gt;&amp;gt; &amp;nbsp; def g0(a):
&lt;br&gt;&amp;gt; &amp;nbsp; &amp;nbsp; print (x + n + a) 
&lt;br&gt;&amp;gt; &amp;nbsp; return g0
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; I'm surprised the snippet below works as expected (py 2.6) without any trick:
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; g = f()
&lt;br&gt;&amp;gt; g(1)	# --&amp;gt; 3
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; This means a (real) closure is built for g0, or what?
&lt;/div&gt;&lt;br&gt;Yes.
&lt;br&gt;&lt;br&gt;&lt;br&gt;&amp;gt; Thought I would need instead to use the old trick of pseudo default-parameters:
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; def f():
&lt;br&gt;&amp;gt; &amp;nbsp; n = 1
&lt;br&gt;&amp;gt; &amp;nbsp; def g0(a, n=n, x=x):
&lt;br&gt;&amp;gt; &amp;nbsp; &amp;nbsp; print (x + n + a) 
&lt;br&gt;&amp;gt; &amp;nbsp; return g0
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; to let the inner func g0 &amp;quot;remember&amp;quot; outer values. Why is this idiom used, then? Has something changed, or do I miss a relevant point?
&lt;br&gt;&lt;br&gt;Different use case. The above uses default arguments for n and x that can
&lt;br&gt;be overridden by callers, but that have a value if callers do not pass
&lt;br&gt;them. Values in closures can only be modified by the owner(s) of the names
&lt;br&gt;that participate in the closure (i.e. the function f in this case).
&lt;br&gt;&lt;br&gt;&lt;br&gt;&amp;gt; The bit below also works:
&lt;br&gt;&amp;gt; 
&lt;br&gt;&amp;gt; x = 2
&lt;br&gt;&amp;gt; ...
&lt;br&gt;&amp;gt; g(1)	# --&amp;gt; 4
&lt;br&gt;&lt;br&gt;x is not in the closure, it's a global name.
&lt;br&gt;&lt;br&gt;&lt;br&gt;&amp;gt; the func's behaviour and result depend on arbitrary external values (referentially opaque). What do you think?
&lt;br&gt;&lt;br&gt;It's a matter of how you use it. Closures make a lot of sense for many
&lt;br&gt;cases, but there are certainly also cases where using them feels like a
&lt;br&gt;goto - just like global variables.
&lt;br&gt;&lt;br&gt;Stefan
&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26577389&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/python-closures-tp26572340p26577389.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26577345</id>
	<title>Re: python closures</title>
	<published>2009-11-30T07:16:05Z</published>
	<updated>2009-11-30T07:16:05Z</updated>
	<author>
		<name>Kent Johnson</name>
	</author>
	<content type="html">On Mon, Nov 30, 2009 at 5:24 AM, spir &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26577345&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;denis.spir@...&lt;/a&gt;&amp;gt; wrote:
&lt;div class='shrinkable-quote'&gt;&lt;br&gt;&amp;gt; Hello,
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; Below startup definitions:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; x = 1
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; def f():
&lt;br&gt;&amp;gt;  n = 1
&lt;br&gt;&amp;gt;  def g0(a):
&lt;br&gt;&amp;gt;    print (x + n + a)
&lt;br&gt;&amp;gt;  return g0
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; I'm surprised the snippet below works as expected (py 2.6) without any trick:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; g = f()
&lt;br&gt;&amp;gt; g(1)    # --&amp;gt; 3
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; This means a (real) closure is built for g0, or what?
&lt;/div&gt;&lt;br&gt;Yes, Python has had real (read-only) closures since 2.1 when nested
&lt;br&gt;scopes where introduced:
&lt;br&gt;&lt;a href=&quot;http://docs.python.org/dev/whatsnew/2.1.html#pep-227-nested-scopes&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://docs.python.org/dev/whatsnew/2.1.html#pep-227-nested-scopes&lt;/a&gt;&lt;br&gt;&lt;br&gt;Python 3 introduces the 'nonlocal' keyword which allows assignment to
&lt;br&gt;names in enclosing scopes, presumably ending at last the debate about
&lt;br&gt;whether Python has 'real' closures:
&lt;br&gt;&lt;a href=&quot;http://www.python.org/dev/peps/pep-3104/&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://www.python.org/dev/peps/pep-3104/&lt;/a&gt;&lt;br&gt;&lt;br&gt;&amp;gt; Thought I would need instead to use the old trick of pseudo default-parameters:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; def f():
&lt;br&gt;&amp;gt;  n = 1
&lt;br&gt;&amp;gt;  def g0(a, n=n, x=x):
&lt;br&gt;&amp;gt;    print (x + n + a)
&lt;br&gt;&amp;gt;  return g0
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; to let the inner func g0 &amp;quot;remember&amp;quot; outer values. Why is this idiom used, then? Has something changed, or do I miss a relevant point?
&lt;br&gt;&lt;br&gt;That has not been needed since 2.1 though it is still useful when
&lt;br&gt;closures are created in a loop (because closures are kind of late
&lt;br&gt;bound - I'm not sure the exact technical explanation):
&lt;br&gt;In [13]: def f():
&lt;br&gt;&amp;nbsp; &amp;nbsp;....: &amp;nbsp; &amp;nbsp; l = []
&lt;br&gt;&amp;nbsp; &amp;nbsp;....: &amp;nbsp; &amp;nbsp; for i in range(3):
&lt;br&gt;&amp;nbsp; &amp;nbsp;....: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; def g():
&lt;br&gt;&amp;nbsp; &amp;nbsp;....: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print i
&lt;br&gt;&amp;nbsp; &amp;nbsp;....: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; l.append(g)
&lt;br&gt;&amp;nbsp; &amp;nbsp;....: &amp;nbsp; &amp;nbsp; return l
&lt;br&gt;&lt;br&gt;In [14]: for g in f(): g()
&lt;br&gt;&amp;nbsp; &amp;nbsp;....:
&lt;br&gt;2
&lt;br&gt;2
&lt;br&gt;2
&lt;br&gt;&lt;br&gt;&lt;br&gt;But with the default argument it captures the value of i each time
&lt;br&gt;through the loop:
&lt;br&gt;In [15]: def f():
&lt;br&gt;&amp;nbsp; &amp;nbsp;....: &amp;nbsp; &amp;nbsp; l = []
&lt;br&gt;&amp;nbsp; &amp;nbsp;....: &amp;nbsp; &amp;nbsp; for i in range(3):
&lt;br&gt;&amp;nbsp; &amp;nbsp;....: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; def g(i=i):
&lt;br&gt;&amp;nbsp; &amp;nbsp;....: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print i
&lt;br&gt;&amp;nbsp; &amp;nbsp;....: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; l.append(g)
&lt;br&gt;&amp;nbsp; &amp;nbsp;....: &amp;nbsp; &amp;nbsp; return l
&lt;br&gt;&lt;br&gt;In [16]: for g in f(): g()
&lt;br&gt;&amp;nbsp; &amp;nbsp;....:
&lt;br&gt;0
&lt;br&gt;1
&lt;br&gt;2
&lt;br&gt;&lt;br&gt;&amp;gt; The bit below also works:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; x = 2
&lt;br&gt;&amp;gt; ...
&lt;br&gt;&amp;gt; g(1)    # --&amp;gt; 4
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; which seems to indicate python really embeds &amp;quot;symbolic references&amp;quot; (*) to outer *variables*, when creating a closure for g0. Not &amp;quot;pointer references&amp;quot; (**), otherwise the replacement of x would not be seen by the closure --like in the case of default-parameter.
&lt;br&gt;&lt;br&gt;In your first definition of f(), x is global and not included in the
&lt;br&gt;closure. This is the same behaviour you would have in older versions.
&lt;br&gt;In your second definition of f(), x is bound to a default argument and
&lt;br&gt;changing the global x doesn't change the result of g().
&lt;br&gt;&lt;br&gt;&amp;gt; Actually, I find this _Bad_. Obviously, the func's behaviour and result depend on arbitrary external values (referentially opaque). What do you think?
&lt;br&gt;&lt;br&gt;That is always the case when a function accesses globals. Globals are
&lt;br&gt;_Bad_, yes.
&lt;br&gt;&lt;br&gt;Kent
&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26577345&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/python-closures-tp26572340p26577345.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26576284</id>
	<title>Equivalent exception of os.path.exists()</title>
	<published>2009-11-30T05:00:03Z</published>
	<updated>2009-11-30T05:00:03Z</updated>
	<author>
		<name>bibi midi</name>
	</author>
	<content type="html">&lt;a href=&quot;http://pastebin.ca/1693849&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://pastebin.ca/1693849&lt;/a&gt;&lt;br&gt;&lt;br&gt;This is end-of-chapter3 exercise of the book Core Python Programming.
&lt;br&gt;&lt;br&gt;I'm reading/searching in the book and other materials but is 
&lt;br&gt;unsuccessful. There are at least 50 exceptions listed but I can't find 
&lt;br&gt;anything close.
&lt;br&gt;&lt;br&gt;I commented out my modified script to just what *should* be applicable. 
&lt;br&gt;Can you please point me to the right direction? TIA.
&lt;br&gt;&lt;br&gt;&lt;br&gt;-- 
&lt;br&gt;Regards,
&lt;br&gt;bibs M.
&lt;br&gt;&lt;br&gt;Host/Kernel/OS &amp;nbsp;&amp;quot;cc000002695&amp;quot; running Linux 2.6.31-5.slh.4-sidux-686 
&lt;br&gt;[sidux 2009-02 Αιθήρ - kde-full - (200907141427) ]
&lt;br&gt;www.sidux.com
&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26576284&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Equivalent-exception-of-os.path.exists%28%29-tp26576284p26576284.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26576129</id>
	<title>Re: Python best practices</title>
	<published>2009-11-30T04:29:14Z</published>
	<updated>2009-11-30T04:29:14Z</updated>
	<author>
		<name>Wayne Werner</name>
	</author>
	<content type="html">&lt;div class=&quot;gmail_quote&quot;&gt;On Mon, Nov 30, 2009 at 4:06 AM, ALAN GAULD &lt;span dir=&quot;ltr&quot;&gt;&amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26576129&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;alan.gauld@...&lt;/a&gt;&amp;gt;&lt;/span&gt; wrote:&lt;br&gt;&lt;blockquote class=&quot;gmail_quote&quot; style=&quot;margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;&quot;&gt;

&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Thats what I think is meant by bad practice in returning &lt;/div&gt;&lt;div&gt;multiple values. The function returns two completely different &lt;/div&gt;&lt;div&gt;things depending on some input flag.&lt;/div&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;div&gt;&lt;br&gt;&lt;/div&gt;Now that&amp;#39;s something I definitely agree with! I can&amp;#39;t think of a single case where that would make sense or be good practice. It would just create a ton more work for the programmer/maintainer and obfuscate the code.&lt;div&gt;

&lt;br&gt;&lt;/div&gt;&lt;div&gt;OTOH I can think of several reasons/times I&amp;#39;ve returned multiple related values - for instance I was just writing a function for homework that found the continuous subsequence with the greatest total that returned the start, end, and total (more for my benefit than any other requirement... still, in terms of time complexity and program complexity, it makes much more sense than trying to break it up as &amp;quot;getMaxTotal&amp;quot; &amp;quot;getstart&amp;quot; and &amp;quot;getend&amp;quot; functions - that would just be ridiculous!)&lt;/div&gt;

&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;-Wayne&lt;br&gt;&lt;br clear=&quot;all&quot;&gt;&lt;br&gt;-- &lt;br&gt;To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi&lt;br&gt;


&lt;/div&gt;
&lt;br /&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26576129&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Python-best-practices-tp26539015p26576129.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26576030</id>
	<title>Re: python closures</title>
	<published>2009-11-30T04:21:54Z</published>
	<updated>2009-11-30T04:21:54Z</updated>
	<author>
		<name>Wayne Werner</name>
	</author>
	<content type="html">&lt;br&gt;&lt;br&gt;&lt;div class=&quot;gmail_quote&quot;&gt;On Mon, Nov 30, 2009 at 4:24 AM, spir &lt;span dir=&quot;ltr&quot;&gt;&amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26576030&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;denis.spir@...&lt;/a&gt;&amp;gt;&lt;/span&gt; wrote:&lt;br&gt;&lt;blockquote class=&quot;gmail_quote&quot; style=&quot;margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;&quot;&gt;

&lt;div id=&quot;:1sk&quot; class=&quot;ii gt&quot;&gt;which seems to indicate python really embeds &amp;quot;symbolic references&amp;quot; (*) to outer *variables*, when creating a closure for g0. Not &amp;quot;pointer references&amp;quot; (**), otherwise the replacement of x would not be seen by the closure --like in the case of default-parameter.&lt;br&gt;


Actually, I find this _Bad_. Obviously, the func&amp;#39;s behaviour and result depend on arbitrary external values (referentially opaque). What do you think?&lt;br&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;I&amp;#39;m not sure *why*/how this behaviour really works, other than it treats x as a global variable... and probably treats n as something similar.&lt;div&gt;

&lt;br&gt;&lt;/div&gt;&lt;div&gt;I don&amp;#39;t know how bad I find it - you should be declaring the variables you&amp;#39;re planning to use in your function anyway... I&amp;#39;m sure there&amp;#39;s *some* case that it would end out problematic, but I can&amp;#39;t think of one ATM.&lt;/div&gt;

&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;-Wayne&lt;br clear=&quot;all&quot;&gt;&lt;br&gt;-- &lt;br&gt;To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi&lt;br&gt;


&lt;/div&gt;
&lt;br /&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26576030&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/python-closures-tp26572340p26576030.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26572340</id>
	<title>python closures</title>
	<published>2009-11-30T02:24:45Z</published>
	<updated>2009-11-30T02:24:45Z</updated>
	<author>
		<name>spir</name>
	</author>
	<content type="html">Hello,
&lt;br&gt;&lt;br&gt;Below startup definitions:
&lt;br&gt;&lt;br&gt;x = 1
&lt;br&gt;&lt;br&gt;def f():
&lt;br&gt;&amp;nbsp; n = 1
&lt;br&gt;&amp;nbsp; def g0(a):
&lt;br&gt;&amp;nbsp; &amp;nbsp; print (x + n + a) 
&lt;br&gt;&amp;nbsp; return g0
&lt;br&gt;&lt;br&gt;&lt;br&gt;I'm surprised the snippet below works as expected (py 2.6) without any trick:
&lt;br&gt;&lt;br&gt;g = f()
&lt;br&gt;g(1)	# --&amp;gt; 3
&lt;br&gt;&lt;br&gt;This means a (real) closure is built for g0, or what?
&lt;br&gt;Thought I would need instead to use the old trick of pseudo default-parameters:
&lt;br&gt;&lt;br&gt;def f():
&lt;br&gt;&amp;nbsp; n = 1
&lt;br&gt;&amp;nbsp; def g0(a, n=n, x=x):
&lt;br&gt;&amp;nbsp; &amp;nbsp; print (x + n + a) 
&lt;br&gt;&amp;nbsp; return g0
&lt;br&gt;&lt;br&gt;to let the inner func g0 &amp;quot;remember&amp;quot; outer values. Why is this idiom used, then? Has something changed, or do I miss a relevant point?
&lt;br&gt;&lt;br&gt;The bit below also works:
&lt;br&gt;&lt;br&gt;x = 2
&lt;br&gt;...
&lt;br&gt;g(1)	# --&amp;gt; 4
&lt;br&gt;&lt;br&gt;which seems to indicate python really embeds &amp;quot;symbolic references&amp;quot; (*) to outer *variables*, when creating a closure for g0. Not &amp;quot;pointer references&amp;quot; (**), otherwise the replacement of x would not be seen by the closure --like in the case of default-parameter.
&lt;br&gt;Actually, I find this _Bad_. Obviously, the func's behaviour and result depend on arbitrary external values (referentially opaque). What do you think?
&lt;br&gt;&lt;br&gt;Denis
&lt;br&gt;&lt;br&gt;(*) by name, indirect access, second table lookup
&lt;br&gt;(**) by address, direct access, no second lookup
&lt;br&gt;________________________________
&lt;br&gt;&lt;br&gt;la vita e estrany
&lt;br&gt;&lt;br&gt;&lt;a href=&quot;http://spir.wikidot.com/&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://spir.wikidot.com/&lt;/a&gt;&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26572340&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/python-closures-tp26572340p26572340.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26572080</id>
	<title>Re: Python best practices</title>
	<published>2009-11-30T02:06:14Z</published>
	<updated>2009-11-30T02:06:14Z</updated>
	<author>
		<name>Alan Gauld</name>
	</author>
	<content type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;div style=&quot;font-family:'Courier New', courier, monaco, monospace, sans-serif;font-size:10pt&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;Code Complete by Steve McConnell is an excellent book&lt;/div&gt;&lt;div&gt;and I agree with almost everything in it. Including&amp;nbsp;&lt;/div&gt;&lt;div&gt;the advice about functions.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;But as I said earlier the concept of multiple return&amp;nbsp;&lt;/div&gt;&lt;div&gt;values in Python is similar to returning a struct in C.&lt;/div&gt;&lt;div&gt;The key thing is that the function has a single purpose&amp;nbsp;&lt;/div&gt;&lt;div&gt;so if returning multiple values or a struct the values&amp;nbsp;&lt;/div&gt;&lt;div&gt;within that group should be related to the single purpose&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;In other words don't write a function called&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;def getLengthOrContent(dataStream, flag)&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;data = readData(dataAStream) &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;if
 flag:&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; return len(data)&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;else:&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; return data&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Thats what I think is meant by bad practice in returning&amp;nbsp;&lt;/div&gt;&lt;div&gt;multiple values. The function returns two completely different&amp;nbsp;&lt;/div&gt;&lt;div&gt;things depending on some input flag.&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;Alan Gauld&lt;br&gt;Author of the Learn To Program website&lt;br&gt;&lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://www.alan-g.me.uk&quot;&gt;http://www.alan-g.me.uk/&lt;br&gt;&lt;/a&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div style=&quot;font-family:Courier New, courier, monaco, monospace, sans-serif;font-size:10pt&quot;&gt;&lt;br&gt;&lt;div style=&quot;font-family:times new roman, new york, times, serif;font-size:12pt&quot;&gt;&lt;font size=&quot;2&quot; face=&quot;Tahoma&quot;&gt;&lt;hr size=&quot;1&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;From:&lt;/span&gt;&lt;/b&gt; Albert-Jan Roskam &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26572080&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;fomcl@...&lt;/a&gt;&amp;gt;&lt;br&gt;&lt;b&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;To:&lt;/span&gt;&lt;/b&gt; &lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26572080&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;tutor@...&lt;/a&gt;; Alan Gauld
 &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26572080&amp;i=2&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;alan.gauld@...&lt;/a&gt;&amp;gt;&lt;br&gt;&lt;b&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Sent:&lt;/span&gt;&lt;/b&gt; Monday, 30 November, 2009 9:58:21&lt;br&gt;&lt;b&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Subject:&lt;/span&gt;&lt;/b&gt; Re: [Tutor] Python best practices&lt;br&gt;&lt;/font&gt;&lt;br&gt;&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td valign=&quot;top&quot; style=&quot;font:inherit;&quot;&gt;&lt;div&gt;I'm currently reading the book &quot;Code Complete&quot; (I don't know the author name), which gives a lot of useful best practices. It's not specifically about one programming language. The author stresses that the majority of those practices are a matter of consensus/consistency and not a matter of religion. There is no one best way to do it.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;Re: functions, the author recommends that they have one purpose and one purpose only, and that a function returns only one result.&lt;br&gt;&lt;br&gt;Cheers!!&lt;br&gt;Albert-Jan&lt;br&gt;&lt;br&gt;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;br&gt;In the face of ambiguity, refuse the temptation to guess.&lt;br&gt;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;br&gt;&lt;br&gt;--- On &lt;b&gt;Mon, 11/30/09, Alan Gauld &lt;i&gt;&amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26572080&amp;i=3&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;alan.gauld@...&lt;/a&gt;&amp;gt;&lt;/i&gt;&lt;/b&gt; wrote:&lt;br&gt;&lt;/div&gt;
&lt;blockquote style=&quot;PADDING-LEFT:5px;MARGIN-LEFT:5px;BORDER-LEFT:rgb(16,16,255) 2px solid;&quot;&gt;&lt;br&gt;From: Alan Gauld &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26572080&amp;i=4&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;alan.gauld@...&lt;/a&gt;&amp;gt;&lt;br&gt;Subject: Re: [Tutor] Python best practices&lt;br&gt;To: &lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26572080&amp;i=5&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;tutor@...&lt;/a&gt;&lt;br&gt;Date: Monday, November 30, 2009, 1:57 AM&lt;br&gt;&lt;br&gt;
&lt;div class=&quot;plainMail&quot;&gt;&quot;spir&quot; &amp;lt;&lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://us.mc1107.mail.yahoo.com/mc/compose?to=denis.spir@free.fr&quot;&gt;denis.spir@...&lt;/a&gt;&amp;gt; wrote&lt;br&gt;&lt;br&gt;&amp;gt;&amp;gt; &amp;gt; - functions should return one value (im not 100% of this one)&lt;br&gt;&amp;gt;&amp;gt; &lt;br&gt;&amp;gt;&amp;gt; I 100% disagree or with this one.&lt;br&gt;&amp;gt; &lt;br&gt;&amp;gt; Could you explain this bit, Lie? I'm very interested.&lt;br&gt;&amp;gt; I use multiple-value result myself, for it's so practicle in given cases.&lt;br&gt;&lt;br&gt;My take on this is that in Python its OK to return multiple values if it&lt;br&gt;is as a tuple - because a tuple is really a single value. Its like returning&lt;br&gt;a record in Pascal or a struct in C or a List in Lisp...&lt;br&gt;&lt;br&gt;&amp;gt; But it makes me uneasy; also have the impression (why?) it&lt;br&gt;&amp;gt; reveals wrong design.&lt;br&gt;&lt;br&gt;Its better than....&lt;br&gt;&lt;br&gt;&amp;gt; a function both to have an effect (eg assignment outside the func scope) and&lt;br&gt;&amp;gt; return a value.&lt;br&gt;&lt;br&gt;Side effects in functions are
 nearly always bad news and are always&lt;br&gt;avoidable if you can return multiple values (or pass arguments by reference).&lt;br&gt;&lt;br&gt;&amp;gt; &quot;Command-Query Separation Principle&quot; (Eiffel) &amp;amp; Pascal &quot;procedure vs function&quot;.&lt;br&gt;&lt;br&gt;You can have Command/Query separation without having side-effects.&lt;br&gt;A Query is a procedure or function that doesn't change anything but just&lt;br&gt;returns a result. A Command changes something, but it can be the thing&lt;br&gt;passed to it - or if a method of a class the internal data of the class.&lt;br&gt;Again a command can be a function or a procedure. Those are separate&lt;br&gt;concepts. (I very rarely write procedures in real programs - there is nearly&lt;br&gt;always something useful you can return - and in an object that's usually&lt;br&gt;a minimum of self! (Returning self is the default idiom in Smalltalk - it&lt;br&gt;allows chaining of methods)&lt;br&gt;&lt;br&gt;HTH,&lt;br&gt;&lt;br&gt;Alan G. &lt;br&gt;&lt;br&gt;_______________________________________________&lt;br&gt;Tutor maillist&amp;nbsp; -&amp;nbsp;
 &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://us.mc1107.mail.yahoo.com/mc/compose?to=Tutor@python.org&quot;&gt;Tutor@...&lt;/a&gt;&lt;br&gt;To unsubscribe or change subscription options:&lt;br&gt;&lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br&gt;



      &lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;position:fixed&quot;&gt;&lt;/div&gt;


&lt;!-- cg5.c241.mail.ird.yahoo.com compressed/chunked Fri Nov  6 01:34:17 PST 2009 --&gt;
&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;&lt;br /&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26572080&amp;i=6&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Python-best-practices-tp26539015p26572080.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26571969</id>
	<title>Re: Python best practices</title>
	<published>2009-11-30T01:58:21Z</published>
	<updated>2009-11-30T01:58:21Z</updated>
	<author>
		<name>Albert-jan Roskam</name>
	</author>
	<content type="html">&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot;&gt;&lt;tr&gt;&lt;td valign=&quot;top&quot; style=&quot;font: inherit;&quot;&gt;&lt;DIV&gt;I'm currently reading the book &quot;Code Complete&quot; (I don't know the author name), which gives a lot of useful best practices. It's not specifically about one programming language. The author stresses that the majority of those practices are a matter of consensus/consistency and not a matter of religion. There is no one best way to do it.&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;Re: functions, the author recommends that they have one purpose and one purpose only, and that a function returns only one result.&lt;BR&gt;&lt;BR&gt;Cheers!!&lt;BR&gt;Albert-Jan&lt;BR&gt;&lt;BR&gt;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;BR&gt;In the face of ambiguity, refuse the temptation to guess.&lt;BR&gt;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;BR&gt;&lt;BR&gt;--- On &lt;B&gt;Mon, 11/30/09, Alan Gauld &lt;I&gt;&amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26571969&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;alan.gauld@...&lt;/a&gt;&amp;gt;&lt;/I&gt;&lt;/B&gt; wrote:&lt;BR&gt;&lt;/DIV&gt;
&lt;BLOCKQUOTE style=&quot;PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: rgb(16,16,255) 2px solid&quot;&gt;&lt;BR&gt;From: Alan Gauld &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26571969&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;alan.gauld@...&lt;/a&gt;&amp;gt;&lt;BR&gt;Subject: Re: [Tutor] Python best practices&lt;BR&gt;To: &lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26571969&amp;i=2&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;tutor@...&lt;/a&gt;&lt;BR&gt;Date: Monday, November 30, 2009, 1:57 AM&lt;BR&gt;&lt;BR&gt;
&lt;DIV class=plainMail&gt;&quot;spir&quot; &amp;lt;&lt;A href=&quot;http://us.mc1107.mail.yahoo.com/mc/compose?to=denis.spir@free.fr&quot; ymailto=&quot;mailto:denis.spir@free.fr&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;denis.spir@...&lt;/A&gt;&amp;gt; wrote&lt;BR&gt;&lt;BR&gt;&amp;gt;&amp;gt; &amp;gt; - functions should return one value (im not 100% of this one)&lt;BR&gt;&amp;gt;&amp;gt; &lt;BR&gt;&amp;gt;&amp;gt; I 100% disagree or with this one.&lt;BR&gt;&amp;gt; &lt;BR&gt;&amp;gt; Could you explain this bit, Lie? I'm very interested.&lt;BR&gt;&amp;gt; I use multiple-value result myself, for it's so practicle in given cases.&lt;BR&gt;&lt;BR&gt;My take on this is that in Python its OK to return multiple values if it&lt;BR&gt;is as a tuple - because a tuple is really a single value. Its like returning&lt;BR&gt;a record in Pascal or a struct in C or a List in Lisp...&lt;BR&gt;&lt;BR&gt;&amp;gt; But it makes me uneasy; also have the impression (why?) it&lt;BR&gt;&amp;gt; reveals wrong design.&lt;BR&gt;&lt;BR&gt;Its better than....&lt;BR&gt;&lt;BR&gt;&amp;gt; a function both to have an effect (eg assignment outside the func scope) and&lt;BR&gt;&amp;gt; return a value.&lt;BR&gt;&lt;BR&gt;Side effects in functions are
 nearly always bad news and are always&lt;BR&gt;avoidable if you can return multiple values (or pass arguments by reference).&lt;BR&gt;&lt;BR&gt;&amp;gt; &quot;Command-Query Separation Principle&quot; (Eiffel) &amp;amp; Pascal &quot;procedure vs function&quot;.&lt;BR&gt;&lt;BR&gt;You can have Command/Query separation without having side-effects.&lt;BR&gt;A Query is a procedure or function that doesn't change anything but just&lt;BR&gt;returns a result. A Command changes something, but it can be the thing&lt;BR&gt;passed to it - or if a method of a class the internal data of the class.&lt;BR&gt;Again a command can be a function or a procedure. Those are separate&lt;BR&gt;concepts. (I very rarely write procedures in real programs - there is nearly&lt;BR&gt;always something useful you can return - and in an object that's usually&lt;BR&gt;a minimum of self! (Returning self is the default idiom in Smalltalk - it&lt;BR&gt;allows chaining of methods)&lt;BR&gt;&lt;BR&gt;HTH,&lt;BR&gt;&lt;BR&gt;Alan G. &lt;BR&gt;&lt;BR&gt;_______________________________________________&lt;BR&gt;Tutor maillist&amp;nbsp; -&amp;nbsp;
 &lt;A href=&quot;http://us.mc1107.mail.yahoo.com/mc/compose?to=Tutor@python.org&quot; ymailto=&quot;mailto:Tutor@python.org&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/A&gt;&lt;BR&gt;To unsubscribe or change subscription options:&lt;BR&gt;&lt;A href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=_blank rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/A&gt;&lt;BR&gt;&lt;/DIV&gt;&lt;/BLOCKQUOTE&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br&gt;



&lt;br /&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26571969&amp;i=3&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Python-best-practices-tp26539015p26571969.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26569885</id>
	<title>Re: Python best practices</title>
	<published>2009-11-29T21:42:28Z</published>
	<updated>2009-11-29T21:42:28Z</updated>
	<author>
		<name>Lie Ryan</name>
	</author>
	<content type="html">On 11/29/2009 8:59 PM, spir wrote:
&lt;br&gt;&amp;gt; Lie Ryan&amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26569885&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;lie.1296@...&lt;/a&gt;&amp;gt; &amp;nbsp;dixit:
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt;&amp;gt;&amp;gt; - functions should return one value (im not 100% of this one)
&lt;br&gt;&amp;gt;&amp;gt;
&lt;br&gt;&amp;gt;&amp;gt; I 100% disagree or with this one.
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; Could you explain this bit, Lie? I'm very interested.
&lt;br&gt;&amp;gt; I use multiple-value result myself, for it's so practicle in given
&lt;br&gt;&amp;nbsp;&amp;gt; cases. But it makes me uneasy; also have the impression (why?) it
&lt;br&gt;&amp;nbsp;&amp;gt; reveals wrong design.
&lt;br&gt;&lt;br&gt;I guess this particular &amp;quot;advice&amp;quot; comes from languages like C/C++ where 
&lt;br&gt;returning a struct means copying the whole struct, which could be a very 
&lt;br&gt;expensive. In python, returning tuple is cheap; it's just copying 
&lt;br&gt;references. Don't hesitate to return multiple values, whenever it is 
&lt;br&gt;meaningful/useful (either tuple or list or dict or ...).
&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26569885&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Python-best-practices-tp26539015p26569885.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26568479</id>
	<title>Re: Python best practices</title>
	<published>2009-11-29T17:22:35Z</published>
	<updated>2009-11-29T17:22:35Z</updated>
	<author>
		<name>Kent Johnson</name>
	</author>
	<content type="html">On Sat, Nov 28, 2009 at 1:26 PM, Lie Ryan &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26568479&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;lie.1296@...&lt;/a&gt;&amp;gt; wrote:
&lt;br&gt;&amp;gt; On 11/27/2009 7:03 PM, Stefan Lesicnik wrote:
&lt;br&gt;&lt;br&gt;&amp;gt;&amp;gt; - functions should return one value (im not 100% of this one)
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; I 100% disagree or with this one.
&lt;br&gt;&lt;br&gt;Me too. Actually I think it is meaningless - every Python function
&lt;br&gt;returns one value, an instance of some class. Each of these functions
&lt;br&gt;returns one value - a list, a tuple and a Foo instance:
&lt;br&gt;&lt;br&gt;def make_list():
&lt;br&gt;&amp;nbsp; return [0, 1]
&lt;br&gt;&lt;br&gt;def make_tuple():
&lt;br&gt;&amp;nbsp; return 0, 1
&lt;br&gt;&lt;br&gt;class Foo(object):
&lt;br&gt;&amp;nbsp; def __init__(self, a, b):
&lt;br&gt;&amp;nbsp; &amp;nbsp; self.a = a
&lt;br&gt;&amp;nbsp; &amp;nbsp; self.b = b
&lt;br&gt;&lt;br&gt;def make_Foo():
&lt;br&gt;&amp;nbsp; return Foo(0, 1)
&lt;br&gt;&lt;br&gt;&lt;br&gt;BTW when I first saw this rule I mis-read it as &amp;quot;Functions should have
&lt;br&gt;a single point of return&amp;quot; which is fairly common advice that I also
&lt;br&gt;disagree with.
&lt;br&gt;&lt;br&gt;Kent
&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26568479&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Python-best-practices-tp26539015p26568479.html" />
</entry>

<entry>
	<id>tag:old.nabble.com,2006:post-26568201</id>
	<title>Re: Python best practices</title>
	<published>2009-11-29T16:57:23Z</published>
	<updated>2009-11-29T16:57:23Z</updated>
	<author>
		<name>Alan Gauld</name>
	</author>
	<content type="html">&amp;quot;spir&amp;quot; &amp;lt;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26568201&amp;i=0&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;denis.spir@...&lt;/a&gt;&amp;gt; wrote
&lt;br&gt;&lt;br&gt;&amp;gt;&amp;gt; &amp;gt; - functions should return one value (im not 100% of this one)
&lt;br&gt;&amp;gt;&amp;gt;
&lt;br&gt;&amp;gt;&amp;gt; I 100% disagree or with this one.
&lt;br&gt;&amp;gt;
&lt;br&gt;&amp;gt; Could you explain this bit, Lie? I'm very interested.
&lt;br&gt;&amp;gt; I use multiple-value result myself, for it's so practicle in given cases.
&lt;br&gt;&lt;br&gt;My take on this is that in Python its OK to return multiple values if it
&lt;br&gt;is as a tuple - because a tuple is really a single value. Its like 
&lt;br&gt;returning
&lt;br&gt;a record in Pascal or a struct in C or a List in Lisp...
&lt;br&gt;&lt;br&gt;&amp;gt; But it makes me uneasy; also have the impression (why?) it
&lt;br&gt;&amp;gt; reveals wrong design.
&lt;br&gt;&lt;br&gt;Its better than....
&lt;br&gt;&lt;br&gt;&amp;gt; a function both to have an effect (eg assignment outside the func scope) 
&lt;br&gt;&amp;gt; and
&lt;br&gt;&amp;gt; return a value.
&lt;br&gt;&lt;br&gt;Side effects in functions are nearly always bad news and are always
&lt;br&gt;avoidable if you can return multiple values (or pass arguments by 
&lt;br&gt;reference).
&lt;br&gt;&lt;br&gt;&amp;gt; &amp;quot;Command-Query Separation Principle&amp;quot; (Eiffel) &amp; Pascal &amp;quot;procedure vs 
&lt;br&gt;&amp;gt; function&amp;quot;.
&lt;br&gt;&lt;br&gt;You can have Command/Query separation without having side-effects.
&lt;br&gt;A Query is a procedure or function that doesn't change anything but just
&lt;br&gt;returns a result. A Command changes something, but it can be the thing
&lt;br&gt;passed to it - or if a method of a class the internal data of the class.
&lt;br&gt;Again a command can be a function or a procedure. Those are separate
&lt;br&gt;concepts. (I very rarely write procedures in real programs - there is 
&lt;br&gt;nearly
&lt;br&gt;always something useful you can return - and in an object that's usually
&lt;br&gt;a minimum of self! (Returning self is the default idiom in Smalltalk - it
&lt;br&gt;allows chaining of methods)
&lt;br&gt;&lt;br&gt;HTH,
&lt;br&gt;&lt;br&gt;Alan G. 
&lt;br&gt;&lt;br&gt;&lt;br&gt;_______________________________________________
&lt;br&gt;Tutor maillist &amp;nbsp;- &amp;nbsp;&lt;a href=&quot;http://old.nabble.com/user/SendEmail.jtp?type=post&amp;post=26568201&amp;i=1&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;Tutor@...&lt;/a&gt;
&lt;br&gt;To unsubscribe or change subscription options:
&lt;br&gt;&lt;a href=&quot;http://mail.python.org/mailman/listinfo/tutor&quot; target=&quot;_top&quot; rel=&quot;nofollow&quot;&gt;http://mail.python.org/mailman/listinfo/tutor&lt;/a&gt;&lt;br&gt;</content>
	<link rel="alternate" type="text/html" href="http://old.nabble.com/Python-best-practices-tp26539015p26568201.html" />
</entry>

</feed>
