Commit 82e01e1c authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Fixed an indentation issue and updated a paragraph on the usage of isinstance().

parent abb08286
......@@ -129,7 +129,7 @@ which requires the use of a local variable and performs a type test on assignmen
If you *know* the return value of :meth:`quest` will be of type :class:`Shrubbery`
you can use a cast to write::
print((<Shrubbery>quest()).width)
print( (<Shrubbery>quest()).width )
This may be dangerous if :meth:`quest()` is not actually a :class:`Shrubbery`, as it
will try to access width as a C struct member which may not exist. At the C level,
......@@ -137,21 +137,17 @@ rather than raising an :class:`AttributeError`, either an nonsensical result wil
returned (interpreting whatever data is at that address as an int) or a segfault
may result from trying to access invalid memory. Instead, one can write::
print((<Shrubbery?>quest()).width)
print( (<Shrubbery?>quest()).width )
which performs a type check (possibly raising a :class:`TypeError`) before making the
cast and allowing the code to proceed.
To explicitly test the type of an object, use the :meth:`isinstance` method. By default,
in Python, the :meth:`isinstance` method checks the :class:`__class__` attribute of the
first argument to determine if it is of the required type. However, this is potentially
unsafe as the :class:`__class__` attribute can be spoofed or changed, but the C structure
of an extension type must be correct to access its :keyword:`cdef` attributes and call its :keyword:`cdef` methods. Cython detects if the second argument is a known extension
type and does a type check instead, analogous to Pyrex's :meth:`typecheck`.
The old behavior is always available by passing a tuple as the second parameter::
print(isinstance(sh, Shrubbery)) # Check the type of sh
print(isinstance(sh, (Shrubbery,))) # Check sh.__class__
To explicitly test the type of an object, use the :meth:`isinstance` method.
For known builtin or extension types, Cython translates these into a
fast and safe type check that ignores changes to
the object's ``__class__`` attribute etc., so that after a successful
:meth:`isinstance` test, code can rely on the expected C structure of the
extension type and its :keyword:`cdef` attributes and methods.
Extension types and None
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment