[Python-ideas] Re: addition of "nameof" operator

2020-01-22 Thread Anders Hovmöller
> On 21 Jan 2020, at 22:50, Andrew Barnert via Python-ideas > wrote: > > But it seems like nameof(self.age) always has to be just “age”, which you > already have to type inside the nameof if you want it to be findable, so > it’ll never be useful. Anywhere you could write `{nameof(self.age)}`

[Python-ideas] Re: addition of "nameof" operator

2020-01-22 Thread Steven D'Aprano
On Tue, Jan 21, 2020 at 02:25:56PM -0500, Ricky Teachey wrote: > Isn't the name of the class more reliably `type(ins).__qualname__`? Depends on whether you want the plain old name of the class, or the qualified name of the class. > At any rate, I've actually wished for a shortcut to the name of

[Python-ideas] Re: Resource imports (as strings/bytes)

2020-01-22 Thread Steven D'Aprano
On Wed, Jan 22, 2020 at 02:39:57PM +0900, Stephen J. Turnbull wrote: > I see some parallels, but I'm definitely in the camp of "confusingly > similar" rather than "instructively similar", and I definitely don't > see a need for a syntax change to enable importing anything inside > expressions. +1

[Python-ideas] Re: addition of "nameof" operator

2020-01-22 Thread Anders Hovmöller
> On 22 Jan 2020, at 11:50, Steven D'Aprano wrote: > > But what should these things do? > >x = y = z = 1 >nameof(1) Syntax error. >nameof(z) "z" >nameof("Gumby") Syntax error. >nameof(mysequence[0]) Syntax error. > It isn't clear what precisely counts as the name o

[Python-ideas] Re: addition of "nameof" operator

2020-01-22 Thread Rhodri James
On 22/01/2020 12:03, Anders Hovmöller wrote: He was pretty clear in saying he wanted the same thing as in C#. That no one in this thread seems to have looked that up is really not his fault in my opinion. Oh, plenty of people have looked it up. The problem is that it relies on part of the na

[Python-ideas] Re: addition of "nameof" operator

2020-01-22 Thread Richard Damon
On 1/22/20 7:08 AM, Rhodri James wrote: On 22/01/2020 12:03, Anders Hovmöller wrote: He was pretty clear in saying he wanted the same thing as in C#. That no one in this thread seems to have looked that up is really not his fault in my opinion. Oh, plenty of people have looked it up.  The pro

[Python-ideas] Re: addition of "nameof" operator

2020-01-22 Thread Anders Hovmöller
> On 22 Jan 2020, at 13:23, Rhodri James wrote: > > On 22/01/2020 12:03, Anders Hovmöller wrote: >> He was pretty clear in saying he wanted the same thing as in C#. That no one >> in this thread seems to have looked that up is really not his fault in my >> opinion. > > Oh, plenty of people

[Python-ideas] Re: addition of "nameof" operator

2020-01-22 Thread Chris Angelico
On Wed, Jan 22, 2020 at 11:57 PM Richard Damon wrote: > Thus nameof(x) can ONLY be "x" in Python (or an error is x isn't > something that is a name), as at best x is referring to some object, but > that object doesn't have a special name to refer to it that is its > holder. Yes, one example wher

[Python-ideas] Re: addition of "nameof" operator

2020-01-22 Thread Alex Hall
You can achieve this now with my executing library: https://github.com/alexmojaki/executing ``` import ast import inspect import executing def nameof(_): frame = inspect.currentframe().f_back call = executing.Source.executing(frame).node arg = call.args[0] if isinstance(arg, ast

[Python-ideas] Re: addition of "nameof" operator

2020-01-22 Thread Anders Hovmöller
> On 22 Jan 2020, at 14:39, Alex Hall wrote: > >  > You can achieve this now with my executing library: > https://github.com/alexmojaki/executing > > ``` > import ast > import inspect > > import executing > > > def nameof(_): > frame = inspect.currentframe().f_back > call = execut

[Python-ideas] Re: addition of "nameof" operator

2020-01-22 Thread Ned Batchelder
On 1/22/20 8:05 AM, Anders Hovmöller wrote: On 22 Jan 2020, at 13:23, Rhodri James wrote: On 22/01/2020 12:03, Anders Hovmöller wrote: He was pretty clear in saying he wanted the same thing as in C#. That no one in this thread seems to have looked that up is really not his fault in my opin

[Python-ideas] Re: docstring for dataclass fields

2020-01-22 Thread Christopher Barker
On Tue, Jan 21, 2020 at 12:55 PM wrote: > Currently, there is no way to add docstrings to fields in dataclasses. > PEP257 talks about attribute docstrings (a string literal following an > assignment), but these are visual only and not accessible at runtime. My > suggestion would be a simple `doc`

[Python-ideas] Re: docstring for dataclass fields

2020-01-22 Thread Ricky Teachey
The doctrings for fields are usually inside the class object docstring (formatting of which depending on the docstring style). As CHB said, there isn't a way to add them to objects. Maybe there's a way to make this happen, though? Like: @dataclass @build_docstring(format="sphinx") class LameClass

[Python-ideas] Re: docstring for dataclass fields

2020-01-22 Thread contact
The fact that i.__doc__ doesn't work is actually a strong argument. You would have to do dataclasses.fields(my_obj)[0].__doc__, which is already complex enough. At that point using metadata probably makes more sense. Thank you for your response. ___ Py

[Python-ideas] Re: docstring for dataclass fields

2020-01-22 Thread Andrew Barnert via Python-ideas
On Jan 22, 2020, at 13:01, Christopher Barker wrote: > >  >> On Tue, Jan 21, 2020 at 12:55 PM wrote: > >> Currently, there is no way to add docstrings to fields in dataclasses. >> PEP257 talks about attribute docstrings (a string literal following an >> assignment), but these are visual only

[Python-ideas] Re: docstring for dataclass fields

2020-01-22 Thread Ricky Teachey
On Wed, Jan 22, 2020, 7:21 PM Andrew Barnert via Python-ideas < [email protected]> wrote: > On Jan 22, 2020, at 13:01, Christopher Barker wrote: > >  > On Tue, Jan 21, 2020 at 12:55 PM wrote: > >> Currently, there is no way to add docstrings to fields in dataclasses. >> PEP257 talks about

[Python-ideas] Re: addition of "nameof" operator

2020-01-22 Thread Random832
On Tue, Jan 21, 2020, at 16:32, Andrew Barnert via Python-ideas wrote: > What would the semantics of nameof be in Python? Would it just be > lambda obj: obj.__name__? Or some fancy inspect-module style chain of > “try this, then that, then the other”? Or does it need to look at the > compiled so