On 7/11/2012 11:39 PM, Dennis Lee Bieber wrote:
On 12 Jul 2012 03:53:03 GMT, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info declaimed the following in
gmane.comp.python.general:
ALU class?
Googling gives me no clue.
Arithmetic/Logic Unit
On Sun, 15 Jul 2012 10:49:48 +1000, Chris Angelico wrote:
On Sun, Jul 15, 2012 at 9:29 AM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
Not necessarily *compile* time, but the distinction is between when the
function is defined (which may at compile time, or it may be at run
On Sun, Jul 15, 2012 at 6:32 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
At compile time, Python parses the source code and turns it into byte-
code. Class and function definitions are executed at run time, the same
as any other statement.
Between the parse step and the
On 7/15/2012 4:32 AM, Steven D'Aprano wrote:
On Sun, 15 Jul 2012 10:49:48 +1000, Chris Angelico wrote:
On Sun, Jul 15, 2012 at 9:29 AM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
Not necessarily *compile* time, but the distinction is between when the
function is defined
On 15/07/12 10:44:09, Chris Angelico wrote:
On Sun, Jul 15, 2012 at 6:32 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
At compile time, Python parses the source code and turns it into byte-
code. Class and function definitions are executed at run time, the same
as any other
On Fri, 13 Jul 2012 21:53:10 -0700, rusi wrote:
On Jul 14, 8:43 am, Steven D'Aprano steve
+comp.lang.pyt...@pearwood.info wrote:
On Fri, 13 Jul 2012 19:31:24 -0700, rusi wrote:
Consider the following
def foo(x):
i = 100
if x:
j = [i for i in range(10)]
Alister於 2012年7月12日星期四UTC+8下午5時44分15秒寫道:
On Wed, 11 Jul 2012 08:43:11 +0200, Daniel Fetchinson wrote:
gt;gt; funcs = [ lambda x: x**i for i in range( 5 ) ]
gt;gt; print funcs[0]( 2 )
gt;gt; print funcs[1]( 2 )
gt;gt; print funcs[2]( 2 )
gt;gt;
gt;gt; This gives me
gt;gt;
gt;gt; 16 16 16
On Fri, 13 Jul 2012 12:54:02 -0600, Ian Kelly wrote:
On Fri, Jul 13, 2012 at 11:53 AM, Hans Mulder han...@xs4all.nl wrote:
The function `function` refers to a variable `VERBOSE` that isn't
local. In some programming langauages, the interpreter would then scan
the call stack at run-time,
On Sat, Jul 14, 2012 at 4:29 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
I don't remember whether it is Javascript or PHP that uses dynamic
binding, but whichever it is, it is generally considered to be a bad
idea, at least as the default or only behaviour.
Bash is
On Sun, Jul 15, 2012 at 9:29 AM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
Not necessarily *compile* time, but the distinction is between when the
function is defined (which may at compile time, or it may be at run time)
versus when the function is called.
I'd treat the
On Thu, 12 Jul 2012 21:33:40 -0700, rusi wrote:
On Jul 11, 11:41 am, Daniel Fetchinson fetchin...@googlemail.com
wrote:
funcs = [ lambda x: x**i for i in range( 5 ) ] print funcs[0]( 2 )
print funcs[1]( 2 )
print funcs[2]( 2 )
This gives me
16
16
16
When I was excepting
1
2
4
On Jul 13, 11:36 am, Steven D'Aprano steve
+comp.lang.pyt...@pearwood.info wrote:
On Thu, 12 Jul 2012 21:33:40 -0700, rusi wrote:
On Jul 11, 11:41 am, Daniel Fetchinson fetchin...@googlemail.com
wrote:
funcs = [ lambda x: x**i for i in range( 5 ) ] print funcs[0]( 2 )
print funcs[1]( 2 )
To come back to the OPs question.
Variables can be assigned. Or they can be bound.
[C++ programmers will be familiar with the difference between
initialization and assignment]
List comprehensions are defined in terms of assignment to the local
variable rather than binding.
Hence the issue.
VERBOSE = True
def function(arg):
if VERBOSE:
print(calling function with arg %r % arg)
process(arg)
def caller():
VERBOSE = False
function(1)
-
Python semantics: function sees VERBOSE False
Haskell semantics:
On Jul 13, 9:12 pm, Prasad, Ramit ramit.pra...@jpmorgan.com wrote:
VERBOSE = True
def function(arg):
if VERBOSE:
print(calling function with arg %r % arg)
process(arg)
def caller():
VERBOSE = False
function(1)
On Sat, Jul 14, 2012 at 2:46 AM, rusi rustompm...@gmail.com wrote:
Ok let me restate: if python were to work that way (without the
global) we could say either
a Python chooses to have dynamic scoping of variables
or
b There is a bug in python's scoping rules
Or c, there's a declaration at
On 13/07/12 18:12:40, Prasad, Ramit wrote:
VERBOSE = True
def function(arg):
if VERBOSE:
print(calling function with arg %r % arg)
process(arg)
def caller():
VERBOSE = False
function(1)
-
Python semantics: function
VERBOSE = True
def function(arg):
if VERBOSE:
print(calling function with arg %r % arg)
process(arg)
def caller():
VERBOSE = False
function(1)
-
Python semantics: function sees VERBOSE False
Haskell
On Fri, Jul 13, 2012 at 11:53 AM, Hans Mulder han...@xs4all.nl wrote:
The function `function` refers to a variable `VERBOSE` that
isn't local. In some programming langauages, the interpreter
would then scan the call stack at run-time, looking for a scope
where that name is defined. It would
On 13/07/12 20:54:02, Ian Kelly wrote:
I've also seen the distinction described as early vs. late binding
on this list, but I'm not sure how precise that is -- I believe that
terminology more accurately describes whether method and attribute
names are looked up at compile-time or at run-time,
On Jul 13, 10:53 pm, Hans Mulder han...@xs4all.nl wrote:
If you add `global VERBOSE` to `caller`, then there is only one
variable named `VERBOSE` and what `function` does, depends on
the most recent assignment to that variable.
If you remove your `global VERBOSE`, then there are two
On Fri, 13 Jul 2012 19:31:24 -0700, rusi wrote:
Consider the following
def foo(x):
i = 100
if x:
j = [i for i in range(10)]
return i
else:
return i
A simpler example:
def foo():
i = 100
j = [i for i in range(10)]
return i
In Python 3,
On Jul 14, 8:43 am, Steven D'Aprano steve
+comp.lang.pyt...@pearwood.info wrote:
On Fri, 13 Jul 2012 19:31:24 -0700, rusi wrote:
Consider the following
def foo(x):
i = 100
if x:
j = [i for i in range(10)]
return i
else:
return i
A simpler
On Wed, 11 Jul 2012 22:04:51 -0700, 8 Dihedral wrote:
I have to make sure my functor to keep the state variable values for
different objects that call the same functor to behave correctly in
order to avoid passing extra parameters in various objects using the
same functor.
Yo dawg, I
In article mailman.2007.1341988993.4697.python-l...@python.org,
Daniel Fetchinson fetchin...@googlemail.com wrote:
funcs = [ lambda x: x**i for i in range( 5 ) ]
print funcs[0]( 2 )
print funcs[1]( 2 )
print funcs[2]( 2 )
This gives me
16
16
16
When I was excepting
1
On 12/07/2012 07:18, Steven D'Aprano wrote:
On Wed, 11 Jul 2012 22:04:51 -0700, 8 Dihedral wrote:
I have to make sure my functor to keep the state variable values for
different objects that call the same functor to behave correctly in
order to avoid passing extra parameters in various
On 7/11/12 9:21 PM, John Ladasky wrote:
Exactly. It's threads like these which remind me why I never use lambda. I would rather
give a function an explicit name and adhere to the familiar Python syntax, despite the
two extra lines of code. I don't even like the name lambda. It doesn't tell
On Wed, 11 Jul 2012 08:43:11 +0200, Daniel Fetchinson wrote:
funcs = [ lambda x: x**i for i in range( 5 ) ]
print funcs[0]( 2 )
print funcs[1]( 2 )
print funcs[2]( 2 )
This gives me
16 16 16
When I was excepting
1
2
4
Does anyone know why?
And more importantly, what's the
On Thu, 12 Jul 2012 09:44:15 +, Alister wrote:
On Wed, 11 Jul 2012 08:43:11 +0200, Daniel Fetchinson wrote:
funcs = [ lambda x: x**i for i in range( 5 ) ]
[...]
Having read Steve's explanation in the other thread (which I think has
finally flipped the light switch on lambda for me) it
On Wed, Jul 11, 2012 at 9:59 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
On Wed, 11 Jul 2012 08:41:57 +0200, Daniel Fetchinson wrote:
funcs = [ lambda x: x**i for i in range( 5 ) ]
Here's another solution:
from functools import partial
funcs = [partial(lambda i, x:
On 12/07/2012 04:59, Steven D'Aprano wrote:
On Wed, 11 Jul 2012 08:41:57 +0200, Daniel Fetchinson wrote:
funcs = [ lambda x: x**i for i in range( 5 ) ]
Here's another solution:
from functools import partial
funcs = [partial(lambda i, x: x**i, i) for i in range(5)]
Notice that the
On Jul 11, 11:41 am, Daniel Fetchinson fetchin...@googlemail.com
wrote:
funcs = [ lambda x: x**i for i in range( 5 ) ]
print funcs[0]( 2 )
print funcs[1]( 2 )
print funcs[2]( 2 )
This gives me
16
16
16
When I was excepting
1
2
4
Does anyone know why?
Cheers,
Daniel
Your
funcs = [ lambda x: x**i for i in range( 5 ) ]
print funcs[0]( 2 )
print funcs[1]( 2 )
print funcs[2]( 2 )
This gives me
16
16
16
When I was excepting
1
2
4
Does anyone know why?
Cheers,
Daniel
--
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
funcs = [ lambda x: x**i for i in range( 5 ) ]
print funcs[0]( 2 )
print funcs[1]( 2 )
print funcs[2]( 2 )
This gives me
16
16
16
When I was excepting
1
2
4
Does anyone know why?
And more importantly, what's the simplest way to achieve the latter? :)
--
Psss, psss, put it
Hi.
funcs = [ lambda x: x**i for i in range( 5 ) ]
print funcs[0]( 2 )
This gives me
16
When I was excepting
1
Does anyone know why?
Just the way Python lambda expressions bind their variable
references. Inner 'i' references the outer scope's 'i' variable and not
its value 'at the
funcs = [ lambda x: x**i for i in range( 5 ) ]
print funcs[0]( 2 )
This gives me
16
When I was excepting
1
Does anyone know why?
Just the way Python lambda expressions bind their variable
references. Inner 'i' references the outer scope's 'i' variable and not
its value 'at the
On 11/07/2012 2:41 AM, Daniel Fetchinson wrote:
funcs = [ lambda x: x**i for i in range( 5 ) ]
print funcs[0]( 2 )
print funcs[1]( 2 )
print funcs[2]( 2 )
This gives me
16
16
16
When I was excepting
1
2
4
Does anyone know why?
Cheers,
Daniel
I don't understand why you would expect 1, 2,
On Wed, Jul 11, 2012 at 4:28 AM, Colin J. Williams c...@ncf.ca wrote:
I don't understand why you would expect 1, 2, 4.
Because:
funcs[0](2) == 2 ** 0 == 1
funcs[1](2) == 2 ** 1 == 2
funcs[2](2) == 2 ** 2 == 4
Perhaps parentheses will help the order of evaluation:
funcs = [(lambda x: x**i)
Exactly. It's threads like these which remind me why I never use lambda. I
would rather give a function an explicit name and adhere to the familiar Python
syntax, despite the two extra lines of code. I don't even like the name
lambda. It doesn't tell you what it is (unless you're John
On 11/07/12 20:38:18, woooee wrote:
You should not be using lambda in this case
.for x in [2, 3]:
.funcs = [x**ctr for ctr in range( 5 )]
.for p in range(5):
.print x, funcs[p]
.print
The list is called funcs because it is meant to contain functions.
Your code does not
On Wed, 11 Jul 2012 11:38:18 -0700, woooee wrote:
You should not be using lambda in this case
.for x in [2, 3]:
.funcs = [x**ctr for ctr in range( 5 )]
.for p in range(5):
.print x, funcs[p]
.print
If you change the requirements, it's always easy to solve problems.
On Wed, 11 Jul 2012 13:21:34 -0700, John Ladasky wrote:
Exactly. It's threads like these which remind me why I never use
lambda. I would rather give a function an explicit name and adhere to
the familiar Python syntax, despite the two extra lines of code.
lambda is familiar Python syntax,
You should not be using lambda in this case
.for x in [2, 3]:
.funcs = [x**ctr for ctr in range( 5 )]
.for p in range(5):
.print x, funcs[p]
.print
If you change the requirements, it's always easy to solve problems. But
it is the wrong problem that you have solved.
On Thursday, July 12, 2012 12:34:33 AM UTC+8, Ian wrote:
On Wed, Jul 11, 2012 at 4:28 AM, Colin J. Williams lt;c...@ncf.cagt; wrote:
gt; I don#39;t understand why you would expect 1, 2, 4.
Because:
funcs[0](2) == 2 ** 0 == 1
funcs[1](2) == 2 ** 1 == 2
funcs[2](2) == 2 ** 2 == 4
gt;
On Wed, 11 Jul 2012 21:05:30 -0400, Dennis Lee Bieber wrote:
{non sequitur: I still recall my archaic C++ class with the OOAD
assignment of designing said calculator -- we never had to implement
one, just design the basic classes/methods/attributes [on 3x5 cards] for
a four-banger. I managed
On Wed, 11 Jul 2012 20:39:45 -0700, 8 Dihedral wrote:
I'll contribute my way of python programming:
def powerb(x, b): #
return x**b
Here's a shorter version:
py pow
built-in function pow
One functor is enough!
Nothing we have been discussing in this thread has been a functor,
On Wed, 11 Jul 2012 08:41:57 +0200, Daniel Fetchinson wrote:
funcs = [ lambda x: x**i for i in range( 5 ) ]
Here's another solution:
from functools import partial
funcs = [partial(lambda i, x: x**i, i) for i in range(5)]
Notice that the arguments i and x are defined in the opposite order.
On 7/11/2012 11:53 PM, Steven D'Aprano wrote:
On Wed, 11 Jul 2012 21:05:30 -0400, Dennis Lee Bieber wrote:
{non sequitur: I still recall my archaic C++ class with the OOAD
assignment of designing said calculator -- we never had to implement
one, just design the basic classes/methods/attributes
On Thursday, July 12, 2012 11:51:04 AM UTC+8, Steven D#39;Aprano wrote:
On Wed, 11 Jul 2012 20:39:45 -0700, 8 Dihedral wrote:
gt; I#39;ll contribute my way of python programming:
gt;
gt; def powerb(x, b): #
gt; return x**b
Here#39;s a shorter version:
pygt; pow
lt;built-in
On Wed, 11 Jul 2012 13:21:34 -0700 (PDT)
John Ladasky john_lada...@sbcglobal.net wrote:
Exactly. It's threads like these which remind me why I never use lambda. I
would rather give a function an explicit name and adhere to the familiar
Python syntax, despite the two extra lines of code. I
50 matches
Mail list logo