[Tutor] operator order

2013-01-31 Thread heathen

why is this:

>>> d = 2
>>> d *= 3 + 4
>>> d
14

not this:

>>> d = 2
>>> d = d * 3 + 4
>>> d
10

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] operator order

2013-01-31 Thread Nick W
because python process the expression on the right side of the assignment
first.
ie d *= 3+4 basically is the equivalent of writing (2) * (3+4).
Hope that explains it.
Nick


On Thu, Jan 31, 2013 at 10:36 AM, heathen  wrote:

> why is this:
>
> >>> d = 2
> >>> d *= 3 + 4
> >>> d
> 14
>
> not this:
>
> >>> d = 2
> >>> d = d * 3 + 4
> >>> d
> 10
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] operator order

2013-01-31 Thread Danny Yoo
On Thu, Jan 31, 2013 at 11:36 AM, heathen  wrote:
> why is this:
>
 d *= 3 + 4


The gory details about how Python understands this expression can be found in:


http://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements

Technically, "3 + 4" is the "expression list" part of the statement,
so it gets evaluated.  Then it does the multiplication of the target
(d) and the value of the expression list (7).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] operator order

2013-01-31 Thread Aurélien DESBRIÈRES
heathen  writes:

> why is this:
>
 d = 2
 d *= 3 + 4
 d
> 14

hmm ...


because d * the result of 3 + 4


> not this:
>
 d = 2
 d = d * 3 + 4
 d
> 10

and here it d multiply by 3 + 4

> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

-- 
Aurélien DESBRIÈRES
Ride free! Ride GNU.ORG
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] operator order

2013-01-31 Thread Hugo Arts
On Thu, Jan 31, 2013 at 8:20 PM, Danny Yoo  wrote:

> On Thu, Jan 31, 2013 at 11:36 AM, heathen 
> wrote:
> > why is this:
> >
>  d *= 3 + 4
>
>
> The gory details about how Python understands this expression can be found
> in:
>
>
> http://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements
>
> Technically, "3 + 4" is the "expression list" part of the statement,
> so it gets evaluated.  Then it does the multiplication of the target
> (d) and the value of the expression list (7).
>
>
The docs really hit it with that "similar but not exactly equal" phrase.
Augmented assignment is one of those things that is sort-of-but-not-really
equivalent, so it can really bite you in the ass. I recently encountered
this little gem on:

>>> a = ([], [])
>>> a[0] += [1]

Traceback (most recent call last):
  File "", line 1, in 
a[0] += [1]
TypeError: 'tuple' object does not support item assignment
>>> a
([1], [])
>>>

tuples are immutable, but lists *do* support in-place modification. And
thus we get an operation that both fails and succeeds at the same time..
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] operator order

2013-01-31 Thread Alan Gauld

On 31/01/13 18:36, heathen wrote:

why is this:

 >>> d = 2
 >>> d *= 3 + 4
 >>> d
14

not this:



 >>> d
10



Others have answered but I'll add my two cents variation...

d *= 3+4 works like
d *= X -> d = d * X

so what is X? Is it the 3 or the 3+4.
Let's put some parentheses around things to make it unambiguous:

where X=(3+4)
(d = d * (3+4))  -> 14 that all works OK

where X = 3
((d = d * 3) + 4)
which is a syntax error since assignment doesn't return
a value in Python...

So it has to be X = (3+4) to make any kind of sense.

In these cases you have to think the way the computer thinks.
How is Python going to interpret it? In the second case it can't.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Need to be able to accept Page Down or CTRL-E

2013-01-31 Thread Dave Wilder
Hello,

In a script I am writing, I would like to be able to accept the PAGE DOWN key 
as an input  as well as the arrow keys.

Is that possible w/ Python (I am using python 2.7.3 and Linux OS)?  If so, 
could someone point me to documentation on how to do that?

I have done things like this before in python.  For example, I used the 
following to send the  character.
send (chr(27)) 

However, I have not been able to find the ascii code equivalent to use for PAGE 
DOWN and the up/down arrow keys.

I'm not even sure this is even a python question.

Thanks,

Dave 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need to be able to accept Page Down or CTRL-E

2013-01-31 Thread Dave Angel

