Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1brPv3-0008Uy-E4 for pgsql-sql@arkaria.postgresql.org; Tue, 04 Oct 2016 13:38:05 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1brPv2-0002lj-QX for pgsql-sql@arkaria.postgresql.org; Tue, 04 Oct 2016 13:38:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1brPu4-0001hI-7M for pgsql-sql@postgresql.org; Tue, 04 Oct 2016 13:37:04 +0000 Received: from mxout3.rambler.ru ([81.19.78.102]) by magus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1brPu0-0006Pl-St for pgsql-sql@postgresql.org; Tue, 04 Oct 2016 13:37:03 +0000 Received: from saddam2.rambler.ru (saddam2.rambler.ru [10.32.16.2]) by mxout3.rambler.ru (Postfix) with ESMTP id 81AF87C0781; Tue, 4 Oct 2016 16:36:59 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rambler.ru; s=mail; t=1475588219; bh=O1cP2YXdN3mbsJ6oZfHb9W9AoRiQp8oEcLrdZHbmaCo=; h=From:To:Cc:Reply-To:Subject:Date:In-Reply-To:References; b=Xo1b3exEto1AN2/fCqHv8tVuRmvmn/t9la9wXaYCkLn2p5xyu4quYgeCr97KOnFoM ickSVWK2eKfnrSgr/UzUYlcv+RekDIBqU9JK9qd45qCrC956XpYHmtuqIZfQq0akEM wnYC/GjMFl94KvQxOtH1C501Cqet2thSS4LJZzcc= Received: from localhost.localdomain (localhost [127.0.0.1]) by saddam2.rambler.ru (Postfix) with ESMTP id 7252335888C5; Tue, 4 Oct 2016 16:36:59 +0300 (MSK) Received: from [193.19.168.163] by mail.rambler.ru with HTTP; Tue, 4 Oct 2016 16:36:59 +0300 From: "Andrey Avakimov" To: "Adrian Klaver" Cc: "Pgsql Sql" Reply-To: "Andrey Avakimov" Subject: Re: PLPython and named arguments Date: Tue, 4 Oct 2016 16:36:59 +0300 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8"; format="flowed" In-Reply-To: Message-Id: <1475588219.350324.21966.19771@mail.rambler.ru> MIME-Version: 1.0 References: X-Mailer: Rambler WebMail, http://mail.rambler.ru/ X-Rambler-User: aquarius1993@rambler.ru/193.19.168.163 X-Pg-Spam-Score: -4.6 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org OMG... I feel ashamed that I missed that part of the documentation. Thank you Best Regards, Andrew > 04.10.2016, 16:22:55 =D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1= =82=D0=B5=D0=BB=D1=8C Adrian Klaver (adrian.klaver@aklaver.com) =D0=BD=D0= =B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB: >=20 > On 10/04/2016 05:20 AM, Andrey Avakimov wrote: > > Hello > > > > My question is about plpython behavior. In some cases procedure ignores > > named arguments and raises an error like: > > > > UnboundLocalError: local variable 'arg_from' referenced before assignme= nt >=20 > Because of this?: >=20 > https://www.postgresql.org/docs/9.6/static/plpython-funcs.html > " > The arguments are set as global variables. Because of the scoping rules=20 > of Python, this has the subtle consequence that an argument variable=20 > cannot be reassigned inside the function to the value of an expression=20 > that involves the variable name itself, unless the variable is=20 > redeclared as global in the block. For example, the following won't work: >=20 > CREATE FUNCTION pystrip(x text) > =C2=A0=C2=A0=C2=A0RETURNS text > AS $$ > =C2=A0=C2=A0=C2=A0x =3D x.strip() =C2=A0=C2=A0# error > =C2=A0=C2=A0=C2=A0return x > $$ LANGUAGE plpythonu; >=20 > because assigning to x makes x a local variable for the entire block,=20 > and so the x on the right-hand side of the assignment refers to a=20 > not-yet-assigned local variable x, not the PL/Python function parameter.= =20 > Using the global statement, this can be made to work: >=20 > CREATE FUNCTION pystrip(x text) > =C2=A0=C2=A0=C2=A0RETURNS text > AS $$ > =C2=A0=C2=A0=C2=A0global x > =C2=A0=C2=A0=C2=A0x =3D x.strip() =C2=A0=C2=A0# ok now > =C2=A0=C2=A0=C2=A0return x > $$ LANGUAGE plpythonu; >=20 > But it is advisable not to rely on this implementation detail of=20 > PL/Python. It is better to treat the function parameters as read-only." >=20 > So: >=20 > test=3D# select * from pystrip('test =C2=A0=C2=A0'); > ERROR: =C2=A0=C2=A0UnboundLocalError: local variable 'x' referenced befo= re assignment > CONTEXT: =C2=A0=C2=A0Traceback (most recent call last): > =C2=A0=C2=A0=C2=A0PL/Python function "pystrip", line 2, in > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0x =3D x.strip() =C2=A0=C2=A0# error > PL/Python function "pystrip" >=20 > > > > This can be solved by using such code: > > > > arg_from, arg_to =3D args > > > > But the reasons of such behavior are still unclear. > > > > Does anyone faced this thing? > > > > I will gladly provide any further information if needed. > > > > Best Regards, > > Andrew > > >=20 > --=20 > Adrian Klaver > adrian.klaver@aklaver.com >=20 > --=20 > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql >=20 >=20 --=20 Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql