LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2018, 12:43 PM   #1
vysero
Member
 
Registered: May 2018
Posts: 137

Rep: Reputation: Disabled
Help with understanding the 'with' keyword and understanding file reading and writing.


I am trying to learn the details behind some basic functions in Python (kinda new) so any help would be greatly appreciated. Let's say I have the following code:

Code:
def SomeConversion(file):
  _content = None
  with open(file, 'rb') as inputVar:
I have a few questions regarding this code.

1) Should content be called _content, __content or just content? Does this change depending on the context its being used in. For instance, if I only ever use the variable inside of the SomeConversions method.

2)Should None be changed to "" or does it matter? Are there other options and what is the difference between the different options?

3) Is the file being named as "file" being saved into a variable called inputVar? If so, is Python just assuming the type of inputVar? If so how does it know what to assume?

4) Does the keyword 'as' always need to be used when using the keyword 'with' or are these two keywords actually completely separates entities?

I will continue investigating on my own and if I end up figuring these answers out I will come back and edit this post. I am new to Python btw, did I say that already? XD
 
Old 05-30-2018, 01:58 PM   #2
ttk
Senior Member
 
Registered: May 2012
Location: Sebastopol, CA
Distribution: Slackware64
Posts: 1,038
Blog Entries: 27

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by vysero View Post
1) Should content be called _content, __content or just content? Does this change depending on the context its being used in. For instance, if I only ever use the variable inside of the SomeConversions method.
Unless you have previously declared content as global scope, you should just call it "content". PEP8 has a section on the expected meanings of underscores before labels, and it does not mention using them to signal a local scope variable:

https://legacy.python.org/dev/peps/p...-naming-styles

Quote:
2) Should None be changed to "" or does it matter? Are there other options and what is the difference between the different options?
If you leave it as None, then if something unforeseen happens and content never gets assigned a value, subsequent attempts to use it as a string will throw an exception. In Python this is the preferred behavior. Initializing it to an empty string would avoid such exceptions, which would have the effect of hiding problems in your program.

Quote:
3) Is the file being named as "file" being saved into a variable called inputVar? If so, is Python just assuming the type of inputVar? If so how does it know what to assume?
It is being assigned the value returned by open(). The only assumptions it makes about this returned value is that it implements some methods (like __exit__, which it invokes before leaving the "with" statement's scope).

If you make your own function foo() which returns an object, "with foo() as bar:" will just assign that returned object to "bar".

Quote:
4) Does the keyword 'as' always need to be used when using the keyword 'with' or are these two keywords actually completely separates entities?
As far as I know they are to be used together.

Quote:
I will continue investigating on my own and if I end up figuring these answers out I will come back and edit this post. I am new to Python btw, did I say that already? XD
Good questions :-) Good luck!
 
1 members found this post helpful.
Old 05-30-2018, 02:15 PM   #3
vysero
Member
 
Registered: May 2018
Posts: 137

Original Poster
Rep: Reputation: Disabled
Ah I see, thanks ttk! One more follow up question. Which of these two statements is the correct method of reading the contents:

Code:
with open(file, 'rb') as inputVar.read()
or..

Code:
with open(file, 'rb') as inputVar:
  content = inputVar.read()
PyDev does not seem to have a problem with either of them; however, one seems more compact. Am I loosing any functionality by choosing to use the first method?
 
Old 05-30-2018, 02:37 PM   #4
ttk
Senior Member
 
Registered: May 2012
Location: Sebastopol, CA
Distribution: Slackware64
Posts: 1,038
Blog Entries: 27

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by vysero View Post
Ah I see, thanks ttk! One more follow up question. Which of these two statements is the correct method of reading the contents:

Code:
with open(file, 'rb') as inputVar.read()
or..

Code:
with open(file, 'rb') as inputVar:
  content = inputVar.read()
PyDev does not seem to have a problem with either of them; however, one seems more compact. Am I loosing any functionality by choosing to use the first method?
I'm surprised that worked at all. In Python 2 on my system it's a syntax error. Your second code example is the expected usage, so other developers will be least confused if you follow that form.

Relevant: https://en.wikipedia.org/wiki/Princi...least_surprise
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Understanding file permissions NotionCommotion Linux - Newbie 2 11-30-2017 04:35 PM
Understanding File Permissions amirC Linux - Newbie 3 11-28-2013 01:42 AM
Understanding File System rupeshkp728 Linux - General 3 07-17-2012 09:48 PM
Understanding the file type soumya1 Linux - Newbie 4 09-13-2011 04:13 AM
Reading and understanding data send to USB device Dahoon Linux - Hardware 1 06-15-2009 11:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration