Re: Syntax question

2020-08-16 Thread Manfred Lotz
On Sun, 16 Aug 2020 06:09:17 -0500
Skip Montanaro  wrote:

> > Typing is not required by
> > Python. However, you may find the extra error-checking helpful...  
> 
> I haven't used type hints much, if at all, but my understanding is
> that the "extra error-checking" of which you speak is gotten through
> other static checkers, correct? 

Yes.

> I know the syntax was developed with
> the MyPy folks (http://mypy-lang.org/), but I'm not sure what other
> tools currently make use of it. Any pointers?
> 

I heard about the following (not tried all, though)

- mypy (Dropbox)
- pyre-check (Facebook)
- pytype (Google)
- pyright (Microsoft)


-- 
Manfred

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


Re: Syntax question

2020-08-16 Thread Skip Montanaro
> Typing is not required by
> Python. However, you may find the extra error-checking helpful...

I haven't used type hints much, if at all, but my understanding is
that the "extra error-checking" of which you speak is gotten through
other static checkers, correct? I know the syntax was developed with
the MyPy folks (http://mypy-lang.org/), but I'm not sure what other
tools currently make use of it. Any pointers?

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Syntax question

2020-08-16 Thread Manfred Lotz
On Sun, 16 Aug 2020 10:12:04 +0200
Klaus Jantzen  wrote:

> Hi,
> 
> the other day I came across the book "Classic Computer Science
> Problems in Python" by David Kopec.
> 
> The function definitions in the examples  like
> 
> =
> def fib2(n: int) -> int:
>      if n < 2:  # base case
>      return n
>      return fib2(n - 2) + fib2(n - 1)  # recursive case
> 
> 
> if __name__ == "__main__":
>      print(fib2(5))
>      print(fib2(10))
> 
> =
> 
> use a syntax that I have never seen on this list or in other
> publications.
> 

What do you mean? The 'n:int' and '-> int'? If yes, this is type
hinting.

See here: https://docs.python.org/3/library/typing.html

-- 
Manfred

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


Re: Syntax question

2020-08-16 Thread dn via Python-list

On 16/08/2020 20:12, Klaus Jantzen wrote:

Hi,

the other day I came across the book "Classic Computer Science Problems 
in Python" by David Kopec.


The function definitions in the examples  like

=
def fib2(n: int) -> int:
     if n < 2:  # base case
     return n
     return fib2(n - 2) + fib2(n - 1)  # recursive case


if __name__ == "__main__":
     print(fib2(5))
     print(fib2(10))

=

use a syntax that I have never seen on this list or in other publications.


About which line of code are you asking?

> def fib2(n: int) -> int:

Please review: Type Hints:
https://docs.python.org/3/library/typing.html

> if __name__ == "__main__":

https://docs.python.org/3/library/__main__.html
https://docs.python.org/3/reference/import.html


My questions:

Is that new?


Typing: v3.5+ (IIRC)



Is is 'recommended' to use this is the future?


Yes and no. The operative word is "Hints". Typing is not required by 
Python. However, you may find the extra error-checking helpful...



I can only see a certain advantage of using this type of function 
definition in resp. to the documentation, as it does not provide an 
automatic check of the type of the argument(s) or of the result as in Java.


There are 'pros' and 'cons'!
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Syntax question

2020-08-16 Thread Klaus Jantzen

Hi,

the other day I came across the book "Classic Computer Science Problems 
in Python" by David Kopec.


The function definitions in the examples  like

=
def fib2(n: int) -> int:
    if n < 2:  # base case
    return n
    return fib2(n - 2) + fib2(n - 1)  # recursive case


if __name__ == "__main__":
    print(fib2(5))
    print(fib2(10))

=

use a syntax that I have never seen on this list or in other publications.

My questions:

Is that new?

Is is 'recommended' to use this is the future?

I can only see a certain advantage of using this type of function 
definition in resp. to the documentation, as it does not provide an 
automatic check of the type of the argument(s) or of the result as in Java.


--

K.D.J.

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


Re: syntax question

2018-06-02 Thread Chris Angelico
On Sat, Jun 2, 2018 at 9:08 PM, Sharan Basappa  wrote:
> Can anyone please tell me what the following line in a python program does:
>
> line = lambda x: x + 3
>
> I have pasted the entire code below for reference:
>
> from scipy.optimize import fsolve
> import numpy as np
> line = lambda x: x + 3
> solution = fsolve(line, -2)
> print solution

That creates a function. It's basically equivalent to:

def line(x):
return x + 3

Normally you won't take a lambda function and immediately assign it to
a name. The point of a lambda function is that, unlike a def function,
it can be used directly in a function call - so you might do something
like this:

solution = fsolve(lambda x: x + 3, -2)

which will have the same effect as the code you showed. If you want it
to have a name, like this, just use 'def'.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: syntax question

2018-06-02 Thread Timo Furrer
>From the Python documentation at
https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions

> Small anonymous functions can be created with the lambda
 keyword. This
function returns the sum of its two arguments: lambda a, b: a+b. Lambda
functions can be used wherever function objects are required. They are
syntactically restricted to a single expression. Semantically, they are
just syntactic sugar for a normal function definition.

Thus, in your case the lambda is equivalent to:

def line(x):
return x + 3

Cheers, Timo

On Sat, Jun 2, 2018 at 1:12 PM Sharan Basappa 
wrote:

> Can anyone please tell me what the following line in a python program does:
>
> line = lambda x: x + 3
>
> I have pasted the entire code below for reference:
>
> from scipy.optimize import fsolve
> import numpy as np
> line = lambda x: x + 3
> solution = fsolve(line, -2)
> print solution
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 
*Timo Furrer*

https://github.com/timofurrer
tuxt...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


syntax question

2018-06-02 Thread Sharan Basappa
Can anyone please tell me what the following line in a python program does:

line = lambda x: x + 3

I have pasted the entire code below for reference:

from scipy.optimize import fsolve
import numpy as np
line = lambda x: x + 3
solution = fsolve(line, -2)
print solution
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: General computer language, syntax question.

2016-03-01 Thread Steven D'Aprano
On Tue, 1 Mar 2016 12:56 am, jonas.thornv...@gmail.com wrote:

> I mean for for example Javascript

Jonas, this is a Python forum. If we thought Javascript was a beautiful and
well-designed language, we're be on a Javascript forum, complaining about
Python.


> if (typeof array[index] !== 'undefined' && array[index] !== null) {
> or this one
> if (array[index] != null) {
> 
> I mean how do they come up with such convoluted syntax, do they pull it
> out of ass? 

Probably. I don't know Javascript very well. It is possible that your
statements about Javascript are as misinformed as your statements about
Python.


In Python, you certainly can do membership testing of a list:

py> alist = ["ab", "cd", "ef", "gh"]
py> "ef" in alist
True
py> "xy" in alist
False


and you can also test for empty lists just as easily:

py> if alist:
... print("not empty")
... else:
... print("empty")
...
not empty

py> blist = []
py> if blist:
... print("not empty")
... else:
... print("empty")
...
empty




-- 
Steven

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


Re: General computer language, syntax question.

2016-02-29 Thread Joel Goldstick
On Mon, Feb 29, 2016 at 9:39 AM, Mark Lawrence 
wrote:

> On 29/02/2016 13:45, jonas.thornv...@gmail.com wrote:
>
>> I have a problem programming uniform networks, "x nodes with y links"
>> that turn out to be really hairy to solve for me and i feel i really lack
>> the programming features
>>
>> "Actually i program in javascript" but the problem seem general for all
>> programming languages including Pyhton.
>>
>> For a beautiful solution it would require "If in list/array return
>> boolean" "If not in list/array return boolean".
>>
>> But there is no such feature Python nor Javascript, so instead i set
>> boolean value "inlist" to false and loop thru, to see if it is already in
>> list. If not it is added to list.
>>
>> So if the current node i generate links for is x and i try to generate a
>> link to  node z, and z's links exhausted i will add it to exhausted list.
>>
>> And if there is node-x exhausted entries in list, well then script should
>> break because it will lock into a cycle. The cyclic lockup is due to only a
>> subset of the networks on the form (links*deep)+1=nodes is possible.
>>
>> So what i need is to know howto write "if list/array ***empty*** do
>> {something}"
>>
>> I sure know howto check if an array have 1 element 2,3,4 but how do you
>> check for empty.
>>
>> I think it is crazy that you can not do general comparissons of the type
>> If in list/array
>> If not in list/array
>>
>> But it is even more crazy that you can not check if list/array is empty,
>> that is just madness.
>>
>>
> Please see
> https://docs.python.org/3/library/stdtypes.html#truth-value-testing
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>>> l = []
>>> l
[]
>>> if l:
...   print 'not empty'
...
>>>


-- 
Joel Goldstick
http://joelgoldstick.com/ 
http://cc-baseballstats.info/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: General computer language, syntax question.

2016-02-29 Thread Ian Kelly
On Feb 29, 2016 7:11 AM,  wrote:
>
> Sorry but would not if (array==empty) suffice and be alot clearer?

In Python, you can just do "if len(array) == 0" or "if not array".

In JavaScript you have "if (array.length === 0)". Is there some problem
with that?

I would prefer this over your suggestion since it doesn't require some
variable named "empty" to have a sensible value set.

On the container test, as others pointed out, Python has the "in" operator.
JavaScript has a draft Array.prototype.includes method, but it doesn't have
broad cross-browser support yet.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: General computer language, syntax question.

2016-02-29 Thread Mark Lawrence

On 29/02/2016 13:45, jonas.thornv...@gmail.com wrote:

I have a problem programming uniform networks, "x nodes with y links" that turn 
out to be really hairy to solve for me and i feel i really lack the programming features

"Actually i program in javascript" but the problem seem general for all 
programming languages including Pyhton.

For a beautiful solution it would require "If in list/array return boolean" "If not 
in list/array return boolean".

But there is no such feature Python nor Javascript, so instead i set boolean value 
"inlist" to false and loop thru, to see if it is already in list. If not it is 
added to list.

So if the current node i generate links for is x and i try to generate a link 
to  node z, and z's links exhausted i will add it to exhausted list.

And if there is node-x exhausted entries in list, well then script should break 
because it will lock into a cycle. The cyclic lockup is due to only a subset of 
the networks on the form (links*deep)+1=nodes is possible.

So what i need is to know howto write "if list/array ***empty*** do {something}"

I sure know howto check if an array have 1 element 2,3,4 but how do you check 
for empty.

I think it is crazy that you can not do general comparissons of the type
If in list/array
If not in list/array

But it is even more crazy that you can not check if list/array is empty, that 
is just madness.



Please see 
https://docs.python.org/3/library/stdtypes.html#truth-value-testing


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: General computer language, syntax question.

2016-02-29 Thread Joel Goldstick
On Mon, Feb 29, 2016 at 9:05 AM,  wrote:

> Den måndag 29 februari 2016 kl. 14:57:04 UTC+1 skrev jonas.t...@gmail.com:
> > Den måndag 29 februari 2016 kl. 14:45:37 UTC+1 skrev
> jonas.t...@gmail.com:
> > > I have a problem programming uniform networks, "x nodes with y links"
> that turn out to be really hairy to solve for me and i feel i really lack
> the programming features
> > >
> > > "Actually i program in javascript" but the problem seem general for
> all programming languages including Pyhton.
> > >
> > > For a beautiful solution it would require "If in list/array return
> boolean" "If not in list/array return boolean".
> > >
> > > But there is no such feature Python nor Javascript, so instead i set
> boolean value "inlist" to false and loop thru, to see if it is already in
> list. If not it is added to list.
> > >
> > > So if the current node i generate links for is x and i try to generate
> a link to  node z, and z's links exhausted i will add it to exhausted list.
> > >
> > > And if there is node-x exhausted entries in list, well then script
> should break because it will lock into a cycle. The cyclic lockup is due to
> only a subset of the networks on the form (links*deep)+1=nodes is possible.
> > >
> > > So what i need is to know howto write "if list/array ***empty*** do
> {something}"
> > >
> > > I sure know howto check if an array have 1 element 2,3,4 but how do
> you check for empty.
> > >
> > > I think it is crazy that you can not do general comparissons of the
> type
> > > If in list/array
> > > If not in list/array
> > >
> > > But it is even more crazy that you can not check if list/array is
> empty, that is just madness.
> >
> > I mean for for example Javascript
> > if (typeof array[index] !== 'undefined' && array[index] !== null) {
> > or this one
> > if (array[index] != null) {
> >
> > I mean how do they come up with such convoluted syntax, do they pull it
> out of ass? Well one can always say it is needed because undefined not
> equal to null.
> >
> > But would not if (array[index]==empty) suffice and be alot clearer?
>
> Sorry but would not if (array==empty) suffice and be alot clearer?
> --
> https://mail.python.org/mailman/listinfo/python-list
>

You might have better luck on a javascript mailing list.  Not sure crazy,
madness, ass are descriptive to your problems

-- 
Joel Goldstick
http://joelgoldstick.com/ 
http://cc-baseballstats.info/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: General computer language, syntax question.

2016-02-29 Thread jonas . thornvall
Den måndag 29 februari 2016 kl. 14:57:04 UTC+1 skrev jonas.t...@gmail.com:
> Den måndag 29 februari 2016 kl. 14:45:37 UTC+1 skrev jonas.t...@gmail.com:
> > I have a problem programming uniform networks, "x nodes with y links" that 
> > turn out to be really hairy to solve for me and i feel i really lack the 
> > programming features 
> > 
> > "Actually i program in javascript" but the problem seem general for all 
> > programming languages including Pyhton.
> > 
> > For a beautiful solution it would require "If in list/array return boolean" 
> > "If not in list/array return boolean". 
> > 
> > But there is no such feature Python nor Javascript, so instead i set 
> > boolean value "inlist" to false and loop thru, to see if it is already in 
> > list. If not it is added to list. 
> > 
> > So if the current node i generate links for is x and i try to generate a 
> > link to  node z, and z's links exhausted i will add it to exhausted list.
> > 
> > And if there is node-x exhausted entries in list, well then script should 
> > break because it will lock into a cycle. The cyclic lockup is due to only a 
> > subset of the networks on the form (links*deep)+1=nodes is possible.
> > 
> > So what i need is to know howto write "if list/array ***empty*** do 
> > {something}"
> > 
> > I sure know howto check if an array have 1 element 2,3,4 but how do you 
> > check for empty.
> > 
> > I think it is crazy that you can not do general comparissons of the type 
> > If in list/array
> > If not in list/array
> > 
> > But it is even more crazy that you can not check if list/array is empty, 
> > that is just madness.
> 
> I mean for for example Javascript
> if (typeof array[index] !== 'undefined' && array[index] !== null) {
> or this one
> if (array[index] != null) {
> 
> I mean how do they come up with such convoluted syntax, do they pull it out 
> of ass? Well one can always say it is needed because undefined not equal to 
> null.
> 
> But would not if (array[index]==empty) suffice and be alot clearer?

Sorry but would not if (array==empty) suffice and be alot clearer?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: General computer language, syntax question.

2016-02-29 Thread MRAB

On 2016-02-29 13:45, jonas.thornv...@gmail.com wrote:

I have a problem programming uniform networks, "x nodes with y links" that turn 
out to be really hairy to solve for me and i feel i really lack the programming features

"Actually i program in javascript" but the problem seem general for all 
programming languages including Pyhton.

For a beautiful solution it would require "If in list/array return boolean" "If not 
in list/array return boolean".

But there is no such feature Python nor Javascript, so instead i set boolean value 
"inlist" to false and loop thru, to see if it is already in list. If not it is 
added to list.

So if the current node i generate links for is x and i try to generate a link 
to  node z, and z's links exhausted i will add it to exhausted list.

And if there is node-x exhausted entries in list, well then script should break 
because it will lock into a cycle. The cyclic lockup is due to only a subset of 
the networks on the form (links*deep)+1=nodes is possible.

So what i need is to know howto write "if list/array ***empty*** do {something}"

I sure know howto check if an array have 1 element 2,3,4 but how do you check 
for empty.

I think it is crazy that you can not do general comparissons of the type
If in list/array
If not in list/array

But it is even more crazy that you can not check if list/array is empty, that 
is just madness.


Does this help?

>>> items = ['a', 'b', 'c', 'd', 'e']
>>> 'c' in items
True
>>> 'f' in items
False
>>> len(items)
5
>>> len([])
0

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


Re: General computer language, syntax question.

2016-02-29 Thread Joel Goldstick
On Mon, Feb 29, 2016 at 8:45 AM,  wrote:

> I have a problem programming uniform networks, "x nodes with y links" that
> turn out to be really hairy to solve for me and i feel i really lack the
> programming features
>
> "Actually i program in javascript" but the problem seem general for all
> programming languages including Pyhton.
>
> For a beautiful solution it would require "If in list/array return
> boolean" "If not in list/array return boolean".
>
> But there is no such feature Python nor Javascript, so instead i set
> boolean value "inlist" to false and loop thru, to see if it is already in
> list. If not it is added to list.
>
> So if the current node i generate links for is x and i try to generate a
> link to  node z, and z's links exhausted i will add it to exhausted list.
>
> And if there is node-x exhausted entries in list, well then script should
> break because it will lock into a cycle. The cyclic lockup is due to only a
> subset of the networks on the form (links*deep)+1=nodes is possible.
>
> So what i need is to know howto write "if list/array ***empty*** do
> {something}"
>
> I sure know howto check if an array have 1 element 2,3,4 but how do you
> check for empty.
>
> I think it is crazy that you can not do general comparissons of the type
> If in list/array
> If not in list/array
>
> But it is even more crazy that you can not check if list/array is empty,
> that is just madness.
> --
> https://mail.python.org/mailman/listinfo/python-list
>

>>> l = [1,2,3,4]
>>> if 1 in l:
...   print 1
...
1
>>> if 6 in l:
...   print 1
...
>>>
>>> l = []
>>> if len(l) == 0:
...   print 'empty'
...
empty


-- 
Joel Goldstick
http://joelgoldstick.com/ 
http://cc-baseballstats.info/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: General computer language, syntax question.

2016-02-29 Thread jonas . thornvall
Den måndag 29 februari 2016 kl. 14:45:37 UTC+1 skrev jonas.t...@gmail.com:
> I have a problem programming uniform networks, "x nodes with y links" that 
> turn out to be really hairy to solve for me and i feel i really lack the 
> programming features 
> 
> "Actually i program in javascript" but the problem seem general for all 
> programming languages including Pyhton.
> 
> For a beautiful solution it would require "If in list/array return boolean" 
> "If not in list/array return boolean". 
> 
> But there is no such feature Python nor Javascript, so instead i set boolean 
> value "inlist" to false and loop thru, to see if it is already in list. If 
> not it is added to list. 
> 
> So if the current node i generate links for is x and i try to generate a link 
> to  node z, and z's links exhausted i will add it to exhausted list.
> 
> And if there is node-x exhausted entries in list, well then script should 
> break because it will lock into a cycle. The cyclic lockup is due to only a 
> subset of the networks on the form (links*deep)+1=nodes is possible.
> 
> So what i need is to know howto write "if list/array ***empty*** do 
> {something}"
> 
> I sure know howto check if an array have 1 element 2,3,4 but how do you check 
> for empty.
> 
> I think it is crazy that you can not do general comparissons of the type 
> If in list/array
> If not in list/array
> 
> But it is even more crazy that you can not check if list/array is empty, that 
> is just madness.

I mean for for example Javascript
if (typeof array[index] !== 'undefined' && array[index] !== null) {
or this one
if (array[index] != null) {

I mean how do they come up with such convoluted syntax, do they pull it out of 
ass? Well one can always say it is needed because undefined not equal to null.

But would not if (array[index]==empty) suffice and be alot clearer?



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


General computer language, syntax question.

2016-02-29 Thread jonas . thornvall
I have a problem programming uniform networks, "x nodes with y links" that turn 
out to be really hairy to solve for me and i feel i really lack the programming 
features 

"Actually i program in javascript" but the problem seem general for all 
programming languages including Pyhton.

For a beautiful solution it would require "If in list/array return boolean" "If 
not in list/array return boolean". 

But there is no such feature Python nor Javascript, so instead i set boolean 
value "inlist" to false and loop thru, to see if it is already in list. If not 
it is added to list. 

So if the current node i generate links for is x and i try to generate a link 
to  node z, and z's links exhausted i will add it to exhausted list.

And if there is node-x exhausted entries in list, well then script should break 
because it will lock into a cycle. The cyclic lockup is due to only a subset of 
the networks on the form (links*deep)+1=nodes is possible.

So what i need is to know howto write "if list/array ***empty*** do {something}"

I sure know howto check if an array have 1 element 2,3,4 but how do you check 
for empty.

I think it is crazy that you can not do general comparissons of the type 
If in list/array
If not in list/array

But it is even more crazy that you can not check if list/array is empty, that 
is just madness.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A syntax question

2014-11-11 Thread alister
On Mon, 10 Nov 2014 19:53:56 -0500, Dennis Lee Bieber wrote:

> On Mon, 10 Nov 2014 14:56:11 GMT, alister
>  declaimed the following:
> 
>>On Mon, 10 Nov 2014 14:54:55 +, alister wrote:
>>
>>> On Mon, 10 Nov 2014 14:44:53 +, Grant Edwards wrote:
>>> 
 On 2014-11-10, David Palao  wrote:
 
>> My crystal ball is currently in for repair and is not expected back
>> in the foreseeable future.
>
> Without a crystal ball, this prediction might be not well founded.
 
 That isn't a prediction.  It's an explicit statement of no
 prediction.
 He said that it is "not expected back" rather than "expected not to
 be back".  They're two different things.  The former asserts a _lack_
 of expection/prediction.  The latter asserts an
 expectation/prediction.
>>> 
>>> It was only a a joke maybe it was a bit to subtle
>>
>>I didn't expect the Spanish inquisition (Damn wish Id though of that
>>before sending the last post)
> 
>   They're now the Congregation of the Doctrine of the Faith -- 
formerly
> led by the person who became Pope Benedict.

but "I didnt expect the congregation of the Doctrine of the Faith" is not 
on topic.




-- 
Never trust an automatic pistol or a D.A.'s deal.
-- John Dillinger
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A syntax question

2014-11-10 Thread Tim Chase
On 2014-11-10 20:08, Mark Lawrence wrote:
> On 10/11/2014 11:31, David Palao wrote:
> >> My crystal ball is currently in for repair and is not expected
> >> back in the foreseeable future.
> >
> > Without a crystal ball, this prediction might be not well founded.
> >
> 
> Especially in the future when sombody asks "Who the hell was he
> replying to?".

That might be a concern if the mail.python.org archive failed,
and all usenet archives fell offline.  Since the threading wasn't
broken, it's a simple matter of looking at the previous message in the
thread, as referenced in the appropriate headers:

In-Reply-To: 
References:  


where that particular message can be found.  Any competent mailer or
news client should handle threading without the user even thinking
about it.

It might also be more of a concern if there was actual
question/answer content rather than just a little throw-away humor.

-tkc



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


Re: A syntax question

2014-11-10 Thread Mark Lawrence

On 10/11/2014 11:31, David Palao wrote:

My crystal ball is currently in for repair and is not expected back in
the foreseeable future.


Without a crystal ball, this prediction might be not well founded.



Especially in the future when sombody asks "Who the hell was he replying 
to?".


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: A syntax question

2014-11-10 Thread Roy Smith
In article ,
 Chris Angelico  wrote:

> On Tue, Nov 11, 2014 at 3:11 AM, Grant Edwards  
> wrote:
> > I know, but in c.l.p, even jokes get nicely pednatic answers.
> 
> And in c.l.p, odd jokes get even more pedantic spelling corrections.
> 
> ChrisA

a
n
d

i
m
a
g
i
n
a
r
y

j
o
k
e
s

g
e
t

r
o
t
a
t
e
d
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A syntax question

2014-11-10 Thread Chris Angelico
On Tue, Nov 11, 2014 at 3:11 AM, Grant Edwards  wrote:
> I know, but in c.l.p, even jokes get nicely pednatic answers.

And in c.l.p, odd jokes get even more pedantic spelling corrections.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A syntax question

2014-11-10 Thread Grant Edwards
On 2014-11-10, alister  wrote:
> On Mon, 10 Nov 2014 14:44:53 +, Grant Edwards wrote:
>
>> On 2014-11-10, David Palao  wrote:
>> 
 My crystal ball is currently in for repair and is not expected back in
 the foreseeable future.
>>>
>>> Without a crystal ball, this prediction might be not well founded.
>> 
>> That isn't a prediction.  It's an explicit statement of no prediction.
>> He said that it is "not expected back" rather than "expected not to be
>> back".  They're two different things.  The former asserts a _lack_ of
>> expection/prediction.  The latter asserts an expectation/prediction.
>
> It was only a a joke maybe it was a bit to subtle 

I know, but in c.l.p, even jokes get nicely pednatic answers.

;)

-- 
Grant Edwards   grant.b.edwardsYow! The FALAFEL SANDWICH
  at   lands on my HEAD and I
  gmail.combecome a VEGETARIAN ...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A syntax question

2014-11-10 Thread Joel Goldstick
On Mon, Nov 10, 2014 at 9:54 AM, Chris Angelico  wrote:
> On Tue, Nov 11, 2014 at 1:35 AM, Joel Goldstick
>  wrote:
>> Your problem is that count is not local.  You are reading count from
>> an outer scope.  When you try to increment count in your function, it
>> can't because it doesn't exist.
>> Don't use globals.
>
> False analysis, I'm afraid. The problem is that the assignment will,
> in the absence of a "global" declaration, cause the name "count" to
> indicate a local variable - so it won't ever be read from outer scope.
> However, the OP's issue is better solved by sharing tracebacks than by
> us peering into crystal balls; mine's showing a very clear image at
> the moment, but it might well be incorrect.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list

Interesting.  Thanks for pointing that out


-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A syntax question

2014-11-10 Thread Peter Otten
Joel Goldstick wrote:

> On Mon, Nov 10, 2014 at 6:39 AM, Wolfgang Maier
>  wrote:
>> You may want to read:
>>
>> https://docs.python.org/3/faq/programming.html?highlight=global#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value
>>
>> from the Python docs Programming FAQ section.
>> It explains your problem pretty well.
>>
>> As others have hinted at, always provide concrete Python error messages
>> and tracebacks instead of vague descriptions.
>>
>> Best,
>> Wolfgang
>>
>>
>>
>> On 11/10/2014 12:07 PM, Mok-Kong Shen wrote:
>>>
>>>
>>> I don't understand the following phenomenon. Could someone kindly
>>> explain it? Thanks in advance.
>>>
>>> M. K. Shen
>>>
>>> -
>>>
>>> count=5
>>>
>>> def test():
>>>print(count)
>>>if count==5:
>>>  count+=0  ### Error message if this line is active, otherwise ok.
>>>  print(count)
>>>return
>>>
>>> test()
>>
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
> 
> Your problem is that count is not local.  You are reading count from
> an outer scope.  When you try to increment count in your function, it
> can't because it doesn't exist.
> Don't use globals.

That's what most would expect, but the error is already triggered by the 
first

print(count)

Python decides at compile-time that count is a local variable if there is an 
assignment ("name binding") to count anywhere in the function's scope --  
even if the corresponding code will never be executed:

>>> x = 42
>>> def test():
... print(x)
... if 0: x = 42
... 
>>> test()
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in test
UnboundLocalError: local variable 'x' referenced before assignment

This is different from the class body where the global namespace is tried 
when a lookup in the local namespace fails:

>>> x = 42
>>> class A:
... print(x)
... x += 1
... 
42
>>> x
42
>>> A.x
43

Historical ;) note: In Python 2 you could trigger a similar behaviour with 
exec:

>>> def f(a):
... if a: exec "x = 42"
... print x
... 
>>> x = "global"
>>> f(True)
42
>>> f(False)
global



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


Re: A syntax question

2014-11-10 Thread alister
On Mon, 10 Nov 2014 14:54:55 +, alister wrote:

> On Mon, 10 Nov 2014 14:44:53 +, Grant Edwards wrote:
> 
>> On 2014-11-10, David Palao  wrote:
>> 
 My crystal ball is currently in for repair and is not expected back
 in the foreseeable future.
>>>
>>> Without a crystal ball, this prediction might be not well founded.
>> 
>> That isn't a prediction.  It's an explicit statement of no prediction.
>> He said that it is "not expected back" rather than "expected not to be
>> back".  They're two different things.  The former asserts a _lack_ of
>> expection/prediction.  The latter asserts an expectation/prediction.
> 
> It was only a a joke maybe it was a bit to subtle

I didn't expect the Spanish inquisition (Damn wish Id though of that 
before sending the last post)



-- 
One picture is worth more than ten thousand words.
-- Chinese proverb
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A syntax question

2014-11-10 Thread alister
On Mon, 10 Nov 2014 14:44:53 +, Grant Edwards wrote:

> On 2014-11-10, David Palao  wrote:
> 
>>> My crystal ball is currently in for repair and is not expected back in
>>> the foreseeable future.
>>
>> Without a crystal ball, this prediction might be not well founded.
> 
> That isn't a prediction.  It's an explicit statement of no prediction.
> He said that it is "not expected back" rather than "expected not to be
> back".  They're two different things.  The former asserts a _lack_ of
> expection/prediction.  The latter asserts an expectation/prediction.

It was only a a joke maybe it was a bit to subtle   



-- 
A light wife doth make a heavy husband.
-- Wm. Shakespeare, "The Merchant of Venice"
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A syntax question

2014-11-10 Thread Chris Angelico
On Tue, Nov 11, 2014 at 1:35 AM, Joel Goldstick
 wrote:
> Your problem is that count is not local.  You are reading count from
> an outer scope.  When you try to increment count in your function, it
> can't because it doesn't exist.
> Don't use globals.

False analysis, I'm afraid. The problem is that the assignment will,
in the absence of a "global" declaration, cause the name "count" to
indicate a local variable - so it won't ever be read from outer scope.
However, the OP's issue is better solved by sharing tracebacks than by
us peering into crystal balls; mine's showing a very clear image at
the moment, but it might well be incorrect.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A syntax question

2014-11-10 Thread Grant Edwards
On 2014-11-10, David Palao  wrote:

>> My crystal ball is currently in for repair and is not expected back
>> in the foreseeable future.
>
> Without a crystal ball, this prediction might be not well founded.

That isn't a prediction.  It's an explicit statement of no prediction.
He said that it is "not expected back" rather than "expected not to be
back".  They're two different things.  The former asserts a _lack_ of
expection/prediction.  The latter asserts an expectation/prediction.

-- 
Grant Edwards   grant.b.edwardsYow! Am I in Milwaukee?
  at   
  gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A syntax question

2014-11-10 Thread Joel Goldstick
On Mon, Nov 10, 2014 at 6:39 AM, Wolfgang Maier
 wrote:
> You may want to read:
>
> https://docs.python.org/3/faq/programming.html?highlight=global#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value
>
> from the Python docs Programming FAQ section.
> It explains your problem pretty well.
>
> As others have hinted at, always provide concrete Python error messages and
> tracebacks instead of vague descriptions.
>
> Best,
> Wolfgang
>
>
>
> On 11/10/2014 12:07 PM, Mok-Kong Shen wrote:
>>
>>
>> I don't understand the following phenomenon. Could someone kindly
>> explain it? Thanks in advance.
>>
>> M. K. Shen
>>
>> -
>>
>> count=5
>>
>> def test():
>>print(count)
>>if count==5:
>>  count+=0  ### Error message if this line is active, otherwise ok.
>>  print(count)
>>return
>>
>> test()
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list

Your problem is that count is not local.  You are reading count from
an outer scope.  When you try to increment count in your function, it
can't because it doesn't exist.
Don't use globals.

-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A syntax question

2014-11-10 Thread Wolfgang Maier

You may want to read:

https://docs.python.org/3/faq/programming.html?highlight=global#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value

from the Python docs Programming FAQ section.
It explains your problem pretty well.

As others have hinted at, always provide concrete Python error messages 
and tracebacks instead of vague descriptions.


Best,
Wolfgang


On 11/10/2014 12:07 PM, Mok-Kong Shen wrote:


I don't understand the following phenomenon. Could someone kindly
explain it? Thanks in advance.

M. K. Shen

-

count=5

def test():
   print(count)
   if count==5:
 count+=0  ### Error message if this line is active, otherwise ok.
 print(count)
   return

test()


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


Re: A syntax question

2014-11-10 Thread David Palao
> My crystal ball is currently in for repair and is not expected back in
> the foreseeable future.

Without a crystal ball, this prediction might be not well founded.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A syntax question

2014-11-10 Thread alister
On Mon, 10 Nov 2014 12:07:58 +0100, Mok-Kong Shen wrote:

> I don't understand the following phenomenon. Could someone kindly
> explain it? Thanks in advance.
> 
> M. K. Shen
> 
> -
> 
> count=5
> 
> def test():
>print(count)
>if count==5:
>  count+=0  ### Error message if this line is active, otherwise ok.
>  print(count)
>return
> 
> test()
My crystal ball is currently in for repair and is not expected back in 
the foreseeable future.

What result are you getting that you don't understand & what do you 
expect?
What is the error message you get?
Post the full traceback



-- 
The POP server is out of Coke
-- 
https://mail.python.org/mailman/listinfo/python-list


A syntax question

2014-11-10 Thread Mok-Kong Shen


I don't understand the following phenomenon. Could someone kindly 
explain it? Thanks in advance.


M. K. Shen

-

count=5

def test():
  print(count)
  if count==5:
count+=0  ### Error message if this line is active, otherwise ok.
print(count)
  return

test()
--
https://mail.python.org/mailman/listinfo/python-list


Re: except clause syntax question

2012-01-31 Thread Mel Wilson
Chris Angelico wrote:

> On Wed, Feb 1, 2012 at 9:03 AM, Duncan Booth
>  wrote:
>> Abitrarily nested tuples of exceptions cannot contain loops so the code
>> simply needs to walk through the tuples until it finds a match.
> 
> Is this absolutely guaranteed? The C API for CPython provides:
> (Py2) http://docs.python.org/c-api/tuple.html#PyTuple_SetItem
> (Py3) http://docs.python.org/dev/c-api/tuple.html#PyTuple_SetItem
> 
> which doesn't have massive warnings on it saying "USE THIS ONLY TO
> INITIALIZE A TUPLE" (compare, for instance, _PyTuple_Resize which does
> carry a similar warning). Is the assumption then that we're all
> adults, and that mutating a tuple is like passing a null pointer to an
> API function (aka "loaded gun in proximity to foot")?

Unfortunately, I can't remember the details now, but I once set out to 
create a recursive tuple by using the C API, and it turned out then that the 
C API went to some lengths to prevent anyone being able to do that.  I did 
finally do it in some peculiar way, but it wasn't simple.  The c.l.python 
archives might still have the post where I described it.

Mel.

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


Re: except clause syntax question

2012-01-31 Thread Chris Angelico
On Wed, Feb 1, 2012 at 12:12 PM, Ian Kelly  wrote:
> Incidentally, I *think* that any correctly written C code attempting
> to nest a tuple inside itself would make the reference count of the
> tuple be at least 2 at the time of the call, and so it would fail.

Good, nice that that's certain :)

Might be worth moving the "[b]ecause tuples are supposed to be
immutable" warning up to the top of the page then, since the bulk of
it applies to all those functions and not just resize.

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


Re: except clause syntax question

2012-01-31 Thread Ian Kelly
On Tue, Jan 31, 2012 at 6:09 PM, Ian Kelly  wrote:
> On Tue, Jan 31, 2012 at 5:53 PM, Chris Angelico  wrote:
>> On Wed, Feb 1, 2012 at 9:03 AM, Duncan Booth
>>  wrote:
>>> Abitrarily nested tuples of exceptions cannot contain loops so the code
>>> simply needs to walk through the tuples until it finds a match.
>>
>> Is this absolutely guaranteed? The C API for CPython provides:
>> (Py2) http://docs.python.org/c-api/tuple.html#PyTuple_SetItem
>> (Py3) http://docs.python.org/dev/c-api/tuple.html#PyTuple_SetItem
>>
>> which doesn't have massive warnings on it saying "USE THIS ONLY TO
>> INITIALIZE A TUPLE" (compare, for instance, _PyTuple_Resize which does
>> carry a similar warning). Is the assumption then that we're all
>> adults, and that mutating a tuple is like passing a null pointer to an
>> API function (aka "loaded gun in proximity to foot")?
>
> I don't know why the docs are written the way that they are, but if
> you check the code, you can see that PyTuple_SetItem will raise a
> SystemError if the reference count is anything other than 1.  So I
> think that it is only meant to be used with similar caution and
> restraint.

Incidentally, I *think* that any correctly written C code attempting
to nest a tuple inside itself would make the reference count of the
tuple be at least 2 at the time of the call, and so it would fail.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: except clause syntax question

2012-01-31 Thread Ian Kelly
On Tue, Jan 31, 2012 at 5:53 PM, Chris Angelico  wrote:
> On Wed, Feb 1, 2012 at 9:03 AM, Duncan Booth
>  wrote:
>> Abitrarily nested tuples of exceptions cannot contain loops so the code
>> simply needs to walk through the tuples until it finds a match.
>
> Is this absolutely guaranteed? The C API for CPython provides:
> (Py2) http://docs.python.org/c-api/tuple.html#PyTuple_SetItem
> (Py3) http://docs.python.org/dev/c-api/tuple.html#PyTuple_SetItem
>
> which doesn't have massive warnings on it saying "USE THIS ONLY TO
> INITIALIZE A TUPLE" (compare, for instance, _PyTuple_Resize which does
> carry a similar warning). Is the assumption then that we're all
> adults, and that mutating a tuple is like passing a null pointer to an
> API function (aka "loaded gun in proximity to foot")?

I don't know why the docs are written the way that they are, but if
you check the code, you can see that PyTuple_SetItem will raise a
SystemError if the reference count is anything other than 1.  So I
think that it is only meant to be used with similar caution and
restraint.

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


Re: except clause syntax question

2012-01-31 Thread Chris Angelico
On Wed, Feb 1, 2012 at 9:03 AM, Duncan Booth
 wrote:
> Abitrarily nested tuples of exceptions cannot contain loops so the code
> simply needs to walk through the tuples until it finds a match.

Is this absolutely guaranteed? The C API for CPython provides:
(Py2) http://docs.python.org/c-api/tuple.html#PyTuple_SetItem
(Py3) http://docs.python.org/dev/c-api/tuple.html#PyTuple_SetItem

which doesn't have massive warnings on it saying "USE THIS ONLY TO
INITIALIZE A TUPLE" (compare, for instance, _PyTuple_Resize which does
carry a similar warning). Is the assumption then that we're all
adults, and that mutating a tuple is like passing a null pointer to an
API function (aka "loaded gun in proximity to foot")?

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


Re: except clause syntax question

2012-01-31 Thread Charles Yeomans

On Jan 31, 2012, at 7:12 PM, Terry Reedy wrote:

> On 1/31/2012 8:57 AM, Charles Yeomans wrote:
> 
>> In any case, though I appreciate your attempt at a post hoc justification,
> > I was hoping for a positive explanation.
> 
> I think the best you are going to get is that Python somewhat consistently*, 
> for both practical and historical reasons#, uses tuples when the syntax 
> allows an object or collection of objects.
> 
> * except, isinstance, isubclass, ''%x, perhaps other places.
> 
> In the last case, that creates a problem when one wants to interpolate a 
> tuple as an object rather than having it viewed as a container of several 
> objects to be interpolated. That was on
> 
> # Python once treated tuples as different from lists in ways that is not true 
> now. (Read the 1.5 docs if really interested.)
> 


I'll do that.  Thanks.


Charles Yeomans

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


Re: except clause syntax question

2012-01-31 Thread Terry Reedy

On 1/31/2012 8:57 AM, Charles Yeomans wrote:


In any case, though I appreciate your attempt at a post hoc justification,

> I was hoping for a positive explanation.

I think the best you are going to get is that Python somewhat 
consistently*, for both practical and historical reasons#, uses tuples 
when the syntax allows an object or collection of objects.


* except, isinstance, isubclass, ''%x, perhaps other places.

In the last case, that creates a problem when one wants to interpolate a 
tuple as an object rather than having it viewed as a container of 
several objects to be interpolated. That was on


# Python once treated tuples as different from lists in ways that is not 
true now. (Read the 1.5 docs if really interested.)


--
Terry Jan Reedy

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


Re: except clause syntax question

2012-01-31 Thread Duncan Booth
Charles Yeomans  wrote:

> To catch more than one exception type in an except block, one writes
> 
> except (A, B, C) as e:
> 
> I'm wondering why it was decided to match tuples, but not lists:
> 
> except [A, B, C] as e:
> 
> The latter makes more sense semantically to me -- "catch all exception
> types in a list" as opposed to "catch this single thing composed of
> three exception types". 
> 
It may not be the only reason but the code would have to be slower and much 
more complex to handle lists.

If you wanted you can write:

   except ((A,), ((B,), C)) as e:

or other such complicated expression with nested tuples. If lists were 
allowed in a similarly nested structure there would be a danger that you 
could pass in a recursive list structure so the code would have to detect 
and avoid infinite loops.

exceptions = [A, B, C]
exceptions[1:1] = exceptions,
...
except exceptions as e: # argh!

Abitrarily nested tuples of exceptions cannot contain loops so the code 
simply needs to walk through the tuples until it finds a match.

-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: except clause syntax question

2012-01-31 Thread Charles Yeomans

On Jan 31, 2012, at 8:24 AM, Mel Wilson wrote:

> Charles Yeomans wrote:
> 
>> To catch more than one exception type in an except block, one writes
>> 
>> except (A, B, C) as e:
>> 
>> I'm wondering why it was decided to match tuples, but not lists:
>> 
>> except [A, B, C] as e:
>> 
>> The latter makes more sense semantically to me -- "catch all exception
>> types in a list" as opposed to "catch this single thing composed of three
>> exception types".
> 
> On reflection, it seems to hint at a style that Python extensions were made 
> in.  (IIRC) the first operand in an `except` statement was originally just 
> an arbitrary marker to identify the exception.  Unique string values were 
> customary, although the Python library defined things with standard 
> exception names.  Using a string means that general exceptions weren't to be 
> collected in general sequences; `except "Serious Error"` was never meant to 
> catch `raise "r"`.  If only tuples were used for collections, it would 
> create havoc for fewer of any weirdos who had used strange markers of their 
> own devising.
> 
> It looks like tuples were chosen as the most "lightweight", or maybe least 
> intrusive, sequence type to require to denote a collection of exceptions.
> 
> You see a similar decision, with the opposite emphasis, with the string 
> modulo operator.  The second operand is supposed to be a tuple, but if the 
> template string needs only one value, then the rules are relaxed and any 
> single non-tuple value is used as-is.
> 

Compatilbility; that makes sense.  I came to python well after strings were 
used for exceptions.  Thanks.


Charles Yeomans

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


Re: except clause syntax question

2012-01-31 Thread Ethan Furman

Charles Yeomans wrote:

On Jan 31, 2012, at 9:51 AM, Steven D'Aprano wrote:

On Tue, 31 Jan 2012 08:57:31 -0500, Charles Yeomans wrote:


I don't think of a tuple as a container, and I don't think it a
misunderstanding on my part to think this.

>>

Well, it is a misunderstanding, because tuples ARE containers. You

>> might as well say "I don't think of boxes as containers". What
>> exactly are they if not containers?


Tuple is a heterogenous datatype that allows one to define objects

> ad hoc.

And any object can be seen as a container for its component pieces -- 
some are just more general than others.


Compare:

location = (13, 4, 9)# line, word, char
time = (10, 15, 41)  # hour, minute, second
result = ('this', 'that', 'huh') # result a, result b, result c

with:

record1 = Record('Ethan', 41, Male)
record2 = Record('Charles', 37, Male)
record3 = Record('Steven', 43, Male)
record4 = Record('Jennifer', 39, Female)

In this example, Records have a set layout and so it is more common to 
think of a Record as a thing;  location, time, and result, however, are 
all random tuples created on the fly with no absolute restrictions on 
what goes in which position.


lists, dicts, sets, and tuples are general purpose containers; strs (and 
most user defined classes) are special purpose containers.



That is to say, a tuple represents a single thing distinct from its

> components.

You could say that about a list as well.  Doesn't change the fact that a 
list is a container.



One can certainly view a tuple as a list, just as one can view a string

> as a list of characters, and sometimes that's useful; the Python dictum
> "there should only be one way to do it" doesn't imply that there is only
> one way to think of it.

The 'dictum' is "there should only be one *obvious* way to do it" 
(emphasis added).


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


Re: except clause syntax question

2012-01-31 Thread Charles Yeomans

On Jan 31, 2012, at 11:38 AM, Devin Jeanpierre wrote:

> On Tue, Jan 31, 2012 at 11:23 AM, Charles Yeomans
>  wrote:
>> 
>> On Jan 31, 2012, at 9:51 AM, Steven D'Aprano wrote:
>> 
>>> On Tue, 31 Jan 2012 08:57:31 -0500, Charles Yeomans wrote:
>>> 
 I don't think of a tuple as a container, and I don't think it a
 misunderstanding on my part to think this.
>>> 
>>> Well, it is a misunderstanding, because tuples ARE containers. You might
>>> as well say "I don't think of boxes as containers". What exactly are they
>>> if not containers?
>> 
>> 
>> Tuple is a heterogenous datatype that allows one to define objects ad hoc. 
>> That is to say, a tuple represents a single thing distinct from its 
>> components.  For example, suppose you need to represent a location in text 
>> by line number and offset within a line.  A tuple object makes it easy to do 
>> so without writing a class having no methods other than a constructor.  
>> Here, the components, a line number and an offset, define a new object 
>> distinct from the pieces.
>> 
>> One can certainly view a tuple as a list, just as one can view a string as a 
>> list of characters, and sometimes that's useful; the Python dictum "there 
>> should only be one way to do it" doesn't imply that there is only one way to 
>> think of it.
>> 
>> Nor am I the only person who sees such a distinction between tuple and list.
> 
> Perhaps it'd be useful to look at how the Python language reference
> defines containers?
> 
> Quote:
> 
> Some objects contain references to other objects; these are called
> containers. Examples of containers are tuples, lists and dictionaries.
> The references are part of a container’s value. In most cases, when we
> talk about the value of a container, we imply the values, not the
> identities of the contained objects; however, when we talk about the
> mutability of a container, only the identities of the immediately
> contained objects are implied. So, if an immutable container (like a
> tuple) contains a reference to a mutable object, its value changes if
> that mutable object is changed.
> 
> End quote.
> (Offtopic: How do I do an external block quote appropriately in an email?)
> 
> Tuples are most certainly containers, precisely _because_ they're an
> ad-hoc way to define objects, where the only purpose of the object is
> to contain the values inside the tuple.
> 
> But these are just words and it's beside the point. We should talk of
> things as if the word "container" didn't matter, because I don't think
> that's what you meant, but neither do I want to put words in your
> mouth.
> 
> My interpretation is that you see tuples as an object where you can
> get meaningfully things by field, rather than just grab arbitrarily
> from a bag of things. This isn't the only way they are used, see the
> except statement (hee) and their use as keys in dictionaries. But it
> is true that their immutable length makes them very well suited to the
> task of representing product types.

I had read that bit of documentation, and don't entirely agree with it.  
Certainly many objects contain references to other objects, but are not 
considered containers.

And I claim that tuples are most certainly not containers, when they are used 
to define an object as a collection of other objects, like the text-location 
example I offered earlier.  On the other hand, it is certainly true that tuples 
quack like containers, as do strings.


Charles Yeomans


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


Re: except clause syntax question

2012-01-31 Thread Devin Jeanpierre
On Tue, Jan 31, 2012 at 11:23 AM, Charles Yeomans
 wrote:
>
> On Jan 31, 2012, at 9:51 AM, Steven D'Aprano wrote:
>
>> On Tue, 31 Jan 2012 08:57:31 -0500, Charles Yeomans wrote:
>>
>>> I don't think of a tuple as a container, and I don't think it a
>>> misunderstanding on my part to think this.
>>
>> Well, it is a misunderstanding, because tuples ARE containers. You might
>> as well say "I don't think of boxes as containers". What exactly are they
>> if not containers?
>
>
> Tuple is a heterogenous datatype that allows one to define objects ad hoc. 
> That is to say, a tuple represents a single thing distinct from its 
> components.  For example, suppose you need to represent a location in text by 
> line number and offset within a line.  A tuple object makes it easy to do so 
> without writing a class having no methods other than a constructor.  Here, 
> the components, a line number and an offset, define a new object distinct 
> from the pieces.
>
> One can certainly view a tuple as a list, just as one can view a string as a 
> list of characters, and sometimes that's useful; the Python dictum "there 
> should only be one way to do it" doesn't imply that there is only one way to 
> think of it.
>
> Nor am I the only person who sees such a distinction between tuple and list.

Perhaps it'd be useful to look at how the Python language reference
defines containers?

Quote:

Some objects contain references to other objects; these are called
containers. Examples of containers are tuples, lists and dictionaries.
The references are part of a container’s value. In most cases, when we
talk about the value of a container, we imply the values, not the
identities of the contained objects; however, when we talk about the
mutability of a container, only the identities of the immediately
contained objects are implied. So, if an immutable container (like a
tuple) contains a reference to a mutable object, its value changes if
that mutable object is changed.

End quote.
(Offtopic: How do I do an external block quote appropriately in an email?)

Tuples are most certainly containers, precisely _because_ they're an
ad-hoc way to define objects, where the only purpose of the object is
to contain the values inside the tuple.

But these are just words and it's beside the point. We should talk of
things as if the word "container" didn't matter, because I don't think
that's what you meant, but neither do I want to put words in your
mouth.

My interpretation is that you see tuples as an object where you can
get meaningfully things by field, rather than just grab arbitrarily
from a bag of things. This isn't the only way they are used, see the
except statement (hee) and their use as keys in dictionaries. But it
is true that their immutable length makes them very well suited to the
task of representing product types.

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


Re: except clause syntax question

2012-01-31 Thread Charles Yeomans

On Jan 31, 2012, at 9:51 AM, Steven D'Aprano wrote:

> On Tue, 31 Jan 2012 08:57:31 -0500, Charles Yeomans wrote:
> 
>> I don't think of a tuple as a container, and I don't think it a
>> misunderstanding on my part to think this.
> 
> Well, it is a misunderstanding, because tuples ARE containers. You might 
> as well say "I don't think of boxes as containers". What exactly are they 
> if not containers?


Tuple is a heterogenous datatype that allows one to define objects ad hoc. That 
is to say, a tuple represents a single thing distinct from its components.  For 
example, suppose you need to represent a location in text by line number and 
offset within a line.  A tuple object makes it easy to do so without writing a 
class having no methods other than a constructor.  Here, the components, a line 
number and an offset, define a new object distinct from the pieces.

One can certainly view a tuple as a list, just as one can view a string as a 
list of characters, and sometimes that's useful; the Python dictum "there 
should only be one way to do it" doesn't imply that there is only one way to 
think of it.

Nor am I the only person who sees such a distinction between tuple and list.


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


Re: except clause syntax question

2012-01-31 Thread Steven D'Aprano
On Tue, 31 Jan 2012 08:57:31 -0500, Charles Yeomans wrote:

> I don't think of a tuple as a container, and I don't think it a
> misunderstanding on my part to think this.

Well, it is a misunderstanding, because tuples ARE containers. You might 
as well say "I don't think of boxes as containers". What exactly are they 
if not containers?




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


Re: except clause syntax question

2012-01-31 Thread Charles Yeomans

On Jan 30, 2012, at 7:00 PM, Steven D'Aprano wrote:

> On Mon, 30 Jan 2012 12:41:00 -0500, Charles Yeomans wrote:
> 
>> To catch more than one exception type in an except block, one writes
>> 
>> except (A, B, C) as e:
>> 
>> I'm wondering why it was decided to match tuples, but not lists:
>> 
>> except [A, B, C] as e:
> 
> Simplicity.
> 
> If you also allow lists, then why not allow arbitrary sequences? What 
> about iterators, do you allow them? That could be awkward, because 
> iterators can only be run through once. Dictionaries are also iterable, 
> so once you allow arbitrary iterables, you get dicts. The whole thing 
> becomes a mess. Better to keep it simple and only allow a single 
> canonical collection type, and in Python, that type is tuple, not list.
> 
> Tuples are that canonical collection type because they have a number of 
> desirable properties:
> 
> - Tuples are small and memory efficient, using the smallest amount of
>  memory needed to hold their items. Lists typically carry a block of
>  spare memory, to make insertions fast.
> 
> - Consequently the Python virtual machine can create them rapidly and
>  efficiently.
> 
> - Tuples are immutable, so you don't have to worry about passing one to a
>  function and having the function modify it behind your back.
> 
> - Tuples are ordered, for the times where that matters.
> 
> - Since the typical use-case is to iterate over the items in fixed order,
>  there's no need to pay the extra expense for a dict or set.
> 
> - Tuples are simple to write: in general you only need commas between
>  items. Sometimes, to avoid ambiguity or change the precedence of
>  calculation, you also need round brackets (parentheses for Americans).
>  Except clauses are one of those times.
> 
> - Frozensets and sets are ruled out for historical reasons: they didn't
>  exist until Python 2.3. Besides, which would you rather write?
> 
>  ("abc", "def")
>  frozenset([abc", "def"])
> 
> - Sets and lists are ruled out because they are mutable, both require
>  much more memory, and sets have a heavier computational burden.
> 
> 
> 
>> The latter makes more sense semantically to me -- "catch all exception
>> types in a list" as opposed to "catch this single thing composed of
>> three exception types".
> 
> Then you are labouring under a misunderstanding. You're not catching a 
> tuple, because tuples are never thrown. You're catching any of the 
> exceptions that are contained in that tuple.
> 
> Both lists and tuples *are* single things in themselves. Both lists and 
> tuples are containers:
> 
> A list is a single thing that contains other things. 
> 
> A tuple is a single thing that contains other things.
> 

I don't think of a tuple as a container, and I don't think it a 
misunderstanding on my part to think this.  But I am aware that it is common to 
use tuples as immutable lists.  

I don't see that performance was really a consideration, given that one can use 
any expression in an except statement --

except IOError if today == 'Monday' else OSError as e

or 

L = []
try:
#code

except tuple(L) as e:
pass

except Exception, e:
L.append(e.__class__)

In any case, though I appreciate your attempt at a post hoc justification, I 
was hoping for a positive explanation. 

Charles Yeomans


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


Re: except clause syntax question

2012-01-31 Thread Mel Wilson
Charles Yeomans wrote:

> To catch more than one exception type in an except block, one writes
> 
> except (A, B, C) as e:
> 
> I'm wondering why it was decided to match tuples, but not lists:
> 
> except [A, B, C] as e:
> 
> The latter makes more sense semantically to me -- "catch all exception
> types in a list" as opposed to "catch this single thing composed of three
> exception types".

On reflection, it seems to hint at a style that Python extensions were made 
in.  (IIRC) the first operand in an `except` statement was originally just 
an arbitrary marker to identify the exception.  Unique string values were 
customary, although the Python library defined things with standard 
exception names.  Using a string means that general exceptions weren't to be 
collected in general sequences; `except "Serious Error"` was never meant to 
catch `raise "r"`.  If only tuples were used for collections, it would 
create havoc for fewer of any weirdos who had used strange markers of their 
own devising.

It looks like tuples were chosen as the most "lightweight", or maybe least 
intrusive, sequence type to require to denote a collection of exceptions.

You see a similar decision, with the opposite emphasis, with the string 
modulo operator.  The second operand is supposed to be a tuple, but if the 
template string needs only one value, then the rules are relaxed and any 
single non-tuple value is used as-is.


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


Re: except clause syntax question

2012-01-30 Thread Devin Jeanpierre
On Mon, Jan 30, 2012 at 7:00 PM, Steven D'Aprano
 wrote:
> On Mon, 30 Jan 2012 12:41:00 -0500, Charles Yeomans wrote:
>
>> To catch more than one exception type in an except block, one writes
>>
>> except (A, B, C) as e:
>>
>> I'm wondering why it was decided to match tuples, but not lists:
>>
>> except [A, B, C] as e:
>
> Simplicity.
>
-snip-

I agree with the snipped, but would also like to add that regardless
of why it might be so, tuples do appear to be the canonical type for
collections that need to be typechecked -- not just for except; it's a
consistent thing that if you're going to do something with "X, or a
bunch of X's", then it's either an X or a tuple of X's. For example,
string formatting with % works this way, as does isinstance(a, X).

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


Re: except clause syntax question

2012-01-30 Thread Steven D'Aprano
On Mon, 30 Jan 2012 12:41:00 -0500, Charles Yeomans wrote:

> To catch more than one exception type in an except block, one writes
> 
> except (A, B, C) as e:
> 
> I'm wondering why it was decided to match tuples, but not lists:
> 
> except [A, B, C] as e:

Simplicity.

If you also allow lists, then why not allow arbitrary sequences? What 
about iterators, do you allow them? That could be awkward, because 
iterators can only be run through once. Dictionaries are also iterable, 
so once you allow arbitrary iterables, you get dicts. The whole thing 
becomes a mess. Better to keep it simple and only allow a single 
canonical collection type, and in Python, that type is tuple, not list.

Tuples are that canonical collection type because they have a number of 
desirable properties:

- Tuples are small and memory efficient, using the smallest amount of
  memory needed to hold their items. Lists typically carry a block of
  spare memory, to make insertions fast.

- Consequently the Python virtual machine can create them rapidly and
  efficiently.

- Tuples are immutable, so you don't have to worry about passing one to a
  function and having the function modify it behind your back.

- Tuples are ordered, for the times where that matters.

- Since the typical use-case is to iterate over the items in fixed order,
  there's no need to pay the extra expense for a dict or set.

- Tuples are simple to write: in general you only need commas between
  items. Sometimes, to avoid ambiguity or change the precedence of
  calculation, you also need round brackets (parentheses for Americans).
  Except clauses are one of those times.

- Frozensets and sets are ruled out for historical reasons: they didn't
  exist until Python 2.3. Besides, which would you rather write?

  ("abc", "def")
  frozenset([abc", "def"])

- Sets and lists are ruled out because they are mutable, both require
  much more memory, and sets have a heavier computational burden.



> The latter makes more sense semantically to me -- "catch all exception
> types in a list" as opposed to "catch this single thing composed of
> three exception types".

Then you are labouring under a misunderstanding. You're not catching a 
tuple, because tuples are never thrown. You're catching any of the 
exceptions that are contained in that tuple.

Both lists and tuples *are* single things in themselves. Both lists and 
tuples are containers:

A list is a single thing that contains other things. 

A tuple is a single thing that contains other things.




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


Re: except clause syntax question

2012-01-30 Thread Mel Wilson
Charles Yeomans wrote:

> To catch more than one exception type in an except block, one writes
> 
> except (A, B, C) as e:
> 
> I'm wondering why it was decided to match tuples, but not lists:
> 
> except [A, B, C] as e:
> 
> The latter makes more sense semantically to me -- "catch all exception
> types in a list" as opposed to "catch this single thing composed of three
> exception types".

I've always been perfectly fine with sometimes treating tuples as immutable 
sequences, so I'm +0 on saying "more sense semantically", but given that the 
exception list can be a variable, I'm not sure what the gain is by keeping 
it immutable.

#---
#!/usr/bin/env python
# -*- coding: ASCII -*-
'''Demonstrate catching variable exceptions.
'''
def excepter (a, exceptions):
try:
1.0/a
'Number ' + a
except exceptions as e:
print '!!! *** EXCEPTER CAUGHT ONE *** !!!'
print repr (e)

#~ excepter (0, [ZeroDivisionError])
excepter (0, (ZeroDivisionError,))
excepter (1, (ZeroDivisionError,TypeError))
excepter (1, (ZeroDivisionError,))

 #---

excepter called with the list catches nothing, of course.


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


Re: except clause syntax question

2012-01-30 Thread Charles Yeomans

On Jan 30, 2012, at 12:56 PM, Aaron wrote:

> On 01/30/2012 06:41 PM, Charles Yeomans wrote:
>> To catch more than one exception type in an except block, one writes
>> 
>> except (A, B, C) as e:
>> 
>> I'm wondering why it was decided to match tuples, but not lists:
>> 
>> except [A, B, C] as e:
>> 
>> The latter makes more sense semantically to me -- "catch all exception types 
>> in a list" as opposed to "catch this single thing composed of three 
>> exception types".
>> 
>> 
>> Charles Yeomans
>> 
>> 
> 
> Then,  semantically, shouldn't it be a set?

Why, I suppose that would make even more sense.


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


Re: except clause syntax question

2012-01-30 Thread Aaron

On 01/30/2012 06:41 PM, Charles Yeomans wrote:

To catch more than one exception type in an except block, one writes

except (A, B, C) as e:

I'm wondering why it was decided to match tuples, but not lists:

except [A, B, C] as e:

The latter makes more sense semantically to me -- "catch all exception types in a list" 
as opposed to "catch this single thing composed of three exception types".


Charles Yeomans




Then,  semantically, shouldn't it be a set?
--
http://mail.python.org/mailman/listinfo/python-list


except clause syntax question

2012-01-30 Thread Charles Yeomans
To catch more than one exception type in an except block, one writes

except (A, B, C) as e:

I'm wondering why it was decided to match tuples, but not lists:

except [A, B, C] as e:

The latter makes more sense semantically to me -- "catch all exception types in 
a list" as opposed to "catch this single thing composed of three exception 
types".


Charles Yeomans


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


Re: Syntax question

2010-06-02 Thread pmz
On 2 Cze, 20:07, Matteo Landi  wrote:
> Anyway I suggest you to use a syntax like:
>
> >>>b = list(a)
>
> in order to copy a list, it should be better than slicing.
>
>
>
>
>
> On Wed, Jun 2, 2010 at 7:56 PM, geremy condra  wrote:
> > On Wed, Jun 2, 2010 at 10:40 AM, pmz  wrote:
> >> Dear Group,
>
> >> It's really rookie question, but I'm currently helping my wife in some
> >> python-cases, where I'm non-python developer and some of syntax-diffs
> >> make me a bit confused.
>
> >> Could anyone give some light on line, as following:
> >> "ds = d[:]"  ### where 'd' is an array
>
> > I'm guessing you mean that d is a list. The square
> > braces with the colon is python's slicing notation,
> > so if I say [1,2,3,4][0] I get a 1 back, and if I say
> > [1,2,3,4][1:4] I get [2,3,4]. Python also allows a
> > shorthand in slicing, which is that if the first index
> > is not provided, then it assumes 0, and that if the
> > second index is not provided, it assumes the end
> > of the list. Thus, [1,2,3,4][:2] would give me [1,2]
> > and [1,2,3,4][2:] would give me [3, 4]. Here, neither
> > has been provided, so the slice simply takes the
> > items in the list from beginning to end and returns
> > them- [1,2,3,4][:] gives [1,2,3,4].
>
> > The reason someone would want to do this is
> > because lists are mutable data structures. If you
> > fire up your terminal you can try the following
> > example:
>
>  a = [1,2,3,4]
>  b = a
>  c = [:]
>  b[0] = 5
>  b
> > [5,2,3,4]
>  # here's the issue
>  a
> > [5,2,3,4]
>  # and the resolution
>  c
> > [1,2,3,4]
>
> > Hope this helps.
>
> > Geremy Condra
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> --
> Matteo Landihttp://www.matteolandi.net/

In fact, that ain't my syntax, I'd rather use C++ for that project,
because that's my world is not Python, but thank you anyway for help -
I see that Python also has many fans and friends online :) I'll try
help her using your explanations.

THANK you again and all the best,
Przemek M. Zawada

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


Re: Syntax question

2010-06-02 Thread Matteo Landi
Yes it is; d[i:j] is equal to "give me the array from the d[i] to d[j
- 1]", and if you omit i and j then the i and j are respectively
assumed as 0 and len(d) - 1.

On Wed, Jun 2, 2010 at 8:01 PM, pmz  wrote:
> On 2 Cze, 19:56, geremy condra  wrote:
>> On Wed, Jun 2, 2010 at 10:40 AM, pmz  wrote:
>> > Dear Group,
>>
>> > It's really rookie question, but I'm currently helping my wife in some
>> > python-cases, where I'm non-python developer and some of syntax-diffs
>> > make me a bit confused.
>>
>> > Could anyone give some light on line, as following:
>> > "ds = d[:]"  ### where 'd' is an array
>>
>> I'm guessing you mean that d is a list. The square
>> braces with the colon is python's slicing notation,
>> so if I say [1,2,3,4][0] I get a 1 back, and if I say
>> [1,2,3,4][1:4] I get [2,3,4]. Python also allows a
>> shorthand in slicing, which is that if the first index
>> is not provided, then it assumes 0, and that if the
>> second index is not provided, it assumes the end
>> of the list. Thus, [1,2,3,4][:2] would give me [1,2]
>> and [1,2,3,4][2:] would give me [3, 4]. Here, neither
>> has been provided, so the slice simply takes the
>> items in the list from beginning to end and returns
>> them- [1,2,3,4][:] gives [1,2,3,4].
>>
>> The reason someone would want to do this is
>> because lists are mutable data structures. If you
>> fire up your terminal you can try the following
>> example:
>>
>> >>> a = [1,2,3,4]
>> >>> b = a
>> >>> c = [:]
>> >>> b[0] = 5
>> >>> b
>> [5,2,3,4]
>> >>> # here's the issue
>> >>> a
>> [5,2,3,4]
>> >>> # and the resolution
>> >>> c
>>
>> [1,2,3,4]
>>
>> Hope this helps.
>>
>> Geremy Condra
>
> Thank you for such fast answer! I quite catch, but:
> As I see, the d[:] is equal to sentence "get the d array from the
> first to the last element"? :)
>
> P.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Matteo Landi
http://www.matteolandi.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Syntax question

2010-06-02 Thread Matteo Landi
Anyway I suggest you to use a syntax like:

>>>b = list(a)

in order to copy a list, it should be better than slicing.

On Wed, Jun 2, 2010 at 7:56 PM, geremy condra  wrote:
> On Wed, Jun 2, 2010 at 10:40 AM, pmz  wrote:
>> Dear Group,
>>
>> It's really rookie question, but I'm currently helping my wife in some
>> python-cases, where I'm non-python developer and some of syntax-diffs
>> make me a bit confused.
>>
>> Could anyone give some light on line, as following:
>> "ds = d[:]"  ### where 'd' is an array
>
> I'm guessing you mean that d is a list. The square
> braces with the colon is python's slicing notation,
> so if I say [1,2,3,4][0] I get a 1 back, and if I say
> [1,2,3,4][1:4] I get [2,3,4]. Python also allows a
> shorthand in slicing, which is that if the first index
> is not provided, then it assumes 0, and that if the
> second index is not provided, it assumes the end
> of the list. Thus, [1,2,3,4][:2] would give me [1,2]
> and [1,2,3,4][2:] would give me [3, 4]. Here, neither
> has been provided, so the slice simply takes the
> items in the list from beginning to end and returns
> them- [1,2,3,4][:] gives [1,2,3,4].
>
> The reason someone would want to do this is
> because lists are mutable data structures. If you
> fire up your terminal you can try the following
> example:
>
 a = [1,2,3,4]
 b = a
 c = [:]
 b[0] = 5
 b
> [5,2,3,4]
 # here's the issue
 a
> [5,2,3,4]
 # and the resolution
 c
> [1,2,3,4]
>
> Hope this helps.
>
> Geremy Condra
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Matteo Landi
http://www.matteolandi.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Syntax question

2010-06-02 Thread pmz
On 2 Cze, 19:56, geremy condra  wrote:
> On Wed, Jun 2, 2010 at 10:40 AM, pmz  wrote:
> > Dear Group,
>
> > It's really rookie question, but I'm currently helping my wife in some
> > python-cases, where I'm non-python developer and some of syntax-diffs
> > make me a bit confused.
>
> > Could anyone give some light on line, as following:
> > "ds = d[:]"  ### where 'd' is an array
>
> I'm guessing you mean that d is a list. The square
> braces with the colon is python's slicing notation,
> so if I say [1,2,3,4][0] I get a 1 back, and if I say
> [1,2,3,4][1:4] I get [2,3,4]. Python also allows a
> shorthand in slicing, which is that if the first index
> is not provided, then it assumes 0, and that if the
> second index is not provided, it assumes the end
> of the list. Thus, [1,2,3,4][:2] would give me [1,2]
> and [1,2,3,4][2:] would give me [3, 4]. Here, neither
> has been provided, so the slice simply takes the
> items in the list from beginning to end and returns
> them- [1,2,3,4][:] gives [1,2,3,4].
>
> The reason someone would want to do this is
> because lists are mutable data structures. If you
> fire up your terminal you can try the following
> example:
>
> >>> a = [1,2,3,4]
> >>> b = a
> >>> c = [:]
> >>> b[0] = 5
> >>> b
> [5,2,3,4]
> >>> # here's the issue
> >>> a
> [5,2,3,4]
> >>> # and the resolution
> >>> c
>
> [1,2,3,4]
>
> Hope this helps.
>
> Geremy Condra

Thank you for such fast answer! I quite catch, but:
As I see, the d[:] is equal to sentence "get the d array from the
first to the last element"? :)

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


Re: Syntax question

2010-06-02 Thread geremy condra
On Wed, Jun 2, 2010 at 10:40 AM, pmz  wrote:
> Dear Group,
>
> It's really rookie question, but I'm currently helping my wife in some
> python-cases, where I'm non-python developer and some of syntax-diffs
> make me a bit confused.
>
> Could anyone give some light on line, as following:
> "ds = d[:]"  ### where 'd' is an array

I'm guessing you mean that d is a list. The square
braces with the colon is python's slicing notation,
so if I say [1,2,3,4][0] I get a 1 back, and if I say
[1,2,3,4][1:4] I get [2,3,4]. Python also allows a
shorthand in slicing, which is that if the first index
is not provided, then it assumes 0, and that if the
second index is not provided, it assumes the end
of the list. Thus, [1,2,3,4][:2] would give me [1,2]
and [1,2,3,4][2:] would give me [3, 4]. Here, neither
has been provided, so the slice simply takes the
items in the list from beginning to end and returns
them- [1,2,3,4][:] gives [1,2,3,4].

The reason someone would want to do this is
because lists are mutable data structures. If you
fire up your terminal you can try the following
example:

>>> a = [1,2,3,4]
>>> b = a
>>> c = [:]
>>> b[0] = 5
>>> b
[5,2,3,4]
>>> # here's the issue
>>> a
[5,2,3,4]
>>> # and the resolution
>>> c
[1,2,3,4]

Hope this helps.

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


Re: Syntax question

2010-06-02 Thread Chris Rebert
On Wed, Jun 2, 2010 at 10:40 AM, pmz  wrote:
> Dear Group,
>
> It's really rookie question, but I'm currently helping my wife in some
> python-cases, where I'm non-python developer and some of syntax-diffs
> make me a bit confused.
>
> Could anyone give some light on line, as following:
> "ds = d[:]"  ### where 'd' is an array
>
> Let me guess, is it a declaration of two-dimension array?

Nope; Python doesn't really have variable declarations.*
That line of code copies the list `d` ( `[]` is the slicing operator,
and the colon indicates the bounds are the entire list; this is a
Python idiom) and assigns the copy to the variable `ds`. Note that the
copying is shallow (i.e. not recursive, only 1 level deep).

*Well, almost: There are `global` and `nonlocal`, but they're only
needed to specify a variable's scope in certain circumstances when you
want to be able to assign to said variable.

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


Syntax question

2010-06-02 Thread pmz
Dear Group,

It's really rookie question, but I'm currently helping my wife in some
python-cases, where I'm non-python developer and some of syntax-diffs
make me a bit confused.

Could anyone give some light on line, as following:
"ds = d[:]"  ### where 'd' is an array

Let me guess, is it a declaration of two-dimension array?

Thanks a lot for help and all the best,
Przemek M. Zawada
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python-3.2 (SVN) bug [was syntax question]

2009-10-12 Thread Mark Dickinson
On Oct 12, 7:55 am, Helmut Jarausch 
wrote:
> I wrote
> I'm trying to build the recent Python-3.2a (SVN).
> It fails in
> Lib/tokenize.py  (line 87)
[...]
> with: TypeError: group() argument after ** must be a mapping, not tuple

I believe I've found the source of this problem:  the --with-tsc
configure option enables some buggy inline assembly:  see the
READ_TIMESTAMP macro in Python/ceval.c.  This uses the constraint
"A" for the output of the x86 'rdtsc' instruction;  for x86 that's
fine, but for x86_64 it apparently refers to the 'rax' register,
which is wrong:  rdtsc loads its result into the edx and eax
registers.  So the edx register ends up being clobbered without
gcc knowing about it, and all hell breaks loose as a result.

So it is a Python bug, not a compiler bug.  I've updated the
bug report, and the bug should be fixed soonish.

Thanks for reporting this, and for whittling the failure down
to the --with-tsc configure option!

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


Re: Python-3.2 (SVN) bug [was syntax question]

2009-10-12 Thread Mark Dickinson
On Oct 12, 7:55 am, Helmut Jarausch 
wrote:
> I wrote
> I'm trying to build the recent Python-3.2a (SVN).
> It fails in
> Lib/tokenize.py  (line 87)
>
> 85  def group(*choices): return '(' + '|'.join(choices) + ')'
> 86  def any(*choices): return group(*choices) + '*'
> 87  def maybe(*choices): return group(*choices) + '?'
>
> with: TypeError: group() argument after ** must be a mapping, not tuple

It looks like there's already a tracker issue open for this (or
for something that looks an awful lot like this issue):

  http://bugs.python.org/issue6603

but work on that bug has stalled, because core developers have
been unable to reproduce the problem.

It would be really helpful if you could add a comment to that
bug report, giving as much system information (including
compiler information) as possible.  If you can come up with any
ideas about what might be causing the failure, that would also
be useful.

Like Antoine, I'd be inclined to suspect that it's a compiler bug,
but it could also be caused by some not-quite-standards-compliant
C code in Python somewhere. What happens if you turn compiler
optimizations off?  (E.g., by editing configure.in to remove all
occurrences of '-O3' and then rerunning autoconf and autoheader.)

Thanks,

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


Python-3.2 (SVN) bug [was syntax question]

2009-10-12 Thread Helmut Jarausch

I wrote
I'm trying to build the recent Python-3.2a (SVN).
It fails in
Lib/tokenize.py  (line 87)


85  def group(*choices): return '(' + '|'.join(choices) + ')'
86  def any(*choices): return group(*choices) + '*'
87  def maybe(*choices): return group(*choices) + '?'

with: TypeError: group() argument after ** must be a mapping, not tuple


Meanwhile I could narrow this down to the --with-tsc configure option.
Without it, it builds just fine.

Helmut.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
--
http://mail.python.org/mailman/listinfo/python-list


Re: Syntax question

2009-10-10 Thread Helmut Jarausch

Benjamin Peterson wrote:

Helmut Jarausch  skynet.be> writes:

As I said, it's 'make' in Python's source directory
(SVN revision 75309  Last Changed Date: 2009-10-10)


I can't reproduce your failure. What are the exact commands you are using?



CFLAGS='-O3 -mtune=native -msse2 -pipe' CXXFLAGS=$CFLAGS \
  ./configure --prefix=/usr/local --with-computed-gotos --enable-big-digits \
  --with-cxx=g++ --with-signal-module --with-threads \
  --with-pymalloc --with-fpectl --with-tsc

make -j4

Thanks for your help
Helmut.

(I'm using gcc-4.4.1)

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
--
http://mail.python.org/mailman/listinfo/python-list


Re: Syntax question

2009-10-10 Thread Benjamin Peterson
Helmut Jarausch  skynet.be> writes:
> As I said, it's 'make' in Python's source directory
> (SVN revision 75309  Last Changed Date: 2009-10-10)

I can't reproduce your failure. What are the exact commands you are using?




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


Re: Syntax question

2009-10-10 Thread Helmut Jarausch

Benjamin Peterson wrote:

Helmut Jarausch  skynet.be> writes:


Hi,

I'm trying to build the recent Python-3.2a (SVN).
It fails in
Lib/tokenize.py  (line 87)


How are you invoking it?


As I said, it's 'make' in Python's source directory
(SVN revision 75309  Last Changed Date: 2009-10-10)

If it helps, here is the full traceback

Traceback (most recent call last):
  File "./setup.py", line 13, in 
from distutils.core import Extension, setup
  File "/Obj/Obj/Python/python3k/Lib/distutils/core.py", line 18, in 
from distutils.dist import Distribution
  File "/Obj/Obj/Python/python3k/Lib/distutils/dist.py", line 12, in 
import warnings
  File "/Obj/Obj/Python/python3k/Lib/warnings.py", line 6, in 
import linecache
  File "/Obj/Obj/Python/python3k/Lib/linecache.py", line 10, in 
import tokenize
  File "/Obj/Obj/Python/python3k/Lib/tokenize.py", line 94, in 
Ignore = Whitespace + any(r'\\\r?\n' + Whitespace) + maybe(Comment)
  File "/Obj/Obj/Python/python3k/Lib/tokenize.py", line 87, in any
def any(*choices): return group(*choices) + '*'
TypeError: group() argument after ** must be a mapping, not tuple


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
--
http://mail.python.org/mailman/listinfo/python-list


Re: Syntax question

2009-10-10 Thread Benjamin Peterson
Helmut Jarausch  skynet.be> writes:

> 
> Hi,
> 
> I'm trying to build the recent Python-3.2a (SVN).
> It fails in
> Lib/tokenize.py  (line 87)

How are you invoking it?




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


Syntax question

2009-10-10 Thread Helmut Jarausch

Hi,

I'm trying to build the recent Python-3.2a (SVN).
It fails in
Lib/tokenize.py  (line 87)


85  def group(*choices): return '(' + '|'.join(choices) + ')'
86  def any(*choices): return group(*choices) + '*'
87  def maybe(*choices): return group(*choices) + '?'

with: TypeError: group() argument after ** must be a mapping, not tuple

I'm afraid I don't understand this error message.
BTW I'm using python-2.6.3 on the machine where I try to install
3.2a

Many thanks for a hint,
Helmut.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python syntax question

2008-10-21 Thread Gabriel Genellina
En Mon, 13 Oct 2008 13:50:03 -0300, Daniel <[EMAIL PROTECTED]>  
escribió:



RPyC is use in pyscripter to provide remote debugging.  I was having
trouble getting the RPyC module working, and the reason is that the
RPyC site only provides a download for Python 3 (not sure why, since I
suspect that a lot of people are still in the 2.x releases).  Anyway,
I found an old version of RPyC and it worked out great.  I'm not
actually developing it.


Don't be confused - RPyC 3.00 RC1 refers to the 3.0 version of RPyC, not  
of Python.
It is a pure Python project; mainly targeted to 2.5, backported to 2.4 and  
2.3.


--
Gabriel Genellina

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


Re: quick newbie syntax question

2008-10-20 Thread Steven D'Aprano
On Mon, 20 Oct 2008 12:24:14 -0700, Robocop wrote:

> oops!   Sorry about that, i should have just copied my code directly. I
> actually did specify an int in range:
>> > year = '2008'
>> > month = '09'
>> > limit = '31'
>> > for i in range(1,int(limit)):
> 
> The code is currently failing due to the syntax in the filter,
> particularly the section "date = year'-'month'-'i"

Name binding ("date = something") is not an expression and must be on a 
line of its own. So you can't do something like:

function(y=x+1, 2, 3)

(Except of course for function default values, which is not quite the 
same thing.)


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


Re: quick newbie syntax question

2008-10-20 Thread Robocop
date = "%s-%s-%s" % (year, month, i) is exactly what i'd like to do.

The Table object will just be a mysql table, and the filter function
will yield a list filtered for those dates.
For my purposes the limit variable will not be static, depending on
which day of the month it is i will only want it to iterate up to that
date in the month (i use 31 here as an example as i would want it to
iterate through the 30th of september).  Thanks for the input!
On Oct 20, 1:21 pm, Larry Bates <[EMAIL PROTECTED]> wrote:


> Robocop wrote:
> > oops!   Sorry about that, i should have just copied my code directly.
> > I actually did specify an int in range:
> >>> year = '2008'
> >>> month = '09'
> >>> limit = '31'
> >>> for i in range(1,int(limit)):
>
> > The code is currently failing due to the syntax in the filter,
> > particularly the section "date = year'-'month'-'i"
>
> I believe you want something more like
>
> date = "%s-%s-%s" % (year, month, i)
>
> but then again I can't quite figure out what is is that you are wanting to do
> (Note: September doesn't have 31 days).
>
> Where did Table object come from and what does the table.objects.filter method
> do and what are the appropriate arguments to that method?  If it was "my"
> method, and I wanted to use it this way, I would think about changing it so 
> that
> it accepted a filtering function and returned only those objects that passed 
> the
> filtering function:
>
> def myFilter(obj):
>      return (obj.date >= '2008-09-01' and obj.date <= '2008-09-30')
>
> class objects(object):
>      def __init__(self):
>          self.objects = []
>
>      def filter(filterFunc):
>          return [x for x in self.objects if filterFunc(x)]
>
> Then
>
> temp = Table.objects.filter(myFilter)
>
> temp is a list of objects you can act on.
>
> -Larry



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


Re: quick newbie syntax question

2008-10-20 Thread Larry Bates

Robocop wrote:

oops!   Sorry about that, i should have just copied my code directly.
I actually did specify an int in range:

year = '2008'
month = '09'
limit = '31'
for i in range(1,int(limit)):


The code is currently failing due to the syntax in the filter,
particularly the section "date = year'-'month'-'i"


I believe you want something more like

date = "%s-%s-%s" % (year, month, i)

but then again I can't quite figure out what is is that you are wanting to do
(Note: September doesn't have 31 days).

Where did Table object come from and what does the table.objects.filter method
do and what are the appropriate arguments to that method?  If it was "my" 
method, and I wanted to use it this way, I would think about changing it so that 
it accepted a filtering function and returned only those objects that passed the 
filtering function:


def myFilter(obj):
return (obj.date >= '2008-09-01' and obj.date <= '2008-09-30')

class objects(object):
def __init__(self):
self.objects = []

def filter(filterFunc):
return [x for x in self.objects if filterFunc(x)]


Then

temp = Table.objects.filter(myFilter)

temp is a list of objects you can act on.

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


Re: quick newbie syntax question

2008-10-20 Thread Robocop
oops!   Sorry about that, i should have just copied my code directly.
I actually did specify an int in range:
> > year = '2008'
> > month = '09'
> > limit = '31'
> > for i in range(1,int(limit)):

The code is currently failing due to the syntax in the filter,
particularly the section "date = year'-'month'-'i"
--
http://mail.python.org/mailman/listinfo/python-list


Re: quick newbie syntax question

2008-10-20 Thread Chris Rebert
On Mon, Oct 20, 2008 at 12:08 PM, Robocop <[EMAIL PROTECTED]> wrote:
> Is it possible to do something like this syntactically:
>
> year = '2008'
> month = '09'
> limit = '31'
> for i in range(1,limit):

This previous line will fail. range() takes numbers, not strings.
Change 'limit' to an int.

>  temp = Table.objects.filter(date = year'-'month'-'i) up syntax
>  ...do something with temp
> return
>
> I know that the syntax within the filter statement is wrong.  Is it
> even possible to do something like that?  Any help is always
> appreciated.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


quick newbie syntax question

2008-10-20 Thread Robocop
Is it possible to do something like this syntactically:

year = '2008'
month = '09'
limit = '31'
for i in range(1,limit):
  temp = Table.objects.filter(date = year'-'month'-'i)

Re: Python syntax question

2008-10-13 Thread Daniel
On Oct 8, 1:19 pm, "Blubaugh, David A." <[EMAIL PROTECTED]> wrote:
> Sir,
>
> I was just wondering that the module that you are utilizing (Rpyc) is a 
> remote process call module for python?  Is this what you are developing with 
> at this time?
>
> Thanks,
>
> David Blubaugh
>
> -Original Message-
> From: Daniel [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, October 08, 2008 3:11 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Python syntax question
>
> On Oct 8, 12:07 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> > On Wed, 08 Oct 2008 11:02:49 -0700, Daniel wrote:
> > > Here is one error I get when I try to import it:
>
> > >>>> import Rpyc
> > > Traceback (most recent call last):
> > >   File "", line 1, in 
> > >   File "C:\Python25\lib\site-packages\Rpyc\__init__.py", line 7, in
> > > 
> > >     from Rpyc.Lib import rpyc_excepthook
> > >   File "C:\Python25\lib\site-packages\Rpyc\Lib.py", line 65
> > >     print("=== Remote traceback ===", file=stderr)
> > >                                                   ^
> > > SyntaxError: invalid syntax
>
> > > The little carrot points to the equal sign ('=') in 'file=stderr'
>
> > > What's the syntax problem?
>
> > That's Python 3.0 syntax where ``print`` is not a keyword anymore but
> > a function.  Won't work with Python 2.5.
>
> > Ciao,
> >         Marc 'BlackJack' Rintsch
>
> Thanks!  With that I was able to find a solution.
>
> This e-mail transmission contains information that is confidential and may be
> privileged. It is intended only for the addressee(s) named above. If you 
> receive
> this e-mail in error, please do not read, copy or disseminate it in any 
> manner.
> If you are not the intended recipient, any disclosure, copying, distribution 
> or
> use of the contents of this information is prohibited. Please reply to the
> message immediately by informing the sender that the message was misdirected.
> After replying, please erase it from your computer system. Your assistance in
> correcting this error is appreciated.
>
>

RPyC is use in pyscripter to provide remote debugging.  I was having
trouble getting the RPyC module working, and the reason is that the
RPyC site only provides a download for Python 3 (not sure why, since I
suspect that a lot of people are still in the 2.x releases).  Anyway,
I found an old version of RPyC and it worked out great.  I'm not
actually developing it.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python syntax question

2008-10-08 Thread Diez B. Roggisch

Blubaugh, David A. schrieb:

Sir,

I was just wondering that the module that you are utilizing (Rpyc) is a remote process call module for python?  Is this what you are developing with at this time? 


Are you internetically challenged?

http://www.google.de/search?q=rpyc&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:de-DE:official&client=firefox-a

*First* hit. And the google excerpt says

"""
RPyC is a transparent, symmetrical python library for 
distributed-computing. Pronounced "are-pie-see", it began as an RPC 
library (hence the name), ...

"""


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


RE: Python syntax question

2008-10-08 Thread Blubaugh, David A.
Sir,

I was just wondering that the module that you are utilizing (Rpyc) is a remote 
process call module for python?  Is this what you are developing with at this 
time? 

Thanks,


David Blubaugh

 

-Original Message-
From: Daniel [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, October 08, 2008 3:11 PM
To: python-list@python.org
Subject: Re: Python syntax question

On Oct 8, 12:07 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Wed, 08 Oct 2008 11:02:49 -0700, Daniel wrote:
> > Here is one error I get when I try to import it:
>
> >>>> import Rpyc
> > Traceback (most recent call last):
> >   File "", line 1, in 
> >   File "C:\Python25\lib\site-packages\Rpyc\__init__.py", line 7, in 
> > 
> >     from Rpyc.Lib import rpyc_excepthook
> >   File "C:\Python25\lib\site-packages\Rpyc\Lib.py", line 65
> >     print("=== Remote traceback ===", file=stderr)
> >                                                   ^
> > SyntaxError: invalid syntax
>
> > The little carrot points to the equal sign ('=') in 'file=stderr'
>
> > What's the syntax problem?
>
> That's Python 3.0 syntax where ``print`` is not a keyword anymore but 
> a function.  Won't work with Python 2.5.
>
> Ciao,
>         Marc 'BlackJack' Rintsch

Thanks!  With that I was able to find a solution.


This e-mail transmission contains information that is confidential and may be 
privileged. It is intended only for the addressee(s) named above. If you 
receive 
this e-mail in error, please do not read, copy or disseminate it in any manner. 
If you are not the intended recipient, any disclosure, copying, distribution or 
use of the contents of this information is prohibited. Please reply to the 
message immediately by informing the sender that the message was misdirected. 
After replying, please erase it from your computer system. Your assistance in 
correcting this error is appreciated.

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


Re: Python syntax question

2008-10-08 Thread Daniel
On Oct 8, 12:07 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Wed, 08 Oct 2008 11:02:49 -0700, Daniel wrote:
> > Here is one error I get when I try to import it:
>
>  import Rpyc
> > Traceback (most recent call last):
> >   File "", line 1, in 
> >   File "C:\Python25\lib\site-packages\Rpyc\__init__.py", line 7, in
> > 
> >     from Rpyc.Lib import rpyc_excepthook
> >   File "C:\Python25\lib\site-packages\Rpyc\Lib.py", line 65
> >     print("=== Remote traceback ===", file=stderr)
> >                                                   ^
> > SyntaxError: invalid syntax
>
> > The little carrot points to the equal sign ('=') in 'file=stderr'
>
> > What's the syntax problem?
>
> That's Python 3.0 syntax where ``print`` is not a keyword anymore but a
> function.  Won't work with Python 2.5.
>
> Ciao,
>         Marc 'BlackJack' Rintsch

Thanks!  With that I was able to find a solution.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python syntax question

2008-10-08 Thread Marc 'BlackJack' Rintsch
On Wed, 08 Oct 2008 11:02:49 -0700, Daniel wrote:

> Here is one error I get when I try to import it:
> 
 import Rpyc
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "C:\Python25\lib\site-packages\Rpyc\__init__.py", line 7, in
> 
> from Rpyc.Lib import rpyc_excepthook
>   File "C:\Python25\lib\site-packages\Rpyc\Lib.py", line 65
> print("=== Remote traceback ===", file=stderr)
>   ^
> SyntaxError: invalid syntax
> 
> The little carrot points to the equal sign ('=') in 'file=stderr'
> 
> What's the syntax problem?

That's Python 3.0 syntax where ``print`` is not a keyword anymore but a 
function.  Won't work with Python 2.5.

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Python syntax question

2008-10-08 Thread Daniel
I hope this question is OK for this list.  I've downloaded Rpyc and
placed it in my site packages dir.  On some machines it works fine, on
others not so much.

Here is one error I get when I try to import it:

>>> import Rpyc
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python25\lib\site-packages\Rpyc\__init__.py", line 7, in

from Rpyc.Lib import rpyc_excepthook
  File "C:\Python25\lib\site-packages\Rpyc\Lib.py", line 65
print("=== Remote traceback ===", file=stderr)
  ^
SyntaxError: invalid syntax

The little carrot points to the equal sign ('=') in 'file=stderr'

What's the syntax problem?

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


Re: Syntax Question - list multiplication

2007-08-19 Thread James Stroud
MC wrote:
> Classic

Thanks Michel, but maybe this bit of programming jargon needs some 
translation for the uninitiated:

Classic \Clas"sic\ (kl[a^]s"s[i^]k), Classical \Clas"sic*al\, a.
0. Read the FAQ
1. First rate
2. Greek
3. Refined

James


-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Syntax Question - list multiplication

2007-08-19 Thread Duncan Smith
Roel Schroeven wrote:
> Pablo Torres schreef:
> 
>> Hi guys!
>>
>> I am working on Conway's Game of Life right now and I've run into a
>> little problem.
>> I represent dead cells with 0s and live ones with 1s. Check this out:
>>
>> >>> grid = [[0] * 3] * 3
>> >>> grid
>> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>> >>> grid[0][0] = 1
>> [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
>>
>> Now, that's not what I want. I just want the first element of the
>> first sublist to be a 1, not the first of every one. Here's what I
>> tried and didn't work:
>>
>> 0.  grid = [[0] * 3][:] * 3then the same grid[0][0] = 1 statement
>> 1.  grid = [[0][:] * 3] * 3followed by the same assignment
>> 2.  (grid[0])[0] = 1with the original initialization of the grid
>>
>> So, that means that it isn't a problem with two different variables
>> refering to the same list (0. and 1. prove that). What I don't
>> understand is why 2. doesn't work either. I'm baby-feeding my
>> instructions to Python and the mistake is still there. Any ideas?
>>
>> Hope you can help. Thanks in advance,
> 
> 
> The multiplication operator doesn't make new copies; all elements
> reference the same object, as you can see with id():
> 
 lst = [0] * 3
 lst
> [0, 0, 0]
 id(lst[0]), id(lst[1]), id(lst[2])
> (9788716, 9788716, 9788716)
> 

I think you probably meant something like,

>>> lst = [[0] * 3] * 3
>>> lst
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> id(lst[0]), id(lst[1]), id(lst[2])
(15937704, 15937704, 15937704)
>>>

i.e. a list of mutable types where the difference between multiple
objects and multiple references to the same object will be apparent.
With small integers you're likely to find you have multiple references
to the same integer objects however the list is constructed.

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


Re: Syntax Question - list multiplication

2007-08-19 Thread Pablo Torres
Thanks everyone. Now I see why every row in my grid were actually the
same object.

Pablo

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


Re: Syntax Question - list multiplication

2007-08-19 Thread Roel Schroeven
Pablo Torres schreef:
> Hi guys!
> 
> I am working on Conway's Game of Life right now and I've run into a
> little problem.
> I represent dead cells with 0s and live ones with 1s. Check this out:
> 
>   >>> grid = [[0] * 3] * 3
>   >>> grid
>   [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>   >>> grid[0][0] = 1
>   [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
> 
> Now, that's not what I want. I just want the first element of the
> first sublist to be a 1, not the first of every one. Here's what I
> tried and didn't work:
> 
> 0.  grid = [[0] * 3][:] * 3then the same grid[0][0] = 1 statement
> 1.  grid = [[0][:] * 3] * 3followed by the same assignment
> 2.  (grid[0])[0] = 1with the original initialization of the grid
> 
> So, that means that it isn't a problem with two different variables
> refering to the same list (0. and 1. prove that). What I don't
> understand is why 2. doesn't work either. I'm baby-feeding my
> instructions to Python and the mistake is still there. Any ideas?
> 
> Hope you can help. Thanks in advance,

The multiplication operator doesn't make new copies; all elements 
reference the same object, as you can see with id():

 >>> lst = [0] * 3
 >>> lst
[0, 0, 0]
 >>> id(lst[0]), id(lst[1]), id(lst[2])
(9788716, 9788716, 9788716)

As a consequence, if you modify one element, you change them all (since 
it's all the same object). Solution: make sure to create independent lists.

 >>> grid = [[0] * 3 for i in range(3)]
 >>> grid
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
 >>> grid[0][0] = 1

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

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


Re: Syntax Question - list multiplication

2007-08-19 Thread Thomas Jollans
On Sunday 19 August 2007, Pablo Torres wrote:
> Hi guys!
>
> I am working on Conway's Game of Life right now and I've run into a
> little problem.
>
> I represent dead cells with 0s and live ones with 1s. Check this out:
>   >>> grid = [[0] * 3] * 3
>   >>> grid
>
>   [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>
>   >>> grid[0][0] = 1
>
>   [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
>
> Now, that's not what I want. I just want the first element of the
> first sublist to be a 1, not the first of every one. Here's what I
> tried and didn't work:
>
> 0.  grid = [[0] * 3][:] * 3then the same grid[0][0] = 1 statement
> 1.  grid = [[0][:] * 3] * 3followed by the same assignment
> 2.  (grid[0])[0] = 1with the original initialization of the grid
>
> So, that means that it isn't a problem with two different variables
> refering to the same list (0. and 1. prove that). What I don't
> understand is why 2. doesn't work either. I'm baby-feeding my
> instructions to Python and the mistake is still there. Any ideas?


If you want three copies of the list, you need to copy it thrice (well, twice)

Thus:

>>> row = [0] * 3
>>> grid = []
>>> for n in xrange(3): grid.append(row[:])
...
>>> grid
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> grid[0][0] =1
>>> grid
[[1, 0, 0], [0, 0, 0], [0, 0, 0]]
>>>


-- 
  Regards,   Thomas Jollans
GPG key: 0xF421434B may be found on various keyservers, eg pgp.mit.edu
Hacker key :
v4sw6+8Yhw4/5ln3pr5Ock2ma2u7Lw2Nl7Di2e2t3/4TMb6HOPTen5/6g5OPa1XsMr9p-7/-6


signature.asc
Description: This is a digitally signed message part.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Syntax Question - list multiplication

2007-08-19 Thread MC
Classic

-- 
@-salutations

Michel Claveau


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


Syntax Question - list multiplication

2007-08-19 Thread Pablo Torres
Hi guys!

I am working on Conway's Game of Life right now and I've run into a
little problem.
I represent dead cells with 0s and live ones with 1s. Check this out:

>>> grid = [[0] * 3] * 3
>>> grid
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> grid[0][0] = 1
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]

Now, that's not what I want. I just want the first element of the
first sublist to be a 1, not the first of every one. Here's what I
tried and didn't work:

0.  grid = [[0] * 3][:] * 3then the same grid[0][0] = 1 statement
1.  grid = [[0][:] * 3] * 3followed by the same assignment
2.  (grid[0])[0] = 1with the original initialization of the grid

So, that means that it isn't a problem with two different variables
refering to the same list (0. and 1. prove that). What I don't
understand is why 2. doesn't work either. I'm baby-feeding my
instructions to Python and the mistake is still there. Any ideas?

Hope you can help. Thanks in advance,

Pablo

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


PyLint syntax question

2006-11-29 Thread Brendon
Hi,

This may seem like a stupid question, but I'd like to know anyway.  How
do I used pylint's --ignore option to prevent it from looking at some
files?  The documentation says you have to use a base name.  What the
heck is a base name?

Are there any other ways to stop it looking at certain files?  At the
moment I'm trying to get it to avoid the ones that contain 'import wx'.


Thanks,
Brendon

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


Newbie ZSI / syntax question

2006-03-06 Thread mrstephengross
I'm getting started working with ZSI. I have a client script that looks
like this:

===
import ZSI.client
b = ZSI.client.Binding()
b.Send('http://...', 'verifyUserRegistered', '00:00:00:00:00:00')
===

When I run it, I get the following error:

===
  File "./sage_installer.py", line 13, in ?
b.Send('http://...', 'verifyUserRegistered', '00:00:00:00:00:00')
  File "/usr/local/lib/python2.3/site-packages/ZSI/client.py", line
209, in Send
self.h.connect()
  File "/usr/local/lib/python2.3/httplib.py", line 548, in connect
raise socket.error, msg
socket.error: (2, 'No such file or directory')
===

I'm trying to make sure I'm calling the Binding.Send() function
correctly. Does it look right? Note that I've replaced the actual url
(to the php server-side script) with '...' in the above example.

Thanks in advance,
--Steve ([EMAIL PROTECTED])

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


Re: syntax question - if 1:print 'a';else:print 'b'

2005-10-28 Thread Christopher Subich
Steve Holden wrote:

>> On Thu, 2005-10-27 at 14:00, Gregory Piñero wrote:
>>
>>> Not quite because if something(3) fails, I still want something(4) to
>>> run.  
> Then the obvious extension:
> 
> for i in range(20):
>...
> 
> but I get the idea that Gregory was thinking of different statements 
> rather than calls to the same function with different arguments.


Sorry for the descendant-reply, but the original hasn't hit my news 
server yet (I think).

It sounds like Gregory wants a Python equivalent of "on error continue 
next," which is really a bad idea almost everywhere.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: syntax question - if 1:print 'a';else:print 'b'

2005-10-27 Thread James Stroud
On Thursday 27 October 2005 11:16, Carsten Haese wrote:
> On Thu, 2005-10-27 at 14:00, Gregory Piñero wrote:
> > Not quite because if something(3) fails, I still want something(4) to
> > run.
>
> def something_ignore_exceptions(x):
>   try: something(x)
>   except: pass
>
> something_ignore_exceptions(1)
> something_ignore_exceptions(2)
> # etc...

I'm at the point where I don't understand why you can't use this:

def try_somethings(alist):
  def try_something(el):
try:
  something(el)
except e:
  do_exception(el, e)
  for an_el in alist:
try_something(an_el)

See also .

James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: syntax question - if 1:print 'a';else:print 'b'

2005-10-27 Thread Steve Holden
Carsten Haese wrote:
> On Thu, 2005-10-27 at 14:00, Gregory Piñero wrote:
> 
>>Not quite because if something(3) fails, I still want something(4) to
>>run.  
> 
> 
> def something_ignore_exceptions(x):
>   try: something(x)
>   except: pass
> 
> something_ignore_exceptions(1)
> something_ignore_exceptions(2)
> # etc...
Then the obvious extension:

for i in range(20):
...

but I get the idea that Gregory was thinking of different statements 
rather than calls to the same function with different arguments.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.python.org/pycon/

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


Re: syntax question - if 1:print 'a';else:print 'b'

2005-10-27 Thread Carsten Haese
On Thu, 2005-10-27 at 14:00, Gregory Piñero wrote:
> Not quite because if something(3) fails, I still want something(4) to
> run.  

def something_ignore_exceptions(x):
  try: something(x)
  except: pass

something_ignore_exceptions(1)
something_ignore_exceptions(2)
# etc...

HTH,

Carsten Haese


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


Re: syntax question - if 1:print 'a';else:print 'b'

2005-10-27 Thread Gregory Piñero
Not quite because if something(3) fails, I still want something(4) to run.  
On 10/27/05, Micah Elliott <[EMAIL PROTECTED]> wrote:
If you just want to ignore the exceptions while saving space/typing,you could equivalently do::try:something(1)something(2)# ...except:pass
or::try:something(1); something(2); # ...except:passor::try:for i in range(yourlimit):something(i)except:pass
--_ _ ___|V|icah |- lliott  http://micah.elliott.name  [EMAIL PROTECTED]" " """
-- Gregory PiñeroChief Innovation OfficerBlended Technologies(www.blendedtechnologies.com)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: syntax question - if 1:print 'a';else:print 'b'

2005-10-27 Thread Micah Elliott
On Oct 27, Gregory Piñero wrote:
> 
>my goal really was to do:
>try:something(1);except:pass
>try:something(2);except:pass
>try:something(3);except:pass
>...
>for about 20 lines.

If you just want to ignore the exceptions while saving space/typing,
you could equivalently do::

try:
something(1)
something(2)
# ...
except:
pass

or::

try:
something(1); something(2); # ...
except:
pass

or::

try:
for i in range(yourlimit):
something(i)
except:
pass

-- 
_ _ ___
|V|icah |- lliott  http://micah.elliott.name  [EMAIL PROTECTED]
" " """
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >