Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
8c6762d2
Commit
8c6762d2
authored
Oct 06, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
explain basestring type in string tutorial
parent
25d2d49e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
12 deletions
+28
-12
docs/src/tutorial/strings.rst
docs/src/tutorial/strings.rst
+28
-12
No files found.
docs/src/tutorial/strings.rst
View file @
8c6762d2
...
@@ -16,18 +16,23 @@ implicitly insert these encoding/decoding steps.
...
@@ -16,18 +16,23 @@ implicitly insert these encoding/decoding steps.
Python string types in Cython code
Python string types in Cython code
----------------------------------
----------------------------------
Cython supports three Python string types: ``bytes``, ``str``
Cython supports four Python string types: ``bytes``, ``str``,
and ``unicode``. The ``str`` type is special in that it is the
``unicode`` and ``basestring``. The ``bytes`` and ``unicode`` types
byte string in Python 2 and the Unicode string in Python 3 (for Cython
are the specific types known from normal Python 2.x (named ``bytes``
code compiled with language level 2, i.e. the default). Thus, in Python
and ``str`` in Python 3).
2, both ``bytes`` and ``str`` represent the byte string type,
whereas in Python 3, ``str`` and ``unicode`` represent the Python
The ``str`` type is special in that it is the byte string in Python 2
Unicode string type. The switch is made at C compile time, the Python
and the Unicode string in Python 3 (for Cython code compiled with
version that is used to run Cython is not relevant.
language level 2, i.e. the default). Meaning, it always corresponds
exactly with the type that the Python runtime itself calls ``str``.
When compiling Cython code with language level 3, the ``str`` type
Thus, in Python 2, both ``bytes`` and ``str`` represent the byte string
is identified with exactly the Unicode string type at Cython compile time,
type, whereas in Python 3, both ``str`` and ``unicode`` represent the
i.e. it no does not identify with ``bytes`` when running in Python 2.
Python Unicode string type. The switch is made at C compile time, the
Python version that is used to run Cython is not relevant.
When compiling Cython code with language level 3, the ``str`` type is
identified with exactly the Unicode string type at Cython compile time,
i.e. it does not identify with ``bytes`` when running in Python 2.
Note that the ``str`` type is not compatible with the ``unicode``
Note that the ``str`` type is not compatible with the ``unicode``
type in Python 2, i.e. you cannot assign a Unicode string to a variable
type in Python 2, i.e. you cannot assign a Unicode string to a variable
...
@@ -40,6 +45,17 @@ and users normally expect code to be able to work with both. Code that
...
@@ -40,6 +45,17 @@ and users normally expect code to be able to work with both. Code that
only targets Python 3 can safely type variables and arguments as either
only targets Python 3 can safely type variables and arguments as either
``bytes`` or ``unicode``.
``bytes`` or ``unicode``.
The ``basestring`` type represents both the types ``str`` and ``unicode``,
i.e. all Python text string types in Python 2 and Python 3. This can be
used for typing text variables that normally contain Unicode text (at
least in Python 3) but must additionally accept the ``str`` type in
Python 2 for backwards compatibility reasons. It is not compatible with
the ``bytes`` type. Its usage should be rare in normal Cython code as
the generic ``object`` type (i.e. untyped code) will normally be good
enough and has the additional advantage of supporting the assignment of
string subtypes. Support for the ``basestring`` type is new in Cython
0.20.
General notes about C strings
General notes about C strings
-----------------------------
-----------------------------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment