[Tex/LaTex] Correctly typesetting words surrounded by double underscores


Forgive me if the answer to this is obvious, I've just begun working with LaTex and I've had no luck with Google.

I'm working on learning LaTeX, and as part of my practice I'm rewriting a guide on special methods in Python in LaTeX. As you might know, Python's special methods take on the form __methodname__. This causes problems when I try to generate a PDF from my .tex source file using pdflatex. How can I make LaTeX ignore the double underscores, or otherwise make my output correct?

In case it might help, here's a sample of what I'm trying to typeset:

 e.g. \texttt{__init__} or \texttt{__lt__}

Which I want to look like "e.g. __init__ or __lt__".

Best Answer

Use \texttt{\_\_init\_\_} or \verb+__init__+ or with the inline options of the listings package

% Ensure that the minus sign is the "-" character in listings
% for cut and paste operations from pdf docs

\lstset{language         =Python,
        showstringspaces =false,
        keepspaces       =true,
        basicstyle       = \raggedright\verbfont\small\selectfont}

    \lstinline[basicstyle = \verbfont\raggedright]}

Then you can use \li{__init__}