Welcome, guest | Sign In | My Account | Store | Cart

Because I did not want to expose my passwords in plain text whenever an exception was thrown or inside the debugger, I wrote this simple snippet. It's very simple, but can be quite useful nonetheless...

Python, 3 lines
class passwd(str):
	def __repr__(self):
		return repr('*' * self.__len__())

To demonstrate its use, here some examples:

>>> astr = 'aString'
>>> astr
>>> print astr
>>> apwd = passwd('aPassword')
>>> apwd
>>> apwd == 'aPassword'
>>> print apwd

More important, the password string will not immediately show up in your debugger in plain text or in exception frames, since __repr__ is used for those cases.


Gabriel Genellina 12 years, 2 months ago  # | flag

Nice! Although I'd write it as:

class passwd(str):
    def __repr__(self):
        return repr('*' * len(self))
david.gaarenstroom (author) 12 years, 2 months ago  # | flag

Yep, that's a better version. I'll be using that one myself from now on... Thanks!

niklas a 12 years ago  # | flag

I like the simplicity of the code. Very useful. However I don't see why you have to reveal the length of your password. Why not use 8 * '*' for example.

Created by david.gaarenstroom on Tue, 15 Sep 2009 (MIT)
Python recipes (4591)
david.gaarenstroom's recipes (3)

Required Modules

  • (none specified)

Other Information and Tasks