Re: [Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

2015-04-08 Thread Dave Angel

On 04/07/2015 10:16 PM, boB Stepp wrote:


Despite Mark's warning, I feel I must see if I understand what is going on here.

Switching to Py 3.4 since I am now at home:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600
64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.

d = {'a': '123'}
def func(s=d['a']):

   print(s)
   print(d['a'])


func()

123
123

d['a'] = 'new value'
func()

123
new value

I added an additional print to the function to show the dictionary
entry's behavior.

First, my current understanding is that this form of the function does
not object to the presence of d['a'] in its parameter list because s
is the real parameter, d['a'] is its default value, but s is not
actually evaluated until run time.


s is not evaluated till the print statement.  s is *bound* at function 
call time.  And at that time it is either bound to the object passed by 
the caller, or to the default object.


In a simple assignment statement:
   a = b + 6

the expression on the right is evaluated.  The name on the left is not 
evaluated, it is bound to.  So we say

   "a is bound to the result of the expression b+6"



But once s *is* evaluated, it stores  a reference to the original


  s is bound to the expression ''default_object'', which is to say it 
copies the same reference that the default object stored earlier.  So it 
is bound to '123'



object, '123'. Changing d['a'] outside the function to a new value
does not alter the fact that s is storing the very same reference to
'123'. After reassigning d['a'] to point to the new object 'new
value', a new call to func() shows s still referencing the original
object and d['a'] referencing the new object. Is my comprehension of
these details correct? If yes, this is why I must constantly remind
myself that identifiers store references to objects, and that some
objects are mutable and some aren't, and these Python facts of life
are constantly challenging my old FORTRAN <= 77 ways of thinking...
~(:>))




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


Re: [Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

2015-04-07 Thread Cameron Simpson

On 07Apr2015 21:16, boB Stepp  wrote:

Despite Mark's warning, I feel I must see if I understand what is going on here.

Switching to Py 3.4 since I am now at home:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600
64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.

d = {'a': '123'}
def func(s=d['a']):

 print(s)
 print(d['a'])


func()

123
123

d['a'] = 'new value'
func()

123
new value

I added an additional print to the function to show the dictionary
entry's behavior.

First, my current understanding is that this form of the function does
not object to the presence of d['a'] in its parameter list because s
is the real parameter, d['a'] is its default value, but s is not
actually evaluated until run time.


Yes and no.

Yes to "s is the real parameter, d['a'] is its default value".

No to "s is not actually evaluated until run time".

When you _define_ the function, the _current_ value of d['a'] is stashed in the 
function definition as the default value.


When you _call_ the function, and omit the 's' parameter, the default value is 
used.


However, that value was _computed_ when the function was compiled: the function 
definition does not keep "d['a']" around for evaluation, instead that 
expression is evaluated when you define the function, and the reference to the 
resulting value kept around. That is thus '123'.


So when you go:

 d['a'] = 'new value'

the contents of the "d" dictionary have changed. But all that has happened is 
that the reference to '123' is no longer in the dictionary; instead a reference 
to 'new value' is in the dictionary.


When you call "func()" the name "s" is bound to the default value, which is the 
'123' as computed at function definition time. And it prints that. Of course, 
when you print "d['a']" it must evaluate that then, and finds 'new value'.


This is one reason why the common idion for default values looks like this:

 def func(s=None):
   if s is None:
 s = ... compute default here ...

Of course, the default is often a constant or some value computed earlier.

Cheers,
Cameron Simpson 

I think... Therefore I ride.  I ride... Therefore I am.
   - Mark Pope 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

2015-04-07 Thread boB Stepp
On Mon, Apr 6, 2015 at 2:42 PM, Dave Angel  wrote:
> On 04/06/2015 03:20 PM, Emile van Sebille wrote:
>>
>> On 4/6/2015 7:54 AM, boB Stepp wrote:
>>>

[...]

>>
>> Maybe this form helps:
>>
>> Python 2.7.6 (default, Mar 22 2014, 22:59:56)
>> [GCC 4.8.2] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>  >>> d = {'a':'123'}
>>  >>> def func(s=d['a']):
>> ...   print s
>> ...
>>  >>> func()
>> 123
>>
>
> Only if you know that nobody is going to be changing d.
>
 d = {"a":"123"}
 def func(s=d["a"]):
> ... print s
> ...
 d["a"] = "new value"
 func()
> 123

Despite Mark's warning, I feel I must see if I understand what is going on here.

Switching to Py 3.4 since I am now at home:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600
64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> d = {'a': '123'}
>>> def func(s=d['a']):
  print(s)
  print(d['a'])

>>> func()
123
123
>>> d['a'] = 'new value'
>>> func()
123
new value

I added an additional print to the function to show the dictionary
entry's behavior.

First, my current understanding is that this form of the function does
not object to the presence of d['a'] in its parameter list because s
is the real parameter, d['a'] is its default value, but s is not
actually evaluated until run time.

But once s *is* evaluated, it stores a reference to the original
object, '123'. Changing d['a'] outside the function to a new value
does not alter the fact that s is storing the very same reference to
'123'. After reassigning d['a'] to point to the new object 'new
value', a new call to func() shows s still referencing the original
object and d['a'] referencing the new object. Is my comprehension of
these details correct? If yes, this is why I must constantly remind
myself that identifiers store references to objects, and that some
objects are mutable and some aren't, and these Python facts of life
are constantly challenging my old FORTRAN <= 77 ways of thinking...
~(:>))

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


