[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-11-20 Thread Rhodri James
On 20/11/2019 08:25, Stephen J. Turnbull wrote: Rob Cliffe via Python-ideas writes: > I (and no doubt others) am less likely to engage with a thread if I > have to spend more time than necessary trying to understand it. You write as if that's a bad thing.;-) I consider that I often make

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-11-20 Thread Stephen J. Turnbull
Rob Cliffe via Python-ideas writes: > I (and no doubt others) am less likely to engage with a thread if I > have to spend more time than necessary trying to understand it. You write as if that's a bad thing. ;-) I consider that I often make the largest contribution to this list by reading

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-11-19 Thread Rob Cliffe via Python-ideas
On 20/10/2019 03:56:22, Steve Jorgensen wrote: When using a custom classdict to implement a DSL or use in the body of a class definition, from what I can tell by experiment, the classdict takes priority, and the surrounding context is only referenced if trying to get an item from the

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-22 Thread Ethan Furman
On 10/22/2019 03:13 PM, Steve Jorgensen wrote: Ethan Furman wrote: Experimenting is good! However, you'll want to either build your own metaclass and/or prepared dict, or do some work on your __new__/__init__ methods for building enum members. Currently, you are reassigning _value_ in

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-22 Thread Steve Jorgensen
Ethan Furman wrote: > > Experimenting is good! However, you'll want to either build your own > > metaclass > and/or prepared dict, or do some work on your __new__/__init__ > methods for building enum members. Currently, you are reassigning _value_ in > __init__, which leaves some internal

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-22 Thread Ethan Furman
On 10/21/2019 10:33 PM, Steve Jorgensen wrote: class ChoiceEnum(Enum): def __init__(self, src=None, label=None): super().__init__() if isinstance(src, Label): value = None label = str(src) else:

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-21 Thread Steve Jorgensen
Andrew Barnert wrote: > On Oct 20, 2019, at 03:36, Steve Jorgensen ste...@stevej.name wrote: > And there are multiple PyPI projects that build Django choices on top of > Enum, which > show that the only thing you need is to override __getitem__ to replace its > mapping > behavior with sequence

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-21 Thread Steve Jorgensen
Andrew Barnert wrote: > On Oct 20, 2019, at 03:36, Steve Jorgensen ste...@stevej.name wrote: > Maybe the additional functionality in Enum gets in your way for some reason I > don’t > understand, that doesn’t affect other people who want Django choices. But > otherwise, I > don’t see how it

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-21 Thread Steve Jorgensen
Steve Jorgensen wrote: > Oops. Obviously, the above is pseudo-code because I left ('~OTHER~', > 'Something > else') the first assert, and I have (Food, Food) instead of > (Food,) * 4 for the second assert. Possibly some other mistakes as well. Umm, like the fact that I'm testing for colors

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-21 Thread Steve Jorgensen
Oops. Obviously, the above is pseudo-code because I left `('~OTHER~', 'Something else')` the first assert, and I have `(Food, Food)` instead of `(Food,) * 4` for the second assert. Possibly some other mistakes as well. ___ Python-ideas mailing list --

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-21 Thread Steve Jorgensen
Anders Hovmöller wrote: > We try to do the same thing in various libraries. We've settled on using > existing > python and end up with syntax like: > class MyForm(Form): > field = Field() > or in your case > class Colors(TokenContainer): > red = Token() > green = Token() > blue =

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-20 Thread Andrew Barnert via Python-ideas
On Oct 20, 2019, at 03:36, Steve Jorgensen wrote: > > Andrew Barnert wrote: >> What you’re describing is just enum.Enum, minus half its features, but a >> sequence >> instead of a mapping. >> I’m not sure why you’d ever want this, > > Actually, it's a bit different. This is for building a

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-20 Thread Ethan Furman
On 10/19/2019 11:25 PM, Andrew Barnert via Python-ideas wrote: On Oct 19, 2019, at 22:57, Steve Jorgensen wrote: The idea is to use a class as a singleton collection of choices where every choice appears both as an attribute of the class/singleton and as a value/label pair when treating it

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-20 Thread Anders Hovmöller
> On 20 Oct 2019, at 12:41, Steve Jorgensen wrote: > > Anders Hovmöller wrote: >> We try to do the same thing in various libraries. We've settled on using >> existing >> python and end up with syntax like: >> class MyForm(Form): >>field = Field() >> or in your case >> class

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-20 Thread Steve Jorgensen
Anders Hovmöller wrote: > We try to do the same thing in various libraries. We've settled on using > existing > python and end up with syntax like: > class MyForm(Form): > field = Field() > or in your case > class Colors(TokenContainer): > red = Token() > green = Token() > blue =

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-20 Thread Steve Jorgensen
Andrew Barnert wrote: > What you’re describing is just enum.Enum, minus half its features, but a > sequence > instead of a mapping. > I’m not sure why you’d ever want this, Actually, it's a bit different. This is for building a collection of value/label pairs where the values are as likely to

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-20 Thread Steve Jorgensen
Steven D'Aprano wrote: > I've read the entire thread, and I don't understand what builtins has to > do with your problem. Just that, as a workaround, I can have the classdict object raise KeyError for anything that's a member of builtins so that the builtins are used instead of being masked.

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-20 Thread Steven D'Aprano
On Sun, Oct 20, 2019 at 02:56:22AM -, Steve Jorgensen wrote: > I have been able to at least unmask builtins by having the classdict > object first try to get a result from `__builtins__` and then fall > back to itself. I've read the entire thread, and I don't understand what builtins has

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-20 Thread Andrew Barnert via Python-ideas
On Oct 19, 2019, at 22:57, Steve Jorgensen wrote: > > The idea is to use a class as a singleton collection of choices where every > choice appears both as an attribute of the class/singleton and as a > value/label pair when treating it as a sequence. What you’re describing is just enum.Enum,

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-20 Thread Anders Hovmöller
We try to do the same thing in various libraries. We've settled on using existing python and end up with syntax like: class MyForm(Form): field = Field() or in your case class Colors(TokenContainer): red = Token() green = Token() blue = Token() (this is using

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-19 Thread Steve Jorgensen
Several requests for a specific use case. This is something I have been working on just to practice getting a deeper understanding of Python metaprogramming, so whether this is something that someone should be trying to do is, of course, a matter of opinion. The idea is to use a class as a

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-19 Thread Steve Jorgensen
Steve Jorgensen wrote: > I'm not sure what the best way to deal with this would be, but my first > thought is to > maybe have a property that can be set on the metaclass type such as > metacls.relictant_classdict = True. Oops. Typo. That should say `metacls.reluctant_classdict = True`.

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-19 Thread Kyle Stanley
> That said, though, I'm not sure that this was even what you were asking about :) It would be significantly more helpful if the OP had an example for the specific case where they wanted contextually defined variables to take priority over the classdict, and explained _why_ this behavior might be

[Python-ideas] Re: Ability to set precedence of classdict (returned from __prepare__) for a metaclass type

2019-10-19 Thread Ethan Furman
On 10/19/2019 07:56 PM, Steve Jorgensen wrote: When using a custom classdict to implement a DSL or use in the body of a class definition, from what I can tell by experiment, the classdict takes priority, and the surrounding context is only referenced if trying to get an item from the