Note that there are some explanatory texts on larger screens.

plurals
  1. POPython generator objects and .join
    primarykey
    data
    text
    <p>Just a fundamental question regarding python and .join() method:</p> <pre><code>file1 = open(f1,"r") file2 = open(f2,"r") file3 = open("results","w") diff = difflib.Differ() result = diff.compare(file1.read(),file2.read()) file3.write("".join(result)), </code></pre> <p>The above snippet of code yields a nice output stored in a file called "results", in string format, showing the differences between the two files line-by-line. However I notice that if I just print "result" <strong><em>without</em></strong> using .join(), the compiler returns a message that includes a memory address. After trying to write the result to the file <strong><em>without</em></strong> using .join(), I was informed by the compiler that only strings and character buffers may be used in the .join() method, and not generator objects. So based off of all the evidence that I have adduced, please correct me if I am wrong:</p> <ol> <li><p><code>result = diff.compare(file1.read(),file2.read())</code> &lt;---- result is a generator object?</p></li> <li><p><code>result</code> is a list of strings, with <code>result</code> itself being the reference to the first string?</p></li> <li><p><code>.join()</code> takes a memory address and points to the first, and then iterates over the rest of the addresses of strings in that structure?</p></li> <li><p>A generator object is an object that returns a pointer?</p></li> </ol> <p>I apologize if my questions are unclear, but I basically wanted to ask the python veterans if my deductions were correct. My question is less about the observable results, and more so about the inner workings of python. I appreciate all of your help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload