[Python-ideas] Re: PEP 637 - support for indexing with keyword arguments

2020-10-06 Thread Stefan Behnel
Stefan Behnel schrieb am 29.09.20 um 11:48: > Salve Stefano, > > Stefano Borini schrieb am 23.09.20 um 22:55: >> "Support for indexing with keyword arguments" has now been merged with >> the assigned PEP number 637. > > Cool, this looks like a great addition to the language! > > One thing that I

[Python-ideas] CPP Namespaces For Python

2020-10-06 Thread Alperen Keleş
Hi, Please pardon me if my idea is not making sense or already exists, I'm kind of new to developing in Python but I had this idea today and I wanted to share it with you. I think a class type such as "@functionclass" may be helpful for creating functions intended to keep a list of methods in a s

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Michael Smith
On Tue, Oct 6, 2020 at 00:37 Kevin Mills wrote: > str_iterator, bytes_iterator, range_iterator, list_iterator, and > tuple_iterator (and probably others) should have a method that is capable > of efficiently advancing the iterator, instead of having to call next > repeatedly. > > I suggest adding

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Irit Katriel via Python-ideas
Hi Alperen,  Why do you need a class at all rather than just a module with some functions? Irit On Tuesday, October 6, 2020, 01:38:21 PM GMT+1, Alperen Keleş wrote: Hi, Please pardon me if my idea is not making sense or already exists, I'm kind of new to developing in Python but I had

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Henk-Jaap Wagenaar
I cannot answer for Alperen, but I commonly encounter this when writing testing code: generally I use the format: some_module.py tests/test_some_module.py where it is expected the filename to test a module is "test_module_name.py". However, within that, I might want to namespace based on the clas

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Alperen Keleş
Hi Irit, In my case, the code structure is as below. I'm writing a city traffic simulator which includes roads and cars. Cars have different states, MovingCar, IdleCar, ParkingCar... A car can move between different states and it keeps the same information. My solution to this was, Having a d

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Ricky Teachey
cf. this relatively recent conversation on the same topic-- worth reading in entirety: https://mail.python.org/archives/list/python-ideas@python.org/thread/TAVHEKDZVYKJUGZKWSVZVAOGBPLZVKQG/ As I said in that conversation, in the past I have wanted to have module-like namespaces inside of modules

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Guido van Rossum
I think the OP would be happy with a decorator they can just copy-paste. All it needs to do is go over the class dict and apply @classmethod to every “normal” function. Probably skip under names. On Tue, Oct 6, 2020 at 06:46 Ricky Teachey wrote: > cf. this relatively recent conversation on the s

[Python-ideas] PEP 637 - problems not solved by existing language

2020-10-06 Thread Jonathan Fine
SUMMARY This post asks for some examples that will improve PEP 637, by providing examples where the existing language specification is not adequate (but PEP 637 is). The rest of this post should tell you why I want these examples, and what they should look like. DISCLAIMER I'm an editor of PEP 637

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Eric V. Smith
I was just working on that, although I prefer staticmethod: def allstatic(cls):     for key, value in cls.__dict__.items():     if not key.startswith('__'):     setattr(cls, key, staticmethod(value))     return cls @allstatic class C:     def foo(a, b):     print(f'{a}, {b}') C.

[Python-ideas] Re: PEP 637 - problems not solved by existing language

2020-10-06 Thread Chris Angelico
On Wed, Oct 7, 2020 at 2:08 AM Jonathan Fine wrote: > Aside. Please treat this as a semantic problem, or in other words assume that > >>> x[SOMETHING] > >>> f(SOMETHING) > impose the same constraint on a well-formed expression SOMETHING. > Be aware that this isn't an "expression" in the t

[Python-ideas] Re: Fwd: Re: Experimenting with dict performance, and an immutable dict

