Re: Scoping Issues

2012-05-25 Thread Jean-Michel Pichavant

Andrew Berg wrote:

On 5/24/2012 8:59 PM, Dave Angel wrote:
  

so I fixed that, and got
 inconsistent use of tabs and spaces in indentation

because you mistakenly used tabs for indentation.

Not to start another tabs-vs.-spaces discussion, 
  
Too late, the seeds of war have been planted, we reached to point of no 
return.


The OP mistakenly used spaces with its tabs for indentation.

JM
--
http://mail.python.org/mailman/listinfo/python-list


Re: Scoping Issues

2012-05-25 Thread Ethan Furman

Andrew Berg wrote:

On 5/24/2012 8:59 PM, Dave Angel wrote:

so I fixed that, and got
 inconsistent use of tabs and spaces in indentation

because you mistakenly used tabs for indentation.

Not to start another tabs-vs.-spaces discussion, but tabs are perfectly
legal indentation in Python. That exception is raised when the
interpreter can't determine how much a line is indented because tabs and
spaces are both used.


To be clear: each entire suite must be consistent with its use of either 
tabs /or/ spaces -- attempting to use both, even on seperate lines, 
raises `TabError: inconsistent use of tabs and spaces in indentation`. 
(Python 3, of course. ;)


~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list


Scoping Issues

2012-05-24 Thread SherjilOzair
def adder():
s = 0
def a(x):
s += x
return sum
return a

pos, neg = adder(), adder()
for i in range(10):
print pos(i), neg(-2*i)

This should work, right? Why does it not?

Checkout slide no. 37 of a Tour of Go to know inspiration. Just wanted to see 
if python was the same as Go in this regard. Sorry, if I'm being rude, or 
anything. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scoping Issues

2012-05-24 Thread MRAB

On 25/05/2012 02:23, SherjilOzair wrote:

def adder():
s = 0
def a(x):
s += x
return sum
return a

pos, neg = adder(), adder()
for i in range(10):
print pos(i), neg(-2*i)

This should work, right? Why does it not?

Checkout slide no. 37 of a Tour of Go to know inspiration. Just wanted
to see if python was the same as Go in this regard. Sorry, if I'm being
rude, or anything.


If you bind to a name (assign to a variable) in a function, that name
is by default local to the function unless it's declared as global 
(which means in this module's namespace) or nonlocal (which means

in the enclosing function's namespace) (Python 3 only).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Scoping Issues

2012-05-24 Thread Steven D'Aprano
On Thu, 24 May 2012 18:23:18 -0700, SherjilOzair wrote:

 def adder():
   s = 0
   def a(x):
   s += x
   return sum
   return a

I think you mean return s, not sum.


 This should work, right? Why does it not?

No, it shouldn't. When you have an assignment, such as s += x, Python 
treats s as a local variable. Since s is local to the inner function a(), 
it has no initial value, and the += fails.

In Python 3, you can declare s to be a non-local variable with the 
nonlocal keyword:

def adder():
s = 0
def a(x):
nonlocal s
s += x
return s
return a


which now works the way you should expect:

 add = adder()
 add(1)
1
 add(2)
3
 add(5)
8


But in Python 2, which you are using, there is no nonlocal keyword and 
you can only write to globals (declaring them with the global keyword) or 
locals (with no declaration), not nonlocals.

There are a number of work-arounds to this. One is to use a callable 
class:

class Adder:
def __init__(self):
self.s = 0
def __call__(self, x):
self.s += x
return self.s


Another is to use an extra level of indirection, and a mutable argument. 
Instead of rebinding the non-local, you simply modify it in place:

def adder():
s = [0]
def a(x):
s[0] += x
return s[0]
return a



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scoping Issues

2012-05-24 Thread Chris Rebert
On Thu, May 24, 2012 at 6:23 PM, SherjilOzair sherjiloz...@gmail.com wrote:
 def adder():
        s = 0
        def a(x):

Add a nonlocal s declaration right here.
See http://www.python.org/dev/peps/pep-3104/

            s += x
            return sum
        return a

 pos, neg = adder(), adder()
 for i in range(10):
        print pos(i), neg(-2*i)

 This should work, right? Why does it not?

Python doesn't have a C-like variable declaration scheme. So without a
`global` or `nonlocal` declaration, you can only assign to names which
reside in the innermost scope.

 Checkout slide no. 37 of a Tour of Go to know inspiration.

You mean slide #38 (And functions are full closures.).

 Just wanted to see if python was the same as Go in this regard. Sorry, if I'm 
 being rude, or anything.

I would suggest reading through the Python Language Reference
(http://docs.python.org/release/3.1.5/reference/index.html ) prior to
asking further questions, as it may well answer them for you.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scoping Issues

2012-05-24 Thread Dave Angel
On 05/24/2012 09:23 PM, SherjilOzair wrote:
 def adder():
   s = 0
   def a(x):
   s += x
   return sum
   return a

 pos, neg = adder(), adder()
 for i in range(10):
   print pos(i), neg(-2*i)

 This should work, right? Why does it not?


Guess that depends on what you mean by work.  First, it gets a syntax
error on the print function call, because you omitted the parens.  When
I fixed that, I got
   UnboundLocalError: local variable 's' referenced before assignment

so I fixed that, and got
 inconsistent use of tabs and spaces in indentation

because you mistakenly used tabs for indentation. Then I got the output
built-in function sum built-in function sum

because sum is a built-in function.  Presumably you meant to return s,
not sum.

Here's what I end up with, and it seems to work fine in Python 3.2 on Linux:

def adder():
s = 0
def a(x):
nonlocal s
s += x
return s
return a

pos, neg = adder(), adder()
for i in range(10):
print (pos(i), neg(-2*i))

.
Output is:

0 0
1 -2
3 -6
6 -12
10 -20
15 -30
21 -42
28 -56
36 -72
45 -90


-- 

DaveA

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scoping Issues

2012-05-24 Thread Andrew Berg
On 5/24/2012 8:59 PM, Dave Angel wrote:
 so I fixed that, and got
  inconsistent use of tabs and spaces in indentation
 
 because you mistakenly used tabs for indentation.
Not to start another tabs-vs.-spaces discussion, but tabs are perfectly
legal indentation in Python. That exception is raised when the
interpreter can't determine how much a line is indented because tabs and
spaces are both used.

-- 
CPython 3.3.0a3 | Windows NT 6.1.7601.17790
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scoping Issues

2012-05-24 Thread Dave Angel
On 05/24/2012 10:27 PM, Andrew Berg wrote:
 On 5/24/2012 8:59 PM, Dave Angel wrote:
 so I fixed that, and got
  inconsistent use of tabs and spaces in indentation

 because you mistakenly used tabs for indentation.
 Not to start another tabs-vs.-spaces discussion, but tabs are perfectly
 legal indentation in Python. That exception is raised when the
 interpreter can't determine how much a line is indented because tabs and
 spaces are both used.


I configure my editor(s) to always use spaces for indentation.  So a
file that currently uses tabs is unusable to me.  You of course are
right that tabs are syntactically correct, but until I find another
editor that makes tabs visible (like the one I used 20 years ago)
they're unacceptable to me.

-- 

DaveA

-- 
http://mail.python.org/mailman/listinfo/python-list