Mailing List Archive

1 2  View All
Re: 3.10 change (?) for __bool__ [ In reply to ]
On Wed, Jan 13, 2021 at 08:27:46PM +1100, Chris Angelico wrote:

> It's a lot faster for C-implemented functions to require positional
> parameters. A number of functions had help text that implied that they
> accepted keyword-or-positional args, but if you tried to call them
> with keyword args, they'd error out.

That's an unfortunate limitation of the docs, not a language feature.
One might even call it a documentation bug.


> And yes. PEP 457 and then PEP 570 introduced real positional-only
> arguments, and part of the debate surrounded the question "should we
> require that all C-implemented functions support keyword args?". Why
> have a performance hit on all C functions just for the sake of an
> arbitrary consistency? Instead, the language definition was changed to
> make this possible - and now we have, for instance, "len(obj, /)",
> which clearly does not accept len(obj="x"), but does accept len("x").

Sorry to be pedantic here... oh who am I fooling, I'm not sorry at all
*wink*

I think that if we are to be precise, and we should be, Python the
language has always supported positional-only arguments. What we lacked
was syntax for declaring named positional-only **parameters**.

There has always been at least two ways to implement positional-only
arguments:

- write your function in C;

- or use `*args` and process them yourself.

So that's not a new feature. The new feature gained in 3.8 was syntax to
define names for positional-only parameters in def statements.

This may not have been really obvious until after PEPs 457 and 570
because we were (and still are) fairly blas? about the difference
between arguments and parameters, and of calling things "positional"
regardless of whether they were actually positional-or-keyword or
positional-only.

In any case, this is very much a red herring. Function call syntax is
not an optimization, it is a syntactic feature of the language. The
ability to pass an argument by position, position-or-keyword, or keyword
is an element of language design. The choice of which ones to support
may be influenced by concerns about efficiency and speed, but that's as
far as it goes.

I think your earlier example of short-cutting equality tests with
identity tests is more relevant here.


[...]
> In theory, "x = (expr)" followed by "if x:" should perform the exact
> same checks as "if (expr):"; if it's okay to violate that principle
> for "if a and b:", then why not for "if a: pass"? Either way, the
> interpreter knows that any sane __bool__ function cannot affect the
> result.

Of course they can: the method can fail and raise an exception. That
could be due to a bug, or by design, as in numpy arrays and (soon)
NotImplemented.

Sane `__bool__` methods can have useful side-effects. More than once
I've debugged code by sticking some logging inside a method I knew would
be called, and that method could as easily be `__bool__` as any other.


--
Steve
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/RH2SYWCCCGZGDMPXWHJ5VMIKK3EQJIVZ/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: 3.10 change (?) for __bool__ [ In reply to ]
On Wed, Jan 13, 2021 at 02:08:01AM -0800, Emily Bowman wrote:
> Even if you define __bool__() as returning a bool, and error/undefined
> behavior otherwise, that doesn't eliminate side effects. Is it even
> possible to nail down a definition to the point that you can say, "Thou
> shalt not mutate or cause anything" and have it meaningfully enforced in
> the compiler or interpreter?

No, I don't think it is possible to enforce lack of side-effects. Not
without rebuilding the language from the ground up with a clear,
definitive and enforcable distinction between pure and impure functions.

(I think Haskell does something like that. But trying to retrofit that
into Python would probably be about as hard as building a restricted
mode, and for similar reasons.)

Besides, we probably don't want to prohibit side-effects in `__bool__`.
That would prohibit useful tricks such as putting logging calls into a
method you are trying to debug.


--
Steve
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/L3M6RLVJ44VN4S34RYJTROSAZMUEPGWM/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: 3.10 change (?) for __bool__ [ In reply to ]
Hello,

On Thu, 14 Jan 2021 03:29:35 +1100
Steven D'Aprano <steve@pearwood.info> wrote:

> On Wed, Jan 13, 2021 at 02:08:01AM -0800, Emily Bowman wrote:
> > Even if you define __bool__() as returning a bool, and
> > error/undefined behavior otherwise, that doesn't eliminate side
> > effects. Is it even possible to nail down a definition to the point
> > that you can say, "Thou shalt not mutate or cause anything" and
> > have it meaningfully enforced in the compiler or interpreter?
>
> No, I don't think it is possible to enforce lack of side-effects. Not
> without rebuilding the language from the ground up with a clear,
> definitive and enforcable distinction between pure and impure
> functions.
>
> (I think Haskell does something like that. But trying to retrofit
> that into Python would probably be about as hard as building a
> restricted mode, and for similar reasons.)
>
> Besides, we probably don't want to prohibit side-effects in
> `__bool__`. That would prohibit useful tricks such as putting logging
> calls into a method you are trying to debug.

Surely, if we do that, we wouldn't use Haskell's definition of
purity ;-). Rather, a practical definition of purity, Python-style. For
example, print() would be considered "pure", as its purpose is to
provide program output, not arbitrarily change program state (so, all of
__str__, __repr__, __format__ would need to be pure, but if someone
overrode print() to shoot at random modules/data at them, then they
shoot themselves in the feet, nothing else).

[]

--
Best regards,
Paul mailto:pmiscml@gmail.com
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/44IGY7CWR3OPM2RN64GJHXOML4ZMW3IX/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: 3.10 change (?) for __bool__ [ In reply to ]
On 14/01/21 5:29 am, Steven D'Aprano wrote:
> No, I don't think it is possible to enforce lack of side-effects. Not
> without rebuilding the language from the ground up with a clear,
> definitive and enforcable distinction between pure and impure functions.
>
> (I think Haskell does something like that.

All functions are pure in Haskell -- not sure if that counts
as "doing something like that". Retrofitting it into
Python would turn it into a very different language.

--
Greg
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/7BOWX7N54CA6FPGJDCQSEH73MQZKJEEE/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: 3.10 change (?) for __bool__ [ In reply to ]
On 14/01/21 6:17 am, Paul Sokolovsky wrote:
> For
> example, print() would be considered "pure", as its purpose is to
> provide program output, not arbitrarily change program state

That definition of purity wouldn't really help with optimisations,
though, because optimising away a print() call would still cause
a visible change in the program's behaviour.

--
Greg
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/B6VDVU7UOB5QNILEJ44J7JOSVBF7WDBF/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: 3.10 change (?) for __bool__ [ In reply to ]
Hello,

On Thu, 14 Jan 2021 12:45:11 +1300
Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:

> On 14/01/21 6:17 am, Paul Sokolovsky wrote:
> > For
> > example, print() would be considered "pure", as its purpose is to
> > provide program output, not arbitrarily change program state
>
> That definition of purity wouldn't really help with optimisations,
> though, because optimising away a print() call would still cause
> a visible change in the program's behaviour.

But nobody talked about optimizing away generic "pure"-annotated
functions (which would differ from "mathematical" definition of
purity), only about optimizing "pure" *dunder* methods (which are by
definition special methods, which are called implicitly; or not called,
which is the aim we discuss).

"Pure" annotation on other functions are drawn only to verify that a
dunder itself is indeed "pure".

And "pure" is of course a pretty rough term meaning "not causing
*non-local* side effects". print() in that regard causes only localized
side effect of output appearing on terminal. print() also can be
redefined to output to io.StringIO, and it still will be "pure", as
side effects will be localized to just that io.StringIO object (which
has dedicated purpose to be print's buffer in the first place). But if
user redefines print() to e.g. manipulate sys.path, it's now causes
non-local side effects, and thus breaks API contract re: "purity".

> --
> Greg

[]

--
Best regards,
Paul mailto:pmiscml@gmail.com
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/B6E2NZYIURXDRZ336TBAVCANI666KH4A/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: 3.10 change (?) for __bool__ [ In reply to ]
On 14/01/21 1:13 pm, Paul Sokolovsky wrote:
> But nobody talked about optimizing away generic "pure"-annotated
> functions (which would differ from "mathematical" definition of
> purity), only about optimizing "pure" *dunder* methods

The same thing applies. If we decide that print() is pure,
then a __bool__ that calls print() is also pure, so there's
nothing wrong with optimising it away, right?

--
Greg
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/KFP2TZXVJAQROMILOOM3CJT24DJESBHJ/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: 3.10 change (?) for __bool__ [ In reply to ]
On 1/13/2021 8:56 PM, Greg Ewing wrote:
> On 14/01/21 1:13 pm, Paul Sokolovsky wrote:
>> But nobody talked about optimizing away generic "pure"-annotated
>> functions (which would differ from "mathematical" definition of
>> purity), only about optimizing "pure" *dunder* methods
>
> The same thing applies. If we decide that print() is pure,
> then a __bool__ that calls print() is also pure, so there's
> nothing wrong with optimising it away, right?

I say 'yes', because the purpose of logging is to document what happens,
and if nothing happens, there is nothing to document. Wrapping a
.__bool__ in a logging decorator might be part of testing it.


--
Terry Jan Reedy
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/NRJT2YZ2U73ZYFZPAKKPXFBKGLJMBG3M/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: 3.10 change (?) for __bool__ [ In reply to ]
On 14/01/21 3:32 pm, Terry Reedy wrote:
> I say 'yes', because the purpose of logging is to document what happens,
> and if nothing happens, there is nothing to document.  Wrapping a
> .__bool__ in a logging decorator might be part of testing it.

Or it might be something else.

It would be fine to *define* __bool__ as something the compiler
is allowed to optimise away. But that's a different thing from
"purity" (which is hard to pin down in an imperative language).

--
Greg
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/FKQSB6SVOAO2Y3BOQSXSHEN5LM2KBETR/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: 3.10 change (?) for __bool__ [ In reply to ]
Hello,

On Thu, 14 Jan 2021 23:00:06 +1300
Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:

> On 14/01/21 3:32 pm, Terry Reedy wrote:
> > I say 'yes', because the purpose of logging is to document what
> > happens, and if nothing happens, there is nothing to document.
> > Wrapping a .__bool__ in a logging decorator might be part of
> > testing it.
>
> Or it might be something else.
>
> It would be fine to *define* __bool__ as something the compiler
> is allowed to optimise away.

Yes, and the talk was exactly about how to *proceed* with (re)defining
__bool__ (and other dunders, then other runtime inefficiencies) as
something the compiler is allowed to optimise away. To retrace the
outline:

1. Current semantics doesn't say that __bool__ can be optimized away,
and it stays that way. (Compare that to the parallel discussion of
PEP649 - it tries to change existing semantics by disallowing variable
annotations inside the "if" statement, and that's raised as a concern).
2. Instead, a new semantic mode is introduced, which needs to be
explicitly enabled.
3. Now the problem comes up about compatibility of the existing source
code with the new mode.
4. It's resolved by requiring __bool__ and other dunders to be
annotated "pure" (tentative code-name). The code which doesn't have all
__bool__, etc. methods annotated as such cannot be run in the new
semantic mode (and benefit from any optimizations it may offer), and has
to continue be run in the old mode. (And note that annotated code
still can run in the old mode either.)
5. Now, stamping annotations is nice, but the code should correspond to
them, so there should be ability to "typecheck" such annotated code
(more like "behavior-check", though effects tracking is also part of
the type theory). It can be handled in the same way as typechecking
existing type annotations happen - by external tools.

> But that's a different thing from
> "purity" (which is hard to pin down in an imperative language).

At the beginning of this sub-thread, example of Haskell was given. It
went the different way - of banning any mutability and side-effects at
all, then making heroic efforts of introducing concepts which would
make writing programs under such model still surmountable, like monads,
etc. That was hard. It didn't stop them. We here talk about
evolutionary, and clearly delinated adjustments to Python semantics,
based on the experience with efficiently executing Python code.
Certainly, much less hard than what Haskell did.

> --
> Greg

[]

--
Best regards,
Paul mailto:pmiscml@gmail.com
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/2AI7DRSQ6AJUNQSVXU672DQ3OOWVQNV7/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: 3.10 change (?) for __bool__ [ In reply to ]
On Wed, Jan 13, 2021 at 08:17:26PM +0300, Paul Sokolovsky wrote:

> > Besides, we probably don't want to prohibit side-effects in
> > `__bool__`. That would prohibit useful tricks such as putting logging
> > calls into a method you are trying to debug.
>
> Surely, if we do that, we wouldn't use Haskell's definition of
> purity ;-). Rather, a practical definition of purity, Python-style.

It's not *Haskell's* definition, it's pretty much *everyone's*
definition of pure.

https://en.wikipedia.org/wiki/Pure_function



> For example, print() would be considered "pure", as its purpose is to
> provide program output, not arbitrarily change program state


Is writing to an external file a change to program state? Keep in mind
that programs can get state from external files.

I would say that what you are describing is not a distinction between
pure and impure functions, based on whether or not they can be safely
optimised away without a change in visible behaviour. The distinction
you are proposing is into two arbitrary sets of functions, some of which
can be optimized away and some which can't, regardless of whether or not
that optimisation leads to change in visible behaviour.

If you want to argue for that, fine, do so, but don't call impure
functions "pure".


--
Steve
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/FKSAUJVPPP2JV56T4BIB7W3B6GD63HGE/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: 3.10 change (?) for __bool__ [ In reply to ]
Hello,

On Thu, 14 Jan 2021 22:05:37 +1100
Steven D'Aprano <steve@pearwood.info> wrote:

> On Wed, Jan 13, 2021 at 08:17:26PM +0300, Paul Sokolovsky wrote:
>
> > > Besides, we probably don't want to prohibit side-effects in
> > > `__bool__`. That would prohibit useful tricks such as putting
> > > logging calls into a method you are trying to debug.
> >
> > Surely, if we do that, we wouldn't use Haskell's definition of
> > purity ;-). Rather, a practical definition of purity,
> > Python-style.
>
> It's not *Haskell's* definition, it's pretty much *everyone's*
> definition of pure.
>
> https://en.wikipedia.org/wiki/Pure_function
>
>
>
> > For example, print() would be considered "pure", as its purpose is
> > to provide program output, not arbitrarily change program state
>
>
> Is writing to an external file a change to program state? Keep in
> mind that programs can get state from external files.

Please reread the reply about "not having non-local side
effects". NotHavingNonLocalSideEffects is a bit longish for an
annotation identifier though...

>
> I would say that what you are describing is not a distinction between
> pure and impure functions,

You say that as if you propose some better identifier for
annotation ;-). I personally would be fine if the annotation is:

def print(*Any) -> PythoniclyPure: ...

I'm talking about small-scale semantic distinctions required for capture
needed large-scale semantic effect. Naming, when it comes to it, will
be subject of long, looooooong bikeshedding. (Which already started,
thanks!)

> based on whether or not they can be safely
> optimised away without a change in visible behaviour. The distinction
> you are proposing is into two arbitrary sets of functions, some of
> which can be optimized away and some which can't, regardless of
> whether or not that optimisation leads to change in visible behaviour.
>
> If you want to argue for that, fine, do so, but don't call impure
> functions "pure".

[]

--
Best regards,
Paul mailto:pmiscml@gmail.com
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/QBWTRDJZHDXYAOY42DZMETRNPOLHPFZH/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: 3.10 change (?) for __bool__ [ In reply to ]
On Fri, Jan 15, 2021 at 10:13 PM Rob Cliffe via Python-Dev
<python-dev@python.org> wrote:
>
>
>
> On 12/01/2021 15:53, Mark Shannon wrote:
> > Hi everyone,
> >
> >
> >
> > In master we convert `if x: pass` to `pass` which is equivalent,
> > unless bool(x) has side effects the first time it is called. This is a
> > recent change.
> >
> Suppose x is not a currently valid variable name at runtime. Will the
> NameError still be "optimised" away?

No, it won't. The expression still gets fully evaluated. The ONLY part
that gets optimized away is the check "is this thing true?".

ChrisA
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/KQ2QH3VB5Q6LYCBE4SUWZKRMML54TOXS/
Code of Conduct: http://python.org/psf/codeofconduct/

1 2  View All