[Python-ideas] Re: Curious : Why staticmethod if classmethods can do everything a static method can?

2020-09-11 Thread Christopher Barker
On Fri, Sep 11, 2020 at 7:27 PM <2qdxy4rzwzuui...@potatochowder.com> wrote:

> But no extraneous cognitive noise?  By definition, methods appear inside
> a class definition, and then I have to process the @staticmethod
> decorator.  Effectively, the decorator "cancels" the class method status
> of the function.  I can accomplish the same thing with clean
> module-level function, modulo the specific namespace in which the
> function is created.
>

Exactly. This question asked why staticmethod when we have classmethod. But
the real question is why staticmethod at all?

And the answer is that there is very little use for staticmethod in Python
-- all it does is put a regular function in the class' namespace, and since
we have modules to be nice namespaces for functions, there is little need
for it.

Sometimes it does make sense to keep some functionality all bundled
together with a class, but I find it pretty rare.

-CHB

-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ZGIJ3QJMNMZLNIWLTGFXPH3A73NDEWUM/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Curious : Why staticmethod if classmethods can do everything a static method can?

2020-09-11 Thread Greg Ewing

On 12/09/20 12:21 pm, Guido van Rossum wrote:
I had heard of this 
concept in another language (C++? Smalltalk?)


Probably C++ or Java. Smalltalk doesn't have static methods, only a
form of class method.

IMO, static methods are a kludge only needed in languages that make
classes do double duty as modules, and also can't have class methods
because classes aren't first-class objects. I've never felt the need
for one in Python.

--
Greg
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/TQCMUPLEWZJO3YGVRPE4ECMHAKC32DEK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Curious : Why staticmethod if classmethods can do everything a static method can?

2020-09-11 Thread 2QdxY4RzWzUUiLuE
On 2020-09-12 at 09:57:10 +1000,
Cameron Simpson  wrote:

> So, consider:
> 
> @classmethod
> def func(cls, foo):
> print(foo)
> 
> A linter will warn you that "cls" is unused. With a static method:
> 
> @staticmethod
> def func(foo):
> print(foo)
> 
> a linter will be happy.
> 
> Think of @classmethod and @staticmethod as ways to write "clean" 
> functions with no extraneous cognitive noise.

I concur with all of Cameron's technical details and explanations.

But no extraneous cognitive noise?  By definition, methods appear inside
a class definition, and then I have to process the @staticmethod
decorator.  Effectively, the decorator "cancels" the class method status
of the function.  I can accomplish the same thing with clean
module-level function, modulo the specific namespace in which the
function is created.

So, in a module m:

class X:
@staticmethod
def f(x):
print(x)

and

def f(x):
print(x)

m.X.f and m.f are interchangeable.  IMO, m.f has less cognitive load
than m.X.f, at their definitions and at call sites.  Full disclosure:  I
consider most of Object Oriented Programming to be extraneous cognitive
noise.

In other languages (*cough* Java *cough*), there are no functions
outside of classes, and static methods fill that role.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/3PEG35ADEQIEMXLKBCDGBFSM2IO5NS4V/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A shortcut to load a JSON file into a dict : json.loadf

2020-09-11 Thread Guido van Rossum
On Fri, Sep 11, 2020 at 4:19 PM David Mertz  wrote:

> On Fri, Sep 11, 2020, 12:59 PM Guido van Rossum  wrote:
>
>> What happened to "not every three-line function needs to be a built-in"?
>> This is *literally* a three-line function. And I know the proposal is not
>> to make it a builtin, but still... ISTM down here lies the path to PHP.
>>
>
> By the same reasoning, though, if you have dumps(), writing dump() in
> terms of it is a three-line function. Same in the venerable pickle versions.
>

Not quite -- if the serialized data is really huge it makes sense to read
or write it directly from/to a file, rather than building up a gigantic
in-memory buffer as an intermediate structure. (And going in the other
direction, if all you have is load/dump, constructing an io.StringIO
instance is a fairly awkward bit of idiom.

But I'm convinced by Inada's observation that it's easy to have
encoding-related bugs -- we should add this in a way that avoids those. And
I'm fine with loadf/dumpf as the names, since we already have loads/dumps.

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/N77PUNWOPG7YKL5CFXQHVXP5WB7TXAGA/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Curious : Why staticmethod if classmethods can do everything a static method can?

2020-09-11 Thread Guido van Rossum
On Fri, Sep 11, 2020 at 4:00 PM The Nomadic Coder 
wrote:

> This question is to further my understanding of the internals. It seems to
> me that a classmethod can do everything a staticmethod can, and
> additionally is not limited by inheritance.
>
> Why does python have them as two different constructs?
>

The answer is actually somewhat embarrassing. IIRC I had heard of this
concept in another language (C++? Smalltalk?) and it sounded useful, so I
added staticmethod. Then after the release and some actual use I realized
that it was actually useful to have access to the class from inside the
method (in case it's called for a subclass). So I also added classmethod.
But since staticmethod was already out of the bag, I kept it around, and
it's found its uses (as you can see from other replies).

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/N5ZBMAUTUHDAQI232PM2MLDB47OJRGQE/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Curious : Why staticmethod if classmethods can do everything a static method can?

2020-09-11 Thread William Pickard
staticmethod is generally used if you don't want an automatically bound 
'self'/'cls' parameter when invoking a method on the type.
classmethod just changes WHICH object is used as the "self" parameter.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5EZTQOWS4ANG4XEYP2X3ADOBI3DPGLOI/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Curious : Why staticmethod if classmethods can do everything a static method can?

2020-09-11 Thread Cameron Simpson
On 11Sep2020 22:58, The Nomadic Coder  wrote:
>This question is to further my understanding of the internals. It seems to me 
>that a classmethod can do everything a staticmethod can, and additionally is 
>not limited by inheritance.
>
>Why does python have them as two different constructs?

It has to do with context. What context does the method need?

The default (an instance method) requires "self" to perform.

A class method requires the class to perform, but not an instance.

A static method requires neither.

Define your method appropriately.

When you start using linters to catch mechanical code issues, the 
"unused variable" is a standard issue: it means either that the function 
has been given context it doesn't need (if the variable came from a 
parameter), or that a function is doing a computation it doesn't need to 
make (or keep), or that you've got a bug because you compute something 
and fail to use it (often a variable name misspelling).

So, consider:

@classmethod
def func(cls, foo):
print(foo)

A linter will warn you that "cls" is unused. With a static method:

@staticmethod
def func(foo):
print(foo)

a linter will be happy.

Think of @classmethod and @staticmethod as ways to write "clean" 
functions with no extraneous cognitive noise.

Also, what's inheritance to do with this? You can inherit static 
methods, I do it all the time.  Maybe I don't understand what you mean 
by "additionally is not limited by inheritance"?

Cheers,
Cameron Simpson 
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/KZ6QVA3V7MZQUPGJAVNIQWQHPZQ22VUX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A shortcut to load a JSON file into a dict : json.loadf

2020-09-11 Thread Cameron Simpson
On 11Sep2020 23:09, The Nomadic Coder  wrote:
>oops, was not aware of "not every three-line function needs to be a built-in"
>
>This came out personal frustration, as I use this 3 line function very, 
>very often, and the whole community does. Still learning-to-navigate 
>what's accepted here and what's not :)

DRY.

Why do not people use modules more often (I don't mean the stdlib, I 
mean extra modules)?

My personal solution to this kind of thing is to keep a little 
library/module of these 3 line things if I use them. Then you just 
import stuff and use it.

2 trite examples:

I've got a cs.lex module with a bunch of little things in there for 
parsing stuff - identifiers, quoted strings, etc etc; it's on PyPI so 
using it elsewhere is trivial.

Closer to Nomadic's use case, I've got an @strable decorator, thus:

@strable
def func(f, ...):
... do something with an open file ...

It intercepts the first argument: if a str, it opens it as a file (by 
default, you can provide an arbitrary function for the "open" action).  
Then the function just has to work with a file (or whatever a str should 
turn into, domain specific).

You could also write:

load_json = strable(json.load)

and be on your way.

This is also in a module (cs.deco), also on PyPI for reuse.

Not every three-line function needs to be a built-in, but for the 
three-line functions _you_ use a lot, write them _once_ and import them 
from your personal little module-of-three-line-functions.

