Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80457 > unrolled thread

A syntax question

Started byMok-Kong Shen <mok-kong.shen@t-online.de>
First post2014-11-10 12:07 +0100
Last post2014-11-10 10:27 -0500
Articles 18 — 12 participants

Back to article view | Back to comp.lang.python


Contents

  A syntax question Mok-Kong Shen <mok-kong.shen@t-online.de> - 2014-11-10 12:07 +0100
    Re: A syntax question alister <alister.nospam.ware@ntlworld.com> - 2014-11-10 11:23 +0000
      Re: A syntax question David Palao <dpalao.python@gmail.com> - 2014-11-10 12:31 +0100
        Re: A syntax question Grant Edwards <invalid@invalid.invalid> - 2014-11-10 14:44 +0000
          Re: A syntax question alister <alister.nospam.ware@ntlworld.com> - 2014-11-10 14:54 +0000
            Re: A syntax question alister <alister.nospam.ware@ntlworld.com> - 2014-11-10 14:56 +0000
              Re: A syntax question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-10 19:53 -0500
                Re: A syntax question alister <alister.nospam.ware@ntlworld.com> - 2014-11-11 09:04 +0000
            Re: A syntax question Grant Edwards <invalid@invalid.invalid> - 2014-11-10 16:11 +0000
              Re: A syntax question Chris Angelico <rosuav@gmail.com> - 2014-11-11 03:18 +1100
                Re: A syntax question Roy Smith <roy@panix.com> - 2014-11-10 14:48 -0500
      Re: A syntax question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-11-10 20:08 +0000
      Re: A syntax question Tim Chase <python.list@tim.thechases.com> - 2014-11-10 14:29 -0600
    Re: A syntax question Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-11-10 12:39 +0100
    Re: A syntax question Joel Goldstick <joel.goldstick@gmail.com> - 2014-11-10 09:35 -0500
    Re: A syntax question Chris Angelico <rosuav@gmail.com> - 2014-11-11 01:54 +1100
    Re: A syntax question Peter Otten <__peter__@web.de> - 2014-11-10 16:10 +0100
    Re: A syntax question Joel Goldstick <joel.goldstick@gmail.com> - 2014-11-10 10:27 -0500

#80457 — A syntax question

FromMok-Kong Shen <mok-kong.shen@t-online.de>
Date2014-11-10 12:07 +0100
SubjectA syntax question
Message-ID<m3q6ae$m8p$1@news.albasani.net>
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()

[toc] | [next] | [standalone]


#80458

Fromalister <alister.nospam.ware@ntlworld.com>
Date2014-11-10 11:23 +0000
Message-ID<bf18w.682041$9R5.556154@fx29.am4>
In reply to#80457
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

[toc] | [prev] | [next] | [standalone]


#80460

FromDavid Palao <dpalao.python@gmail.com>
Date2014-11-10 12:31 +0100
Message-ID<mailman.15620.1415619071.18130.python-list@python.org>
In reply to#80458
> 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.

[toc] | [prev] | [next] | [standalone]


#80465

FromGrant Edwards <invalid@invalid.invalid>
Date2014-11-10 14:44 +0000
Message-ID<m3qj14$srf$1@reader1.panix.com>
In reply to#80460
On 2014-11-10, David Palao <dpalao.python@gmail.com> 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.edwards        Yow! Am I in Milwaukee?
                                  at               
                              gmail.com            

[toc] | [prev] | [next] | [standalone]


#80467

Fromalister <alister.nospam.ware@ntlworld.com>
Date2014-11-10 14:54 +0000
Message-ID<3l48w.682048$9R5.402039@fx29.am4>
In reply to#80465
On Mon, 10 Nov 2014 14:44:53 +0000, Grant Edwards wrote:

> On 2014-11-10, David Palao <dpalao.python@gmail.com> 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"

[toc] | [prev] | [next] | [standalone]


#80468

Fromalister <alister.nospam.ware@ntlworld.com>
Date2014-11-10 14:56 +0000
Message-ID<fm48w.682049$9R5.149587@fx29.am4>
In reply to#80467
On Mon, 10 Nov 2014 14:54:55 +0000, alister wrote:

> On Mon, 10 Nov 2014 14:44:53 +0000, Grant Edwards wrote:
> 
>> On 2014-11-10, David Palao <dpalao.python@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#80501

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2014-11-10 19:53 -0500
Message-ID<mailman.15646.1415667242.18130.python-list@python.org>
In reply to#80468
On Mon, 10 Nov 2014 14:56:11 GMT, alister
<alister.nospam.ware@ntlworld.com> declaimed the following:

>On Mon, 10 Nov 2014 14:54:55 +0000, alister wrote:
>
>> On Mon, 10 Nov 2014 14:44:53 +0000, Grant Edwards wrote:
>> 
>>> On 2014-11-10, David Palao <dpalao.python@gmail.com> 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.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#80512

Fromalister <alister.nospam.ware@ntlworld.com>
Date2014-11-11 09:04 +0000
Message-ID<Uik8w.690677$3F6.423979@fx02.am4>
In reply to#80501
On Mon, 10 Nov 2014 19:53:56 -0500, Dennis Lee Bieber wrote:

> On Mon, 10 Nov 2014 14:56:11 GMT, alister
> <alister.nospam.ware@ntlworld.com> declaimed the following:
> 
>>On Mon, 10 Nov 2014 14:54:55 +0000, alister wrote:
>>
>>> On Mon, 10 Nov 2014 14:44:53 +0000, Grant Edwards wrote:
>>> 
>>>> On 2014-11-10, David Palao <dpalao.python@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#80471

FromGrant Edwards <invalid@invalid.invalid>
Date2014-11-10 16:11 +0000
Message-ID<m3qo3a$gi6$1@reader1.panix.com>
In reply to#80467
On 2014-11-10, alister <alister.nospam.ware@ntlworld.com> wrote:
> On Mon, 10 Nov 2014 14:44:53 +0000, Grant Edwards wrote:
>
>> On 2014-11-10, David Palao <dpalao.python@gmail.com> 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.edwards        Yow! The FALAFEL SANDWICH
                                  at               lands on my HEAD and I
                              gmail.com            become a VEGETARIAN ...

[toc] | [prev] | [next] | [standalone]


#80472

FromChris Angelico <rosuav@gmail.com>
Date2014-11-11 03:18 +1100
Message-ID<mailman.15627.1415636743.18130.python-list@python.org>
In reply to#80471
On Tue, Nov 11, 2014 at 3:11 AM, Grant Edwards <invalid@invalid.invalid> 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

[toc] | [prev] | [next] | [standalone]


#80481

FromRoy Smith <roy@panix.com>
Date2014-11-10 14:48 -0500
Message-ID<roy-57EAFE.14474210112014@news.panix.com>
In reply to#80472
In article <mailman.15627.1415636743.18130.python-list@python.org>,
 Chris Angelico <rosuav@gmail.com> wrote:

> On Tue, Nov 11, 2014 at 3:11 AM, Grant Edwards <invalid@invalid.invalid> 
> 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

[toc] | [prev] | [next] | [standalone]


#80486

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-11-10 20:08 +0000
Message-ID<mailman.15636.1415650206.18130.python-list@python.org>
In reply to#80458
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

[toc] | [prev] | [next] | [standalone]


#80488

FromTim Chase <python.list@tim.thechases.com>
Date2014-11-10 14:29 -0600
Message-ID<mailman.15638.1415651228.18130.python-list@python.org>
In reply to#80458
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: <bf18w.682041$9R5.556154@fx29.am4>
References: <m3q6ae$m8p$1@news.albasani.net> <bf18w.682041$9R5.556154@fx29.am4>

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


[toc] | [prev] | [next] | [standalone]


#80461

FromWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Date2014-11-10 12:39 +0100
Message-ID<mailman.15621.1415619550.18130.python-list@python.org>
In reply to#80457
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()

[toc] | [prev] | [next] | [standalone]


#80464

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2014-11-10 09:35 -0500
Message-ID<mailman.15623.1415630155.18130.python-list@python.org>
In reply to#80457
On Mon, Nov 10, 2014 at 6:39 AM, Wolfgang Maier
<wolfgang.maier@biologie.uni-freiburg.de> 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

[toc] | [prev] | [next] | [standalone]


#80466

FromChris Angelico <rosuav@gmail.com>
Date2014-11-11 01:54 +1100
Message-ID<mailman.15624.1415631252.18130.python-list@python.org>
In reply to#80457
On Tue, Nov 11, 2014 at 1:35 AM, Joel Goldstick
<joel.goldstick@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#80469

FromPeter Otten <__peter__@web.de>
Date2014-11-10 16:10 +0100
Message-ID<mailman.15625.1415632232.18130.python-list@python.org>
In reply to#80457
Joel Goldstick wrote:

> On Mon, Nov 10, 2014 at 6:39 AM, Wolfgang Maier
> <wolfgang.maier@biologie.uni-freiburg.de> 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 "<stdin>", line 1, in <module>
  File "<stdin>", 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


[toc] | [prev] | [next] | [standalone]


#80470

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2014-11-10 10:27 -0500
Message-ID<mailman.15626.1415633253.18130.python-list@python.org>
In reply to#80457
On Mon, Nov 10, 2014 at 9:54 AM, Chris Angelico <rosuav@gmail.com> wrote:
> On Tue, Nov 11, 2014 at 1:35 AM, Joel Goldstick
> <joel.goldstick@gmail.com> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web