On 01/31/2013 09:43 PM, Dave Wilder wrote:

Hello,

In a script I am writing, I would like to be able to accept the PAGE DOWN key 
as an input  as well as the arrow keys.

Is that possible w/ Python (I am using python 2.7.3 and Linux OS)?  If so, 
could someone point me to documentation on how to do that?

I have done things like this before in python.  For example, I used the following to 
send the  character.
send (chr(27))

However, I have not been able to find the ascii code equivalent to use for PAGE 
DOWN and the up/down arrow keys.

I'm not even sure this is even a python question.



So how are you taking this input in?  If you're writing a terminal 
application, using raw_input(), it would be very difficult, as you'd 
probably have to modify the readline function.


Perhaps you're using some GUI.  Please tell us which one.

--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need to be able to accept Page Down or CTRL-E

2013-01-31 Thread Dave Wilder


>> On 01/31/2013 09:43 PM, Dave Wilder wrote:
>> Hello,
>>
>> In a script I am writing, I would like to be able to accept the PAGE DOWN 
>> key as an input  as well as the arrow keys.
>>
>> Is that possible w/ Python (I am using python 2.7.3 and Linux OS)?  If so, 
>> could someone point me to documentation on how to do that?
>>
>> I have done things like this before in python.  For example, I used the 
>> following to send the  character.
>> send (chr(27))
>>
>> However, I have not been able to find the ascii code equivalent to use for 
>> PAGE DOWN and the up/down arrow keys.
>>
>> I'm not even sure this is even a python question.
>>

> So how are you taking this input in?  If you're writing a terminal 
> application, using raw_input(), it would be very difficult, as you'd probably 
> have to modify the readline function.
> Perhaps you're using some GUI.  Please tell us which one.
>- DaveA

I am using a terminal application and an application called pexpect (instead of 
raw_input) where  I send something to the console and then look for a result, 
such as  or
UP/DOWN arrows.  I'm not sure if pexpect is standard Python application.  I 
thought I recalled someone on this list saying it wasn't. 

Dave

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] operator order

2013-01-31 Thread eryksun
On Thu, Jan 31, 2013 at 5:53 PM, Hugo Arts  wrote:
>
 a = ([], [])
 a[0] += [1]
> TypeError: 'tuple' object does not support item assignment
 a
> ([1], [])

Yep, storing the result fails for a read-only subscript or attribute
(e.g. a property without fset), but only after the in-place op
executes. Solution: don't do that. ;)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need to be able to accept Page Down or CTRL-E

2013-01-31 Thread Dave Angel

On 01/31/2013 10:19 PM, Dave Wilder wrote:




On 01/31/2013 09:43 PM, Dave Wilder wrote:
Hello,

In a script I am writing, I would like to be able to accept the PAGE DOWN key 
as an input  as well as the arrow keys.

Is that possible w/ Python (I am using python 2.7.3 and Linux OS)?  If so, 
could someone point me to documentation on how to do that?

I have done things like this before in python.  For example, I used the following to 
send the  character.
send (chr(27))

However, I have not been able to find the ascii code equivalent to use for PAGE 
DOWN and the up/down arrow keys.

I'm not even sure this is even a python question.




So how are you taking this input in?  If you're writing a terminal application, 
using raw_input(), it would be very difficult, as you'd probably have to modify 
the readline function.
Perhaps you're using some GUI.  Please tell us which one.
- DaveA


I am using a terminal application and an application called pexpect (instead of 
raw_input) where  I send something to the console and then look for a result, such as 
 or
UP/DOWN arrows.  I'm not sure if pexpect is standard Python application.  I 
thought I recalled someone on this list saying it wasn't.



I don't use pexpect.  But there is both a pexpect in the stdlib, and at 
least one external library of the same name.  So it may be relevant 
which one you're using.  But it's still quite confusing to me whether 
you're writing the controlled app or the controlling app.  And your 
wording is confusing me still further, by referring to pexpect as an 
application.


Maybe somebody else can make sense out of it.  Or maybe you should start 
over, using names and verbal diagrams.  Who is sending what to whom? 
Which programs are you stuck with, which ones are you writing?  If 
you're writing all of it, why would you use pexpect?




--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor