LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with understanding the 'with' keyword and understanding file reading and writing. (https://www.linuxquestions.org/questions/programming-9/help-with-understanding-the-with-keyword-and-understanding-file-reading-and-writing-4175630822/)

vysero 05-30-2018 12:43 PM

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

ttk 05-30-2018 01:58 PM

Quote:

Originally Posted by vysero (Post 5861358)
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!

vysero 05-30-2018 02:15 PM

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?

ttk 05-30-2018 02:37 PM

Quote:

Originally Posted by vysero (Post 5861389)
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


All times are GMT -5. The time now is 06:49 PM.