Re: [Tutor] How to write a function which reads files

2018-08-08 Thread boB Stepp
On Wed, Aug 8, 2018 at 8:30 PM boB Stepp  wrote:
>
> On Tue, Aug 7, 2018 at 9:13 AM Rafael Knuth  wrote:

Curses!  Sorry, Chris!  This should be:

> > Chris Warrick wrote:
> > > Also, consider using snake_case instead of PascalCase for your
> > > function name, since the latter is typically used for classes, and
> > > perhaps call it read_file to better describe it?
> >
> > thanks, I wasn't aware of the naming conventions for functions and classes.
> > will bear that in mind!
>
> Have you had a look at PEP 8 yet?  It covers most of the Python
> stylistic conventions, particularly if you intend to contribute to
> Python and its standard libraries.  It may be found at:
> https://www.python.org/dev/peps/pep-0008/

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


Re: [Tutor] How to write a function which reads files

2018-08-08 Thread boB Stepp
On Tue, Aug 7, 2018 at 9:13 AM Rafael Knuth  wrote:

> Alan Gauld wrote:
> > Also, consider using snake_case instead of PascalCase for your
> > function name, since the latter is typically used for classes, and
> > perhaps call it read_file to better describe it?
>
> thanks, I wasn't aware of the naming conventions for functions and classes.
> will bear that in mind!

Have you had a look at PEP 8 yet?  It covers most of the Python
stylistic conventions, particularly if you intend to contribute to
Python and its standard libraries.  It may be found at:
https://www.python.org/dev/peps/pep-0008/



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


Re: [Tutor] How to write a function which reads files

2018-08-07 Thread Mats Wichmann



Question is already answered, just wanted to add a mini-note.


def FileReader(file_path):with open(file_path) as file_object:
 contents = file_object.readreturn contents
you /can/ return the read method here, which is what this typo does. And
the caller of the function can use it to read, as in:

print(FileReader("...path...")())

that is, since FileReader as originally written returns a callable, you
can call it.

However in this case, since the use of the file context manager (open
using "with" statement) does what it's supposed to, the read will fail,
and you get:

ValueError: I/O operation on closed file.

because as soon as the with statement is exited, things are cleaned up.
That's what context managers do.  That doesn't mean there are not cases
when you will want to return a function/method for use elsewhere, it's
part of the "Python is powerful because functions are first-class"
argument :)


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


Re: [Tutor] How to write a function which reads files

2018-08-07 Thread Alan Gauld via Tutor
On 07/08/18 13:46, Rafael Knuth wrote:

> Now I want to convert the code above into a function.
> This is what I wrote:
> 
> def FileReader(file_path):
> with open(file_path) as file_object:
> contents = file_object.read
> return contents
> 
> print(FilePrinter("C:\\Users\\...\\MyFile.txt")) # path shortened for

Note that you used a different name here than in your
function definition!

I'm guessing that's a typo, but its one reason to always
cut 'n paste real code into your emails.

Also as a matter of style its usual in Python to make
function names start with a lower case letter:

def myFunction():

or

def my_function():

But that's just a community convention.

But Chris has answered your original question, you
need to add calling parens.

-- 
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] How to write a function which reads files

2018-08-07 Thread Chris Warrick
On Tue, 7 Aug 2018 at 15:07, Rafael Knuth  wrote:
> def FileReader(file_path):
> with open(file_path) as file_object:
> contents = file_object.read
> return contents
>
> print(FilePrinter("C:\\Users\\...\\MyFile.txt")) # path shortened for
> better readability
>
> I got this error message:
>
> 

You forgot the parentheses (), and are returning a reference to the
function instead of calling it and returning its result. Do this:
contents = file_object.read()

Also, consider using snake_case instead of PascalCase for your
function name, since the latter is typically used for classes, and
perhaps call it read_file to better describe it?

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