No need to publish to PyPI (extra work) - it's as easy to keep them 
locally unless you need them elsewhere. But don't rewrite - reuse!

Cheers,
Cameron Simpson 
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/QIZ4SZUT3OYCFFPWKKTNQ5E2F2G6T2XH/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A shortcut to load a JSON file into a dict : json.loadf

2020-09-11 Thread Inada Naoki
On Sat, Sep 12, 2020 at 7:59 AM Guido van Rossum  wrote:
>
> What happened to "not every three-line function needs to be a built-in"? This 
> is *literally* a three-line function.

This is not only common two line idiom. It creates huge amount of
potential bugs.

```
with open("myfile.json") as f:
data = json.load(f)

with open("myfile.json", "w") as f:
json.dump(f, ensure_ascii=False)
```

Both two lines have bugs; they don't specify `encoding` [1]. It uses
locale encoding to read/write JSON although the JSON file must be
encoded in UTF-8.
The locale encoding is legacy encoding on Windows. It is very easy to
write "not work on Windows".

My PEP 597 [2] will help to find such bugs. But it warns only in dev
mode to avoid too noisy DeprecationWarning.
Huge amounts of DeprecationWarning make people dismiss DeprecationWarning.

So helper functions will save people from this kind of bugs too.

[1] In case of `json.load(f)`, we can use binary file instead.
[2] https://www.python.org/dev/peps/pep-0597/

Regards,

-- 
Inada Naoki  
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/X5GEMWMPBGQGXPQLBRKV7D7KHKC6ODOA/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A shortcut to load a JSON file into a dict : json.loadf

2020-09-11 Thread David Mertz
On Fri, Sep 11, 2020, 12:59 PM Guido van Rossum  wrote:

> What happened to "not every three-line function needs to be a built-in"?
> This is *literally* a three-line function. And I know the proposal is not
> to make it a builtin, but still... ISTM down here lies the path to PHP.
>

By the same reasoning, though, if you have dumps(), writing dump() in terms
of it is a three-line function. Same in the venerable pickle versions.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JO25HTMYFX66XU6SGHIAYXMR2ZPJ23ZC/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A shortcut to load a JSON file into a dict : json.loadf

2020-09-11 Thread The Nomadic Coder
oops, was not aware of "not every three-line function needs to be a built-in"

This came out personal frustration, as I use this 3 line function very, very 
often, and the whole community does. Still learning-to-navigate what's accepted 
here and what's not :)
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/J6Z7T7S5ORDUWXAAODDS53Z3P3VYB7L5/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Curious : Why staticmethod if classmethods can do everything a static method can?

2020-09-11 Thread The Nomadic Coder
This question is to further my understanding of the internals. It seems to me 
that a classmethod can do everything a staticmethod can, and additionally is 
not limited by inheritance.

Why does python have them as two different constructs?
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UBCT2AE2VGFJCCXEBOSNMF62EA4JBD3C/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A shortcut to load a JSON file into a dict : json.loadf

2020-09-11 Thread Guido van Rossum
What happened to "not every three-line function needs to be a built-in"?
This is *literally* a three-line function. And I know the proposal is not
to make it a builtin, but still... ISTM down here lies the path to PHP.

On Fri, Sep 11, 2020 at 3:16 PM Christopher Barker 
wrote:

