![]() |
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): 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 |
Quote:
https://legacy.python.org/dev/peps/p...-naming-styles Quote:
Quote:
If you make your own function foo() which returns an object, "with foo() as bar:" will just assign that returned object to "bar". Quote:
Quote:
|
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() Code:
with open(file, 'rb') as inputVar: |
Quote:
Relevant: https://en.wikipedia.org/wiki/Princi...least_surprise |
All times are GMT -5. The time now is 06:49 PM. |