gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
5.6KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>45.2. PL/Python Functions</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="plpython-python23.html" title="45.1. Python 2 vs. Python 3" /><link rel="next" href="plpython-data.html" title="45.3. Data Values" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">45.2. PL/Python Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plpython-python23.html" title="45.1. Python 2 vs. Python 3">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="plpython.html" title="Chapter 45. PL/Python - Python Procedural Language">Up</a></td><th width="60%" align="center">Chapter 45. PL/Python - Python Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="plpython-data.html" title="45.3. Data Values">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="PLPYTHON-FUNCS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">45.2. PL/Python Functions</h2></div></div></div><p>
  3. Functions in PL/Python are declared via the
  4. standard <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a> syntax:
  5. </p><pre class="programlisting">
  6. CREATE FUNCTION <em class="replaceable"><code>funcname</code></em> (<em class="replaceable"><code>argument-list</code></em>)
  7. RETURNS <em class="replaceable"><code>return-type</code></em>
  8. AS $$
  9. # PL/Python function body
  10. $$ LANGUAGE plpythonu;
  11. </pre><p>
  12. </p><p>
  13. The body of a function is simply a Python script. When the function
  14. is called, its arguments are passed as elements of the list
  15. <code class="varname">args</code>; named arguments are also passed as
  16. ordinary variables to the Python script. Use of named arguments is
  17. usually more readable. The result is returned from the Python code
  18. in the usual way, with <code class="literal">return</code> or
  19. <code class="literal">yield</code> (in case of a result-set statement). If
  20. you do not provide a return value, Python returns the default
  21. <code class="symbol">None</code>. <span class="application">PL/Python</span> translates
  22. Python's <code class="symbol">None</code> into the SQL null value. In a procedure,
  23. the result from the Python code must be <code class="symbol">None</code> (typically
  24. achieved by ending the procedure without a <code class="literal">return</code>
  25. statement or by using a <code class="literal">return</code> statement without
  26. argument); otherwise, an error will be raised.
  27. </p><p>
  28. For example, a function to return the greater of two integers can be
  29. defined as:
  30. </p><pre class="programlisting">
  31. CREATE FUNCTION pymax (a integer, b integer)
  32. RETURNS integer
  33. AS $$
  34. if a &gt; b:
  35. return a
  36. return b
  37. $$ LANGUAGE plpythonu;
  38. </pre><p>
  39. The Python code that is given as the body of the function definition
  40. is transformed into a Python function. For example, the above results in:
  41. </p><pre class="programlisting">
  42. def __plpython_procedure_pymax_23456():
  43. if a &gt; b:
  44. return a
  45. return b
  46. </pre><p>
  47. assuming that 23456 is the OID assigned to the function by
  48. <span class="productname">PostgreSQL</span>.
  49. </p><p>
  50. The arguments are set as global variables. Because of the scoping
  51. rules of Python, this has the subtle consequence that an argument
  52. variable cannot be reassigned inside the function to the value of
  53. an expression that involves the variable name itself, unless the
  54. variable is redeclared as global in the block. For example, the
  55. following won't work:
  56. </p><pre class="programlisting">
  57. CREATE FUNCTION pystrip(x text)
  58. RETURNS text
  59. AS $$
  60. x = x.strip() # error
  61. return x
  62. $$ LANGUAGE plpythonu;
  63. </pre><p>
  64. because assigning to <code class="varname">x</code>
  65. makes <code class="varname">x</code> a local variable for the entire block,
  66. and so the <code class="varname">x</code> on the right-hand side of the
  67. assignment refers to a not-yet-assigned local
  68. variable <code class="varname">x</code>, not the PL/Python function
  69. parameter. Using the <code class="literal">global</code> statement, this can
  70. be made to work:
  71. </p><pre class="programlisting">
  72. CREATE FUNCTION pystrip(x text)
  73. RETURNS text
  74. AS $$
  75. global x
  76. x = x.strip() # ok now
  77. return x
  78. $$ LANGUAGE plpythonu;
  79. </pre><p>
  80. But it is advisable not to rely on this implementation detail of
  81. PL/Python. It is better to treat the function parameters as
  82. read-only.
  83. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plpython-python23.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plpython.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plpython-data.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">45.1. Python 2 vs. Python 3 </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 45.3. Data Values</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1