2020-10-06 Thread Marco Sulla
In the meanwhile, I updated the code of frozendict to the new 3.10 code. And here I need some help. As you can see by the new benchs: https://github.com/Marco-Sulla/cpython/blob/frozendict/frozendict/test/bench.txt creation of frozendict is not faster anymore. This is because Inada introduced mem

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Kevin Mills
Sorry for the duplicate message. I realized two seconds after I sent it, that I only replied to you and not the group. I didn't see the `consume` recipe until after I posted, or I probably would've mentioned it. What I want would have to be done in C, because `it_index` (as `listiterobject` and

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Christopher Barker
NOTE 1: After writing this whole post, I realized that while you used @classmethod, what you seem to really want is a fully @staticmethod class -- certainly your example was a static method, and some other posters were talking about static method (i.e. "it's bad style to have unused parameters in a

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Marco Sulla
What I do not understand is why you need to use the iterator instead of using the iterable itself. This way you can jump to whatever position without slicing. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Christopher Barker
I suspect you will encounter a bit of resistence here, because itertools is about, well, itertors, and designbed to work with arbitrary iterables, and what you are looking for is somnethign optimized for Sequences. Which doesn't mean it couldn't be there. > Slicing large lists/strings/bytes/tuples

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Christopher Barker
On Tue, Oct 6, 2020 at 10:14 AM Marco Sulla wrote: > What I do not understand is why you need to use the iterator instead > of using the iterable itself. This way you can jump to whatever > position without slicing. > if you want the Nth item, that's easy, yes. if you want to iterate through it

[Python-ideas] SupportsString Protocol Type Annotation

2020-10-06 Thread aleksiy123
Currently there are a few Protocols https://docs.python.org/3/library/typing.html#protocols defined in the typing module. One that I recently felt like was missing was a typing annotation for classes with a __str__() method defined. Seems like a fairly straightforward implementation. @runtime_chec

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Alex Hall
On Tue, Oct 6, 2020 at 7:21 PM Christopher Barker wrote: > > > On Tue, Oct 6, 2020 at 10:14 AM Marco Sulla > wrote: > >> What I do not understand is why you need to use the iterator instead >> of using the iterable itself. This way you can jump to whatever >> position without slicing. >> > > if

[Python-ideas] Re: SupportsString Protocol Type Annotation

2020-10-06 Thread Guido van Rossum
The nice thing about Protocols is that they are so easy to define, you can just define them yourself. There's no need to have a "standard" protocol in typing.py for ever single dunder method in existence. PS. There's no need for `__slots__ = ()` assuming you're not going to inherit directly from i

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Christopher Barker
On Tue, Oct 6, 2020 at 10:28 AM Alex Hall wrote: > > if you want to iterate through items N to the end, then how do you do that > without either iterating through the first N and throwing them away, or > making a slice, which copies the rest of the sequence? > > ```python > for i in range(start,

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Wes Turner
So, Sequence views that do direct addressing with doubly-linked lists? https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence https://docs.python.org/3/library/stdtypes.html#dict-views : > The objects returned by dict.keys(), dict.values() and dict.items() are view obje

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Wes Turner
Arrow Buffers, memoryview, array.array Apache Arrow Buffers support zero-copy slicing: > arrow::Buffer can be zero-copy sliced to permit Buffers to cheaply reference other Buffers, while preserving memory lifetime and clean parent-child relationships. > > There are many implementations of arrow::

[Python-ideas] Re: PEP 637 - support for indexing with keyword arguments

2020-10-06 Thread Guido van Rossum
On Tue, Oct 6, 2020 at 1:16 AM Stefan Behnel wrote: > Stefan Behnel schrieb am 29.09.20 um 11:48: > > One thing that I'm missing from the PEP is the C side of things, though. > > How are C extension types going to implement this? > > > > Will there be two new slot methods for them? Something like

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Chris Angelico
On Wed, Oct 7, 2020 at 4:53 AM Christopher Barker wrote: > > On Tue, Oct 6, 2020 at 10:28 AM Alex Hall wrote: >> >> >> if you want to iterate through items N to the end, then how do you do that >> without either iterating through the first N and throwing them away, or >> making a slice, which c

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread David Mertz
On Tue, Oct 6, 2020, 1:21 PM Christopher Barker > if you want to iterate through items N to the end, then how do you do that > without either iterating through the first N and throwing them away, or > making a slice, which copies the rest of the sequence? > it = (lst[i] for i in range(N, len(lst)

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Caleb Donovick
I am +0.3 on this as I don't personally have a need for this but do see the utility. I can think of a number of examples where an `__advance__` would be preferable to any of the proposed solutions: A skip list which doesn't support O(1) random access but can advance faster than naively calling nex

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Josh Rosenberg
This: def advance(it, n): try: return it[n:] except TypeError: return itertools.islice(it, n, None) has the disadvantages of: 1. Requiring a temporary copy of the data sliced (if len(it) is 1_000_000, and n is 500_000, you're stuck between 500_000 pointless __next__ calls

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Chris Angelico
On Wed, Oct 7, 2020 at 9:06 AM Josh Rosenberg wrote: > > This: > > def advance(it, n): > try: > return it[n:] > except TypeError: > return itertools.islice(it, n, None) > > has the disadvantages of: > > 1. Requiring a temporary copy of the data sliced (if len(it) is 1_000_0

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Guido van Rossum
I’m still waiting for an example of a real app where this matters. -- --Guido (mobile) ___ 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-ide

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Steven D'Aprano
On Tue, Oct 06, 2020 at 02:27:54PM -0700, Caleb Donovick wrote: > I am +0.3 on this as I don't personally have a need for this but do see the > utility. > > I can think of a number of examples where an `__advance__` would be > preferable to any of the proposed solutions: [...] For `__advance__` t

[Python-ideas] Re: SupportsString Protocol Type Annotation

2020-10-06 Thread Steven D'Aprano
On Tue, Oct 06, 2020 at 09:30:06AM -0700, aleksiy123 wrote: > Currently there are a few Protocols > https://docs.python.org/3/library/typing.html#protocols defined in the > typing module. One that I recently felt like was missing was a typing > annotation for classes with a __str__() method defined

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Random832
On Tue, Oct 6, 2020, at 02:50, Alperen Keleş wrote: > Hi, > > Please pardon me if my idea is not making sense or already exists, I'm > kind of new to developing in Python but I had this idea today and I > wanted to share it with you. > > I think a class type such as "@functionclass" may be help

[Python-ideas] Re: SupportsString Protocol Type Annotation

2020-10-06 Thread Ricky Teachey
On Tue, Oct 6, 2020 at 9:35 PM Steven D'Aprano wrote: > On Tue, Oct 06, 2020 at 09:30:06AM -0700, aleksiy123 wrote: > > Currently there are a few Protocols > > https://docs.python.org/3/library/typing.html#protocols defined in the > > typing module. One that I recently felt like was missing was a

[Python-ideas] Re: PEP 637 - problems not solved by existing language

2020-10-06 Thread Steven D'Aprano
Jonathan, It sounds like you want to propose a competing PEP that provides your kwkey as a built-in as an alternative to PEP 637. I encourage you to do so, although I have no interest in acting as sponsor, and I fear that your window of opportunity to write a competing PEP is small. Alternati

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Ricky Teachey
On Tue, Oct 6, 2020 at 9:49 PM Random832 wrote: > On Tue, Oct 6, 2020, at 02:50, Alperen Keleş wrote: > > I think a class type such as "@functionclass" may be helpful for > > creating functions intended to keep a list of methods in a scope. > > > > At the moment, I achieved this via writing "@cla

[Python-ideas] Re: PEP 637 - problems not solved by existing language

2020-10-06 Thread Steven D'Aprano
On Tue, Oct 06, 2020 at 04:04:37PM +0100, Jonathan Fine wrote: > PEP 1, which defines the PEP process, states that any PEP that changes the > existing language specification should clearly explain "why the existing > language specification is inadequate to address the problem that the PEP > solves

[Python-ideas] Re: PEP 637 - problems not solved by existing language

2020-10-06 Thread Chris Angelico
On Wed, Oct 7, 2020 at 1:46 PM Steven D'Aprano wrote: > > Recall that we already have literal expressions such as > > >>> [1, 2, 3] # List literal > > >>> (1, 2, 3) # Tuple literal > > >>> {1, 2, 3} # Set literal > > Point of terminology: these are not literals (although informally peo

[Python-ideas] Re: SupportsString Protocol Type Annotation

2020-10-06 Thread Guido van Rossum
On Tue, Oct 6, 2020 at 6:50 PM Ricky Teachey wrote: > On Tue, Oct 6, 2020 at 9:35 PM Steven D'Aprano > wrote: > >> On Tue, Oct 06, 2020 at 09:30:06AM -0700, aleksiy123 wrote: >> > Currently there are a few Protocols >> > https://docs.python.org/3/library/typing.html#protocols defined in the >> >

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Guido van Rossum
On Tue, Oct 6, 2020 at 6:47 PM Random832 wrote: > On Tue, Oct 6, 2020, at 02:50, Alperen Keleş wrote: > > Please pardon me if my idea is not making sense or already exists, I'm > > kind of new to developing in Python but I had this idea today and I > > wanted to share it with you. > > > > I think

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Christopher Barker
On Tue, Oct 6, 2020 at 6:16 PM Steven D'Aprano wrote: > > My ladder two examples demonstrate that this could have utility outside > of > > sequences but for iterators in general. > > I'm sorry, I don't know what your ladder two examples are. Did you post > them in another thread? > I think that

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Greg Ewing
In Python 3 you can do this without any decorators. The following works and produces the same output: class AA: def greet(A_instance): print("Hello", A_instance.name) class BB: def greet(A_instance): print("Hi", A_instance.name) class A: def __init__(self, state, name): self.

[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Guido van Rossum
On Tue, Oct 6, 2020 at 18:16 Steven D'Aprano wrote: > For `__advance__` to be an official Python protocol, it would almost > certainly have to be of use for *general purpose iterators*, not just > specialised ones -- and probably not *hypothetical* iterators which may > not even exist. Do you hav

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Marco Sulla
On Tue, 6 Oct 2020 at 15:33, Alperen Keleş wrote: > Cars have different states, MovingCar, IdleCar, ParkingCar... Well, IMHO the solution is quite more simple: class Car: def __init__(self): self.state = "parking" def move(self): if self.state != "moving": ra

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Greg Ewing
On 7/10/20 2:45 pm, Random832 wrote: I think the feature should allow for the functions to directly access each other from the namespace's scope without requiring an attribute lookup. That got me thinking, and I came up with this: def submodule(f): return type(f.__name__, (), f()) @submodul

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Christopher Barker
I have no idea if this is a good idea, but Python already has modules to be namespaces for a collection of functions and values. And while a class decorator is *supposed* to return a class, it can, in fact, return anything. So you can make a decorator that converts a class definition to a module o

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Greg Ewing
New improved version: def submodule(f): co = f.__code__ i = len(co.co_consts) b = bytes([0x64, i, 0x83, 0x0, 0x53, 0x0]) f.__code__ = co.replace( co_consts = co.co_consts + (locals,), co_code = co.co_code[:-4] + b ) return type(f.__name__, (), f()) @submodule def Stuff():