Commit bd25194d authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2414 from gabrieldemarmiesse/test_language_basics_4

Added tests for "language basics" part 4
parents 15205ce9 2c739e07
cdef class A:
cdef foo(self)
cdef class B(A):
cdef foo(self, x=*)
cdef class C(B):
cpdef foo(self, x=*, int k=*)
from __future__ import print_function
cdef class A:
cdef foo(self):
print("A")
cdef class B(A):
cdef foo(self, x=None):
print("B", x)
cdef class C(B):
cpdef foo(self, x=True, int k=3):
print("C", x, k)
...@@ -300,35 +300,15 @@ To avoid repetition (and potential future inconsistencies), default argument val ...@@ -300,35 +300,15 @@ To avoid repetition (and potential future inconsistencies), default argument val
not visible in the declaration (in ``.pxd`` files) but only in not visible in the declaration (in ``.pxd`` files) but only in
the implementation (in ``.pyx`` files). the implementation (in ``.pyx`` files).
When in a ``.pyx`` file, the signature is the same as it is in Python itself:: When in a ``.pyx`` file, the signature is the same as it is in Python itself:
from __future__ import print_function
cdef class A:
cdef foo(self):
print("A")
cdef class B(A):
cdef foo(self, x=None):
print("B", x)
cdef class C(B):
cpdef foo(self, x=True, int k=3):
print("C", x, k)
.. literalinclude:: ../../examples/userguide/language_basics/optional_subclassing.pyx
When in a ``.pxd`` file, the signature is different like this example: ``cdef foo(x=*)``. When in a ``.pxd`` file, the signature is different like this example: ``cdef foo(x=*)``.
This is because the program calling the function just needs to know what signatures are This is because the program calling the function just needs to know what signatures are
possible in C, but doesn't need to know the value of the default arguments.:: possible in C, but doesn't need to know the value of the default arguments.:
cdef class A:
cdef foo(self)
cdef class B(A):
cdef foo(self, x=*)
cdef class C(B):
cpdef foo(self, x=*, int k=*)
.. literalinclude:: ../../examples/userguide/language_basics/optional_subclassing.pxd
.. note:: .. note::
The number of arguments may increase when subclassing, The number of arguments may increase when subclassing,
...@@ -481,21 +461,9 @@ Overriding in extension types ...@@ -481,21 +461,9 @@ Overriding in extension types
----------------------------- -----------------------------
``cpdef`` methods can override ``cdef`` methods:: ``cpdef`` methods can override ``cdef`` methods:
from __future__ import print_function
cdef class A:
cdef foo(self):
print("A")
cdef class B(A):
cdef foo(self, x=None):
print("B", x)
cdef class C(B): .. literalinclude:: ../../examples/userguide/language_basics/optional_subclassing.pyx
cpdef foo(self, x=True, int k=3):
print("C", x, k)
When subclassing an extension type with a Python class, When subclassing an extension type with a Python class,
``def`` methods can override ``cpdef`` methods but not ``cdef`` ``def`` methods can override ``cpdef`` methods but not ``cdef``
......
...@@ -261,26 +261,13 @@ remain the same. There is a slight performance penalty in some cases when a ...@@ -261,26 +261,13 @@ remain the same. There is a slight performance penalty in some cases when a
cdef/cpdef function without any optional is overridden with one that does have cdef/cpdef function without any optional is overridden with one that does have
default argument values. default argument values.
For example, one can have the ``.pxd`` file:: For example, one can have the ``.pxd`` file:
cdef class A: .. literalinclude:: ../../examples/userguide/language_basics/optional_subclassing.pxd
cdef foo(self)
cdef class B(A)
cdef foo(self, x=*)
cdef class C(B):
cpdef foo(self, x=*, int k=*)
with corresponding ``.pyx`` file:: with corresponding ``.pyx`` file:
cdef class A: .. literalinclude:: ../../examples/userguide/language_basics/optional_subclassing.pyx
cdef foo(self):
print "A"
cdef class B(A)
cdef foo(self, x=None)
print "B", x
cdef class C(B):
cpdef foo(self, x=True, int k=3)
print "C", x, k
.. note:: .. note::
......
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