Re: [Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

2015-04-07 Thread boB Stepp
On Mon, Apr 6, 2015 at 12:54 PM, Dave Angel  wrote:
> On 04/06/2015 12:43 PM, boB Stepp wrote:
>
>>
>> I was breaking down longer functions into smaller ones. Along the way
>> I noticed I was passing an entire dictionary from one function to
>> another. I only needed to pass one particular value, not the whole
>> dictionary, so that is how I got into the issue I asked about.
>
>
> Just to reinforce something you probably know well, passing a dictionary
> takes no more memory or time than passing an item from that dictionary...

One thing about Python that I must keep reminding myself is that its
identifiers store references to objects, not the actual objects
themselves.

> ... The
> real choice is whether the called function should dealing with a single item
> or with a dictionary.  It would have a different name in each case, and a
> different set of reuse possibilities.

In my actual code, I am trying to take advantage of these ideas.

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


Re: [Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

2015-04-07 Thread Emile van Sebille

On 4/6/2015 12:42 PM, Dave Angel wrote:

On 04/06/2015 03:20 PM, Emile van Sebille wrote:



Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> d = {'a':'123'}
 >>> def func(s=d['a']):
...   print s
...
 >>> func()
123



Only if you know that nobody is going to be changing d.


Clearly my example avoids the pitfalls of a changing d.  :)


 >>> d = {"a":"123"}
 >>> def func(s=d["a"]):
... print s
...
 >>> d["a"] = "new value"
 >>> func()
123


Good point -- I forgot about setting default parameters at compile time.

>>> d={'a':'123'}
>>> def func(s=d):print s['a']
...
>>> func()
123
>>>
>>> d['a']='456'
>>> func()
456
>>>



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


Re: [Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

2015-04-06 Thread Mark Lawrence

On 06/04/2015 20:20, Emile van Sebille wrote:

On 4/6/2015 7:54 AM, boB Stepp wrote:

Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

d = {'n': 'Print me!'}
d

{'n': 'Print me!'}

d['n']

'Print me!'

def func(d['n']):

SyntaxError: invalid syntax

def func(d):

 print d['n']


func(d)

Print me!

The plain text does not show it, but in the invalid syntax the "[" is
highlighted red.

Why is it invalid syntax to pass a particular dictionary value in a
function? Or does it require a different form to do so?


Maybe this form helps:

Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> d = {'a':'123'}
 >>> def func(s=d['a']):
...   print s
...
 >>> func()
123

Emile



Dreadful advice.  There have always have been and always will be 
questions from newbies as they do not understand how this construct 
works in Python.  Please *DO NOT* put such things forward.


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

Mark Lawrence

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


Re: [Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

2015-04-06 Thread Dave Angel

On 04/06/2015 03:20 PM, Emile van Sebille wrote:

On 4/6/2015 7:54 AM, boB Stepp wrote:

Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

d = {'n': 'Print me!'}
d

{'n': 'Print me!'}

d['n']

'Print me!'

def func(d['n']):

SyntaxError: invalid syntax

def func(d):

 print d['n']


func(d)

Print me!

The plain text does not show it, but in the invalid syntax the "[" is
highlighted red.

Why is it invalid syntax to pass a particular dictionary value in a
function? Or does it require a different form to do so?


Maybe this form helps:

Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> d = {'a':'123'}
 >>> def func(s=d['a']):
...   print s
...
 >>> func()
123



Only if you know that nobody is going to be changing d.

>>> d = {"a":"123"}
>>> def func(s=d["a"]):
... print s
...
>>> d["a"] = "new value"
>>> func()
123
>>>


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


Re: [Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

2015-04-06 Thread Emile van Sebille

On 4/6/2015 7:54 AM, boB Stepp wrote:

Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

d = {'n': 'Print me!'}
d

{'n': 'Print me!'}

d['n']

'Print me!'

def func(d['n']):

SyntaxError: invalid syntax

def func(d):

 print d['n']


func(d)

Print me!

The plain text does not show it, but in the invalid syntax the "[" is
highlighted red.

Why is it invalid syntax to pass a particular dictionary value in a
function? Or does it require a different form to do so?


Maybe this form helps:

Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {'a':'123'}
>>> def func(s=d['a']):
...   print s
...
>>> func()
123

Emile



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


Re: [Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

2015-04-06 Thread Dave Angel

On 04/06/2015 12:43 PM, boB Stepp wrote:



I was breaking down longer functions into smaller ones. Along the way
I noticed I was passing an entire dictionary from one function to
another. I only needed to pass one particular value, not the whole
dictionary, so that is how I got into the issue I asked about.


Just to reinforce something you probably know well, passing a dictionary 
takes no more memory or time than passing an item from that dictionary. 
 The real choice is whether the called function should dealing with a 
single item or with a dictionary.  It would have a different name in 
each case, and a different set of reuse possibilities.


I know the following example abuses the dictionary, using it as though 
it were an instance of class Person.  It's just what popped into my head.


def check_person(person):
if person["name"] in list_of_preferred:
 do_something...
 return True
return False

def check_name(name):
if name in list_of_preferred:
 do_something...
 return True
return False

In the first case, the function would be able use other elements of the 
dictionary, like "email_address" or "phone_number".


In the second case, you could call the function from someplace that has 
only names, and isn't worried about what dict the name might be part of.



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


Re: [Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

2015-04-06 Thread boB Stepp
Thanks, Joel! Thanks, Dave!

On Mon, Apr 6, 2015 at 11:31 AM, Dave Angel  wrote:

[...]

> Now, it's possible that what you're trying to do is something that can be
> accomplished some other way.  So please elaborate on your purpose in using
> the syntax you did.  Or supply a small program that shows a function being
> defined and called, that would give meaning to the syntax you're trying.

I was breaking down longer functions into smaller ones. Along the way
I noticed I was passing an entire dictionary from one function to
another. I only needed to pass one particular value, not the whole
dictionary, so that is how I got into the issue I asked about. Once
you and Joel responded it was *obvious*. A bunch of years ago, it
would have been *obvious* and I never would have asked the question in
the first place. This is easy enough to correct now that I realize
what I was doing.

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


Re: [Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

2015-04-06 Thread Dave Angel

On 04/06/2015 10:54 AM, boB Stepp wrote:

Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

d = {'n': 'Print me!'}
d

{'n': 'Print me!'}

d['n']

'Print me!'

def func(d['n']):

SyntaxError: invalid syntax

def func(d):

 print d['n']


func(d)

Print me!

The plain text does not show it, but in the invalid syntax the "[" is
highlighted red.

Why is it invalid syntax to pass a particular dictionary value in a
function? Or does it require a different form to do so?


You're getting confused between defining a function and calling one.

For Python 2.7, see:

https://docs.python.org/2/reference/compound_stmts.html#function-definitions


When defining a function, you provide (not pass) the formal parameters, 
and they must be simple names.  Those names will end up being local 
variables when the function is finally called.


(The only sort-of exception to this in 2.7 is when you define a default 
argument to a function.  In that case, the parameter still must be a 
valid python variable name, but the default value can be an arbitrary 
expression, as long as it's valid at the time of function definition.)



Once the function is being called, then you can use an arbitrary 
expression for your argument.  The results of that expression is bound 
to the formal parameter specified above.


Further reading:
https://docs.python.org/2/faq/programming.html#faq-argument-vs-parameter
https://docs.python.org/2/glossary.html#term-parameter
https://docs.python.org/2/glossary.html#term-argument


Now, it's possible that what you're trying to do is something that can 
be accomplished some other way.  So please elaborate on your purpose in 
using the syntax you did.  Or supply a small program that shows a 
function being defined and called, that would give meaning to the syntax 
you're trying.

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


Re: [Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

2015-04-06 Thread Joel Goldstick
On Mon, Apr 6, 2015 at 11:20 AM, Joel Goldstick
 wrote:
> On Mon, Apr 6, 2015 at 10:54 AM, boB Stepp  wrote:
>> Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit
>> (Intel)] on win32
>> Type "copyright", "credits" or "license()" for more information.
> d = {'n': 'Print me!'}
> d
>> {'n': 'Print me!'}
> d['n']
>> 'Print me!'
> def func(d['n']):
>> SyntaxError: invalid syntax
> def func(d):
>> print d['n']
>>
> func(d)
>> Print me!
>>
>> The plain text does not show it, but in the invalid syntax the "[" is
>> highlighted red.
>>
>> Why is it invalid syntax to pass a particular dictionary value in a
>> function? Or does it require a different form to do so?
>>
>
> Here is another example:
>
 def f(6):
>   File "", line 1
> def f(6):
>   ^
> SyntaxError: invalid syntax

>
> You can't pass a value as a parameter to a  function definition.  You
> need to provide a name.  The actual value is supplied when you call
> the function

The python.org site has this:
https://docs.python.org/2/reference/compound_stmts.html#function-definitions

>
>> Thanks!
>>
>> --
>> boB
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>
>
>
> --
> Joel Goldstick
> http://joelgoldstick.com



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


Re: [Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

2015-04-06 Thread Joel Goldstick
On Mon, Apr 6, 2015 at 10:54 AM, boB Stepp  wrote:
> Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "copyright", "credits" or "license()" for more information.
 d = {'n': 'Print me!'}
 d
> {'n': 'Print me!'}
 d['n']
> 'Print me!'
 def func(d['n']):
> SyntaxError: invalid syntax
 def func(d):
> print d['n']
>
 func(d)
> Print me!
>
> The plain text does not show it, but in the invalid syntax the "[" is
> highlighted red.
>
> Why is it invalid syntax to pass a particular dictionary value in a
> function? Or does it require a different form to do so?
>

Here is another example:

>>> def f(6):
  File "", line 1
def f(6):
  ^
SyntaxError: invalid syntax
>>>

You can't pass a value as a parameter to a  function definition.  You
need to provide a name.  The actual value is supplied when you call
the function

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



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


Re: [Tutor] Why is it not working?

2015-02-04 Thread boB Stepp
On Wed, Feb 4, 2015 at 6:12 PM, Antonio Zagheni
 wrote:
> Hi there,
>
> I am a begginer in python and I'm trying to learn something about Tkinter.
>
> I did a game (the code is below) using Tkinter were two players have to fill 
> a row, a column or a diagonal with either 'X' or 'O'.
> When it happens, the code was supposed to recognize the winner ('Jogador 1 or 
> 2 vence!' --> translating from portuguese means 'player 1 or 2 wins!').
>
> The problem is that sometimes the code doesn't recognize the winner and I 
> can't find the problem.
>
> I don't have any error message.
>
> I belive that the problem is related with the fact that after clicking a 
> button it calls a function an inside that function another function is called 
> (analise()), but I am not sure...
>
> If somebody could take a look and give me some tips to solve the problem I 
> will apreciate a lot.
>
> The problem occurs either using Windows 7 or Ubuntu 14.04 in a 64 bit 
> computer, both running idle with python's version 2.7.7.

It has been mentioned here in the past that one should not run a
program using Tkinter from within IDLE as IDLE itself is implemented
using Tkinter. Just to eliminate one other variable from your
troubleshooting you might ensure you start your program from the
command line and not from within IDLE.

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


Re: [Tutor] Why is it not working?

2015-02-04 Thread Alan Gauld

On 05/02/15 00:12, Antonio Zagheni wrote:


from Tkinter import *

...


jogador = 1
rodada = 1
fim = False


Notice that fim is declared here as a global variable


def analise():
 global jogador, rodada


But you do not include fim here


 print flag_botaoA1, flag_botaoA2, flag_botaoA3
 print flag_botaoB1, flag_botaoB2, flag_botaoB3
 print flag_botaoC1, flag_botaoC2, flag_botaoC3
 print jogador, rodada
 if rodada == 9:
 active_player.set('Game Over!')
 fim = True


So when you set fim here you are setting a local variable
only visible inside the analise() function.

You need to make fim global.

That's the only thing I can see although not speaking Portuguese
makes it harder to check the logic.
You have a lot of repetitive code there that could be
made much simpler. But that's a mail for another time.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Why is it...

2007-03-23 Thread Jay Mutter III

Got it - it needs the blank line to signal that code block has ended.
Thanks

On Mar 22, 2007, at 3:05 PM, Jason Massey wrote:


In the interpreter this doesn't work:

>>> f = open(r"c:\python24\image.dat")
>>> line = f.readline()
>>> while line:
... line = f.readline()
... f.close()
Traceback (  File "", line 3
f.close()
^
SyntaxError: invalid syntax

But this does:

>>> f = open(r"c:\python24\image.dat")
>>> line = f.readline()
>>> while line:
... line = f.readline()
...
>>> f.close()
>>>

Note the differing placement of the f.close() statement, it's not  
part of the while.



On 3/22/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
Jay Mutter III wrote:
> Why is it that when I run the following interactively
>
> f = open('Patents-1920.txt')
> line = f.readline()
> while line:
>  print line,
>  line = f.readline()
> f.close()
>
> I get an error message
>
> File "", line 4
>  f.close()
>  ^
> SyntaxError: invalid syntax
>
> but if i run it in a script there is no error?

Can you copy/paste the actual console transcript?

BTW a better way to write this is
f = open(...)
for line in f:
 print line,
f.close()

Kent

>
> Thanks
>
> Jay
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is it...

2007-03-22 Thread Terry Carroll
On Thu, 22 Mar 2007, Jason Massey wrote:

> In the interpreter this doesn't work:
> 
> >>> f = open(r"c:\python24\image.dat")
> >>> line = f.readline()
> >>> while line:
> ... line = f.readline()
> ... f.close()
> Traceback (  File "", line 3
> f.close()
> ^
> SyntaxError: invalid syntax

The interactive interpreter was expecting a blank line to end the suite of
the compound statement.  Without the blank line, it interpreted the close
statement to be part of the suite;  and since it's not indented as the
suite should be, it's a syntax error.

The blank line requirement is only in interactive sessions:

  Note that a (top-level) compound statement must be followed by a 
  blank line in interactive mode; this is needed to help the parser
  detect the end of the input.

 http://docs.python.org/ref/interactive.html

> But this does:
> 
> >>> f = open(r"c:\python24\image.dat")
> >>> line = f.readline()
> >>> while line:
> ... line = f.readline()
> ...
> >>> f.close()
> >>>
> 
> Note the differing placement of the f.close() statement, it's not part of
> the while.

The difference is that this time you gave it the blank line to end the 
suite.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is it...

2007-03-22 Thread Jason Massey

In the interpreter this doesn't work:


f = open(r"c:\python24\image.dat")
line = f.readline()
while line:

... line = f.readline()
... f.close()
Traceback (  File "", line 3
   f.close()
   ^
SyntaxError: invalid syntax

But this does:


f = open(r"c:\python24\image.dat")
line = f.readline()
while line:

... line = f.readline()
...

f.close()



Note the differing placement of the f.close() statement, it's not part of
the while.


On 3/22/07, Kent Johnson <[EMAIL PROTECTED]> wrote:


Jay Mutter III wrote:
> Why is it that when I run the following interactively
>
> f = open('Patents-1920.txt')
> line = f.readline()
> while line:
>  print line,
>  line = f.readline()
> f.close()
>
> I get an error message
>
> File "", line 4
>  f.close()
>  ^
> SyntaxError: invalid syntax
>
> but if i run it in a script there is no error?

Can you copy/paste the actual console transcript?

BTW a better way to write this is
f = open(...)
for line in f:
 print line,
f.close()

Kent

>
> Thanks
>
> Jay
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is it...

2007-03-22 Thread Kent Johnson
Jay Mutter III wrote:
> Why is it that when I run the following interactively
> 
> f = open('Patents-1920.txt')
> line = f.readline()
> while line:
>  print line,
>  line = f.readline()
> f.close()
> 
> I get an error message
> 
> File "", line 4
>  f.close()
>  ^
> SyntaxError: invalid syntax
> 
> but if i run it in a script there is no error?

Can you copy/paste the actual console transcript?

BTW a better way to write this is
f = open(...)
for line in f:
 print line,
f.close()

Kent

> 
> Thanks
> 
> Jay
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor