File: CallByRef.rst

package info (click to toggle)
python-pytooling 8.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,564 kB
  • sloc: python: 23,883; makefile: 13
file content (98 lines) | stat: -rw-r--r-- 3,126 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
.. _COMMON/CallByRef:

CallByRef
#########

The :mod:`pyTooling.CallByRef` package contains auxiliary classes to implement *call-by-reference* emulation for
function parameter handover. The callee gets enabled to return out-parameters for simple types like :class:`bool` and
:class:`int` to the caller.

.. #contents:: Table of Contents
   :local:
   :depth: 2

By implementing a wrapper-class :class:`~pyTooling.CallByRef.CallByRefParam`, any type's value can be passed
by-reference. In addition, standard types like :class:`int` or :class:`bool` can be handled
by derived wrapper-classes.

.. admonition:: Python Background

   Python does not allow a user to distinguish between *call-by-value* and *call-by-reference*
   parameter passing. Python's standard types are passed by-value to a function or method.
   Instances of a class are passed by-reference (pointer) to a function or method.

.. rubric:: Inheritance diagram:

.. inheritance-diagram:: pyTooling.CallByRef
   :parts: 1

.. admonition:: Example

   .. code-block:: Python

      from pyTooling.CallByRef import CallByRefIntParam

      # define a call-by-reference parameter for integer values
      myInt = CallByRefIntParam(3)

      # a function using a call-by-reference parameter
      def func(param : CallByRefIntParam):
        param <<= param * 4

      # call the function and pass the wrapper object
      func(myInt)

      print(myInt.value)


.. _COMMON/CallByRefParam:

CallByRefParam
**************

:class:`~pyTooling.CallByRef.CallByRefParam` implements a wrapper class for an arbitrary *call-by-reference*
parameter that can be passed to a function or method.

The parameter can be initialized via the constructor. If no init-value was given,
the init value will be ``None``. The wrappers internal value can be updated by
using the inplace shift-left operator ``<=``.

In addition, operators ``=`` and ``!=`` are also implemented for any *call-by-reference*
wrapper. Calls to ``__repr__`` and ``__str__`` are passed to the internal value.

The internal value can be used via ``obj.value``.


Type-Specific *call-by-reference* Classes
*****************************************

.. _COMMON/CallByRefBoolParam:

CallByRefBoolParam
==================

The class :class:`~pyTooling.CallByRef.CallByRefBoolParam` implements call-by-ref behavior for the boolean type
(:class:`bool`).

Implemented operators:

* Binary comparison operators: ``==``, ``!=``
* Type conversions: ``bool()``, ``int()``

.. _COMMON/CallByRefIntParam:

CallByRefIntParam
=================

The class :class:`~pyTooling.CallByRef.CallByRefIntParam` implements call-by-ref behavior for the integer type
(:class:`int`).

Implemented operators:

* Unary operators: ``+``, ``-``, ``~``
* Binary boolean operators: ``&``, ``|``, ``^``
* Binary arithmetic operators: ``+``, ``-``, ``*``, ``/``, ``//``, ``%``, ``**``
* Binary comparison operators: ``==``, ``!=``, ``<``, ``<=``, ``>``, ``>=``
* Inplace boolean operators: ``&=``, ``|=``, ``^=``
* Inplace arithmetic operators: ``+=``, ``-=``, ``*=``, ``/=``, ``//=``, ``%=``, ``**=``
* Type conversions: ``bool()``, ``int()``