What are the internals of Pythons str.join()? (Hiding passwords from output) : <p>I just stumbled upon an interesting(?) way to hide passwords (and other personal data) from general output from screen to logfiles.</p>

<p>In his book <a href="http://www.oreilly.com/programming/free/files/how-to-make-mistakes-in-python.pdf" rel="nofollow noreferrer">How to make mistakes in Python</a> Mike Pirnat suggests to implement a class for sensitive strings and to overload its <code>__str__</code>- and <code>__repr__</code>-methods.</p>

<p>I experimented with that and got this:</p>



<pre class="lang-py prettyprint-override"><code>class secret(str):

    def __init__(self, s):
        self.string = s

    def __repr__(self):
        return "'" + "R"*len(self.string) + "'"

    def __str__(self):
        return "S" * len(self.string)

    def __add__(self, other):
        return str.__add__(self.__str__(), other)

    def __radd__(self, other):
        return str.__add__(other, self.__str__())

    def __getslice__(self, i, j):
        return ("X"*len(self.string))[i:j]
</code></pre>

<p>(I'm aware that using <code>len</code> provides information about the content to hide. It's just for testing.)</p>

<p>It works fine in this cases:</p>

<pre class="lang-py prettyprint-override"><code>pwd = secret("nothidden")

print("The passwort is " + pwd)                  # The passwort is SSSSSSSSS
print(pwd + " is the passwort.")                 # SSSSSSSSS is the password.

print("The passwort is {}.".format(pwd))         # The password is SSSSSSSSS.
print(["The", "passwort", "is", pwd])            # ['The', 'password', 'is', 'RRRRRRRRR']
print(pwd[:])                                    # XXXXXXXXX
</code></pre>

<p>However this does not work:</p>

<pre class="lang-py prettyprint-override"><code>print(" ".join(["The", "password", "is", pwd]))  # The password is nothidden
</code></pre>

<p>So, how does str.join() work internally? Which method would I have to overload to obscure the string?</p>
