Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9482d25

Browse files
committed
Update example for the type() function to use the currently accepted
preference of using "is" instead of "==" to compare types, use built-in names where available, and point to the isinstance() function. Closes SF bug #632196.
1 parent 57bc5fa commit 9482d25

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

Doc/lib/libfuncs.tex

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,13 +836,24 @@ \section{Built-in Functions \label{built-in-funcs}}
836836
Return the type of an \var{object}. The return value is a
837837
type\obindex{type} object. The standard module
838838
\module{types}\refstmodindex{types} defines names for all built-in
839-
types.
839+
types that don't already have built-in names.
840840
For instance:
841841

842842
\begin{verbatim}
843843
>>> import types
844-
>>> if type(x) == types.StringType: print "It's a string"
844+
>>> x = 'abc'
845+
>>> if type(x) is str: print "It's a string"
846+
...
847+
It's a string
848+
>>> def f(): pass
849+
...
850+
>>> if type(f) is types.FunctionType: print "It's a function"
851+
...
852+
It's a function
845853
\end{verbatim}
854+
855+
The \function{isinstance()} built-in function is recommended for
856+
testing the type of an object.
846857
\end{funcdesc}
847858

848859
\begin{funcdesc}{unichr}{i}

0 commit comments

Comments
 (0)