> I"m pretty sure this came up recently, and was pretty much rejected.
>
> Another option would be to have json.dump take a file-like-object or a
> path-like object -- there's plenty of code out there that does that.
>
> hmm.. maybe that was the version that was rejected.
>
> But I like the idea either way. it always seemed cumbersome to me to write
> the whole context manager in these kinds of cases.
>
> -CHB
>
>
>
>
>
> On Fri, Sep 11, 2020 at 2:05 PM The Nomadic Coder <
> atemysemico...@gmail.com> wrote:
>
>> Personally prefer it to be in the json module as it just feels more
>> logical. But that's just a personal choice.
>>
>> I didn't mention about dumpf as I see a previous thread that became quite
>> controversial (seems like dumps was a more common usage at that time).
>>
>> Something I forgot to mention : A (non-exact)search for this construct in
>> github (https://github.com/search?q=with+open+%3A+json.load=Code)
>> gives 20million+ results. Seems like it's a popular set of statements that
>> people use ...
>>
>> 
>> The Nomadic Coder
>> ___
>> Python-ideas mailing list -- python-ideas@python.org
>> To unsubscribe send an email to python-ideas-le...@python.org
>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>> Message archived at
>> https://mail.python.org/archives/list/python-ideas@python.org/message/R4CZ2IGLXR5NYNQDDFZZE5YB3RHD3RP6/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
> --
> Christopher Barker, PhD
>
> Python Language Consulting
>   - Teaching
>   - Scientific Software Development
>   - Desktop GUI and Web Development
>   - wxPython, numpy, scipy, Cython
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/4XVZAXRXQKZKTJJAH4WHXFVQGA57YFAV/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FRKQ36IA6OJIZHNBNA3DQ4YLJUCBDR6O/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A shortcut to load a JSON file into a dict : json.loadf

2020-09-11 Thread The Nomadic Coder
How about load_from_path or loadp?

I can understand that loadf is a bit misleading, you might think that it loads 
from a file-like object, but parses from a file instead.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/K3MEGF53HU24JP6O4YZWX2CMEXQ4NZES/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A shortcut to load a JSON file into a dict : json.loadf

2020-09-11 Thread David Mertz
I like the idea of having these functions, but I don't like overloading the
argument to a function with "filename or file-like object" as is common in
libraries like Pandas.  I think there are a few places the standard library
does it, but the separation seems better to me.

I don't LOVE the names dumpf() and loadf(), but I don't have an obviously
better choice.  I guess probably I'd want fromfile() and tofile() as more
"modern" names.

On Fri, Sep 11, 2020 at 12:17 PM Christopher Barker 
wrote:

> I"m pretty sure this came up recently, and was pretty much rejected.
>
> Another option would be to have json.dump take a file-like-object or a
> path-like object -- there's plenty of code out there that does that.
>
> hmm.. maybe that was the version that was rejected.
>
> But I like the idea either way. it always seemed cumbersome to me to write
> the whole context manager in these kinds of cases.
>
> -CHB
>
>
>
>
>
> On Fri, Sep 11, 2020 at 2:05 PM The Nomadic Coder <
> atemysemico...@gmail.com> wrote:
>
>> Personally prefer it to be in the json module as it just feels more
>> logical. But that's just a personal choice.
>>
>> I didn't mention about dumpf as I see a previous thread that became quite
>> controversial (seems like dumps was a more common usage at that time).
>>
>> Something I forgot to mention : A (non-exact)search for this construct in
>> github (https://github.com/search?q=with+open+%3A+json.load=Code)
>> gives 20million+ results. Seems like it's a popular set of statements that
>> people use ...
>>
>> 
>> The Nomadic Coder
>> ___
>> Python-ideas mailing list -- python-ideas@python.org
>> To unsubscribe send an email to python-ideas-le...@python.org
>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>> Message archived at
>> https://mail.python.org/archives/list/python-ideas@python.org/message/R4CZ2IGLXR5NYNQDDFZZE5YB3RHD3RP6/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
> --
> Christopher Barker, PhD
>
> Python Language Consulting
>   - Teaching
>   - Scientific Software Development
>   - Desktop GUI and Web Development
>   - wxPython, numpy, scipy, Cython
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/4XVZAXRXQKZKTJJAH4WHXFVQGA57YFAV/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
The dead increasingly dominate and strangle both the living and the
not-yet born.  Vampiric capital and undead corporate persons abuse
the lives and control the thoughts of homo faber. Ideas, once born,
become abortifacients against new conceptions.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/WOTLFRSGGRGVIYEIOCTGQJAWA2KK5PPJ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A shortcut to load a JSON file into a dict : json.loadf

2020-09-11 Thread Christopher Barker
I"m pretty sure this came up recently, and was pretty much rejected.

Another option would be to have json.dump take a file-like-object or a
path-like object -- there's plenty of code out there that does that.

hmm.. maybe that was the version that was rejected.

But I like the idea either way. it always seemed cumbersome to me to write
the whole context manager in these kinds of cases.

-CHB





On Fri, Sep 11, 2020 at 2:05 PM The Nomadic Coder 
wrote:

> Personally prefer it to be in the json module as it just feels more
> logical. But that's just a personal choice.
>
> I didn't mention about dumpf as I see a previous thread that became quite
> controversial (seems like dumps was a more common usage at that time).
>
> Something I forgot to mention : A (non-exact)search for this construct in
> github (https://github.com/search?q=with+open+%3A+json.load=Code)
> gives 20million+ results. Seems like it's a popular set of statements that
> people use ...
>
> 
> The Nomadic Coder
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/R4CZ2IGLXR5NYNQDDFZZE5YB3RHD3RP6/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/4XVZAXRXQKZKTJJAH4WHXFVQGA57YFAV/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: 'Infinity' constant in Python

2020-09-11 Thread Christopher Barker
On Fri, Sep 11, 2020 at 9:46 AM Stephen J. Turnbull

> itself doesn't seem to be enough to produce inf:


Indeed -- overflow and underflow and divide by zero all all caught and
raise. But you can get it with a simple huge literal:

In [22]: 1e1000

Out[22]: inf

But if there is going to be a change, just import inf and nan into the
>
> builtin namespace as identifiers.


Indeed — that’s what the PEP will propose.


> Christopher Barker writes:
>
>  > Another point is that these are not going to be singletons, like True,
>
>  > False and None
>
> Singleton isn't the problem here.  inf = None causes no problems if
>
> that's what you want to do.  It's that those three names are keywords,
>
> and can't be used as identifiers.


But why are they keywords? In Py2, True and False were just identifiers-
they were made keywords in py3.

I’m suggesting that one reason that they were made keywords is that it's
useful for them to be singletons, which is much harder to do (or at least
use consistently) if the names can be reassigned.

But these float special values can't be singletons anyway -- they could
come from anywhere, so you really don't want to have people doing:

something is inf
or
something is nan

test anyway. Which means there really isn't a reason to make them keywords
at all.

 > -- they are just floats that happen to have particular values.
>
> s/floats/identifiers/
>

The values are floats, but inf and nan are syntactically identifiers, not
> floats.
>

indeed -- I meant that the object bound to math.inf and math.nan (and if
this goes through bound to the built in names) are just floats with
particular values. No different than, say, math.pi, or 1.2 for that matter.

As far as I can tell, there is no way to instantiate a different bool,
which we would not want to do with these special values -- even if it were
even possible.

-CHB
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/DQOMOJ3YW4BAVGWGG2LFQW54MWWWHJQL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: 'Infinity' constant in Python

2020-09-11 Thread David Mertz
On Fri, Sep 11, 2020 at 10:19 AM Guido van Rossum  wrote:

> While one may argue that writing `1e1000` is not an "arithmetic
> operation", certainly it's certainly not "casting strings to floats", and
> it's the simeplest way of producing `inf` in a pinch (in theory it's not
> portable, but I think "in a pinch" means you don't care about that).
>

For 128-bit versions of Python you'd need 1e4933.  For 256-bit, 1e78914.
But those work fine on 32-bit or 64-bit also.

I don't actually understand why Stephen made this claim about arithmetic
> operations, since inf and nan exist *exactly* because arithmetic operations
> may produce them. And you don't need to involve pi either, just `1e300 *
> 1e300` does it.
>

Yeah, pi was irrelevant in my example.  It's just something from my writing
for hypothetical code that does stuff with 22/7 vs. with pi to see how
different they are.

-- 
The dead increasingly dominate and strangle both the living and the
not-yet born.  Vampiric capital and undead corporate persons abuse
the lives and control the thoughts of homo faber. Ideas, once born,
become abortifacients against new conceptions.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XBF4OHIZ43RH3OH7LTHEGGOCV47HD633/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A shortcut to load a JSON file into a dict : json.loadf

2020-09-11 Thread The Nomadic Coder
Personally prefer it to be in the json module as it just feels more logical. 
But that's just a personal choice. 

I didn't mention about dumpf as I see a previous thread that became quite 
controversial (seems like dumps was a more common usage at that time).

Something I forgot to mention : A (non-exact)search for this construct in 
github (https://github.com/search?q=with+open+%3A+json.load=Code) gives 
20million+ results. Seems like it's a popular set of statements that people use 
...


The Nomadic Coder
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/R4CZ2IGLXR5NYNQDDFZZE5YB3RHD3RP6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A shortcut to load a JSON file into a dict : json.loadf

2020-09-11 Thread Alex Hall
+1 because the general idea is one of my most commonly used utility
functions. There should also be a similar function for writing to a file,
e.g. json.dumpf if we stick to this naming scheme.

As an alternative, pathlib.Path.read/write_text are pretty cool, maybe we
could have .read/write_json?

 In any case if we add anything like this it would probably make sense to
add similar functions for pickle, and maybe other formats if the API is
obvious enough.

On Fri, Sep 11, 2020 at 10:40 PM The Nomadic Coder 
wrote:

> Hi All,
>
> This is the first time I'm posting to this mailing group, so forgive me if
> I'm making any mistakes.
>
> So one of the most common ways to load json, is via a file. This is used
> extensively in data science and the lines. We often write something like :-
>
> with open(filename.json, "r") as f:
> my_dict = json.load(f)
>
> or
> my_dict = json.load(open("filename.json", "r"))
>
> Since this is so common, why doesn't python have something like :-
> json.loadf("filename.json")
>
> Is there an obvious issue by defining this in the cpython? I don't
> whipping up a PR if it gains traction.
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/YHO575YY4FQC3GBDF4SKOWIEAUSY3OQX/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/RMA3POELBUPIXJ7U7W75NNX5FEOTQZS7/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] A shortcut to load a JSON file into a dict : json.loadf

2020-09-11 Thread The Nomadic Coder
Hi All,

This is the first time I'm posting to this mailing group, so forgive me if I'm 
making any mistakes.

So one of the most common ways to load json, is via a file. This is used 
extensively in data science and the lines. We often write something like :-

with open(filename.json, "r") as f:
my_dict = json.load(f)

or  
my_dict = json.load(open("filename.json", "r"))

Since this is so common, why doesn't python have something like :-
json.loadf("filename.json")

Is there an obvious issue by defining this in the cpython? I don't whipping up 
a PR if it gains traction.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YHO575YY4FQC3GBDF4SKOWIEAUSY3OQX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: 'Infinity' constant in Python

2020-09-11 Thread Guido van Rossum
On Fri, Sep 11, 2020 at 1:10 PM David Mertz  wrote:

> On Fri, Sep 11, 2020 at 8:58 AM Guido van Rossum  wrote:
>
>> What's wrong with 1e1000?
>>
>
> As a spelling of "infinity" generically, or just as an example of an
> "arithmetic operation"?
>
> On the latter, I didn't use that just because it feels sort of like a
> "cheat" rather than an "operation."  I.e. my longer example started out
> with some "reasonable" numbers, but wound up tripping into inf and nan.
>

Presumably this is all meant to counter Stephen Turnbull's claim:

inf and nan only exist in Python the
>
language (including builtins) via casting strings to floats (there are
> no arithmetic operations that produce them).
>

While one may argue that writing `1e1000` is not an "arithmetic operation",
certainly it's certainly not "casting strings to floats", and it's the
simeplest way of producing `inf` in a pinch (in theory it's not portable,
but I think "in a pinch" means you don't care about that).

I don't actually understand why Stephen made this claim about arithmetic
operations, since inf and nan exist *exactly* because arithmetic operations
may produce them. And you don't need to involve pi either, just `1e300 *
1e300` does it.

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JXKM7KQY3BVTYB7H3GNGJD427SUG2JJK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: 'Infinity' constant in Python

2020-09-11 Thread David Mertz
On Fri, Sep 11, 2020 at 8:58 AM Guido van Rossum  wrote:

> What's wrong with 1e1000?
>

As a spelling of "infinity" generically, or just as an example of an
"arithmetic operation"?

On the latter, I didn't use that just because it feels sort of like a
"cheat" rather than an "operation."  I.e. my longer example started out
with some "reasonable" numbers, but wound up tripping into inf and nan.

-- 
The dead increasingly dominate and strangle both the living and the
not-yet born.  Vampiric capital and undead corporate persons abuse
the lives and control the thoughts of homo faber. Ideas, once born,
become abortifacients against new conceptions.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/35SCSRK2E2HIBVUUWK4FBRZPZE7TUXSL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: 'Infinity' constant in Python

2020-09-11 Thread Guido van Rossum
What's wrong with 1e1000?

On Fri, Sep 11, 2020 at 11:51 AM David Mertz  wrote:

> On Fri, Sep 11, 2020 at 6:48 AM Stephen J. Turnbull
>
>>  inf and nan only exist in Python the
>>
> language (including builtins) via casting strings to floats (there are
>> no arithmetic operations that produce them).
>
>
> >>> import sys; sys.version
> '3.8.3 (default, May 19 2020, 18:47:26) \n[GCC 7.3.0]'
> >>> from math import pi as π
> >>> exp = 2**9
> >>> a = (22/7) ** exp1
> >>> b = π ** exp1
> >>> a
> 4.2679182652117097e+254
> >>> a * a
> inf
> >>> (a*a)/(b*b)
> nan
>
> ---
> The dead increasingly dominate and strangle both the living and the
> not-yet born.  Vampiric capital and undead corporate persons abuse
> the lives and control the thoughts of homo faber. Ideas, once born,
> become abortifacients against new conceptions.
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/SI55GC4JYNC6XMVJRNT7SBVWHDIHRALK/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YECRUOVLHXVVGYBTFEDUUGYZOGKFHM55/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: 'Infinity' constant in Python

2020-09-11 Thread David Mertz
On Fri, Sep 11, 2020 at 6:48 AM Stephen J. Turnbull

>  inf and nan only exist in Python the
>
language (including builtins) via casting strings to floats (there are
> no arithmetic operations that produce them).


>>> import sys; sys.version
'3.8.3 (default, May 19 2020, 18:47:26) \n[GCC 7.3.0]'
>>> from math import pi as π
>>> exp = 2**9
>>> a = (22/7) ** exp1
>>> b = π ** exp1
>>> a
4.2679182652117097e+254
>>> a * a
inf
>>> (a*a)/(b*b)
nan

---
The dead increasingly dominate and strangle both the living and the
not-yet born.  Vampiric capital and undead corporate persons abuse
the lives and control the thoughts of homo faber. Ideas, once born,
become abortifacients against new conceptions.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/SI55GC4JYNC6XMVJRNT7SBVWHDIHRALK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: 'Infinity' constant in Python

2020-09-11 Thread Stephen J. Turnbull
Christopher Barker writes:
 > On Fri, Sep 11, 2020 at 12:09 AM Random832  wrote:

 > > What if we created a new syntax [and used it for the repr] that is not
 > > currently a valid identifier?
 > >
 > > something like "1.INF"

Not for this please; save it for a case where we need it.  I'm against
any change by TOOWTDI and because inf and nan only exist in Python the
language (including builtins) via casting strings to floats (there are
no arithmetic operations that produce them).  It seems right and
proper to me that you import these from math (amusingly, math by
itself doesn't seem to be enough to produce inf: the closest I've
found is math.tan(math.pi/2.0) = 1.633123935319537e+16, and of course
math.cot doesn't exist, while math.cos(0.0)/math.sin(0.0) just gives a
ZeroDivisionError).

But if there is going to be a change, just import inf and nan into the
builtin namespace as identifiers.  Steven d'Aprano made a pretty
convincing case that you have to do something pretty perverse for this
to cause trouble.

Christopher Barker writes:

 > And I'd maybe go with .INF. :-)

Syntax using a leading dot has been proposed multiple times, most
recently in the pattern matching thread (PEP 622 I think?).  In the
past it has been proposed as an abbreviation for the self.x = x
pattern in __init__, and also in a Pascal-like with statement.  I
don't think we want to use it for something as small as fixing up one
repr.

 > Another point is that these are not going to be singletons, like True,
 > False and None

Singleton isn't the problem here.  inf = None causes no problems if
that's what you want to do.  It's that those three names are keywords,
and can't be used as identifiers.  True and False caused problems
because by introducing them the common idiom "False, True = 0, 1"
became a syntax error.

 > -- they are just floats that happen to have particular values.

s/floats/identifiers/

The values are floats, but inf and nan are syntactically identifiers,
not floats.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/55XH32PFJXRZUFPQGTSYJAMVNRO55DA7/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: 'Infinity' constant in Python

2020-09-11 Thread Christopher Barker
On Fri, Sep 11, 2020 at 12:09 AM Random832  wrote:

> On Fri, Sep 4, 2020, at 12:45, Cade Brown wrote:
> > I am positing that Python should contain a constant (similar to True,
> > False, None), called Infinity.
>
> What if we created a new syntax [and used it for the repr] that is not
> currently a valid identifier?
>
> something like "1.INF"
>

if we were starting from scratch, that would be a good idea, but as the
repr has been consistent for quite some timem I dont think we want to
change it. And we do want teh names to match the repr

And I'd maybe go with .INF. :-)

Another point is that these are not going to be singletons, like True,
False and None -- they are
just floats that happen to have particular values.

-CHB



> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/BN5ZRN5IWERVBZFUSPBD4RJKQ7FGO5HB/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/C47U7YAHI6DGSEM52RCB6WGOLWUEZHXP/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: 'Infinity' constant in Python

2020-09-11 Thread Cade Brown
This syntax seems ugly to me, clunky, and as you said probably breaks
existing code

This, to me, is less clear than current methods of generating an 'inf'
which is the whole reason I proposed it

Thanks,

*Cade Brown*
Research Assistant @ ICL (Innovative Computing Laboratory)
Personal Email: brown.c...@gmail.com
ICL/College Email: c...@utk.edu




On Fri, Sep 11, 2020 at 9:38 AM Ricky Teachey  wrote:

> On Fri, Sep 11, 2020 at 3:09 AM Random832  wrote:
>
>> On Fri, Sep 4, 2020, at 12:45, Cade Brown wrote:
>> > I am positing that Python should contain a constant (similar to True,
>> > False, None), called Infinity.
>>
>> What if we created a new syntax [and used it for the repr] that is not
>> currently a valid identifier?
>>
>> something like "1.INF"
>>
>
>  This is out of the box and might be considered insane, but what about:
>
> >>> INF#
> INF#
> >>> INF # this is a comment as usual
> NameError: INF
>
> But I suppose this would be considered a breaking change, since the text
> "INF#" probably exists in code somewhere.
>
> ---
> Ricky.
>
> "I've never met a Kentucky man who wasn't either thinking about going home
> or actually going home." - Happy Chandler
>
>
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/VAJC2IEAQ4CHVE24JLZOYPYGIRHSOWGG/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/NIGHQJZ7QXXBEXFRKBWDP6R235CDDBED/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: 'Infinity' constant in Python

2020-09-11 Thread Ricky Teachey
On Fri, Sep 11, 2020 at 3:09 AM Random832  wrote:

> On Fri, Sep 4, 2020, at 12:45, Cade Brown wrote:
> > I am positing that Python should contain a constant (similar to True,
> > False, None), called Infinity.
>
> What if we created a new syntax [and used it for the repr] that is not
> currently a valid identifier?
>
> something like "1.INF"
>

 This is out of the box and might be considered insane, but what about:

>>> INF#
INF#
>>> INF # this is a comment as usual
NameError: INF

But I suppose this would be considered a breaking change, since the text
"INF#" probably exists in code somewhere.

---
Ricky.

"I've never met a Kentucky man who wasn't either thinking about going home
or actually going home." - Happy Chandler
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/VAJC2IEAQ4CHVE24JLZOYPYGIRHSOWGG/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: 'Infinity' constant in Python

2020-09-11 Thread Random832
On Fri, Sep 4, 2020, at 12:45, Cade Brown wrote:
> I am positing that Python should contain a constant (similar to True, 
> False, None), called Infinity.

What if we created a new syntax [and used it for the repr] that is not 
currently a valid identifier?

something like "1.INF"
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BN5ZRN5IWERVBZFUSPBD4RJKQ7FGO5HB/
Code of Conduct: http://python.org/psf/codeofconduct/