On Wed, Feb 5, 2020 at 5:14 AM Christopher Barker wrote:
>
> > A statement like print("bar", foo.bar) would be very hard to notice that
>>
>> the "bar" match the foo.bar, while
>>
>> print(nameof(foo.bar), foo.bar) makes the connection explicit.
>
>
> Isn’t someone working on a “debug string” PEP
> A statement like print("bar", foo.bar) would be very hard to notice that
> the "bar" match the foo.bar, while
>
> print(nameof(foo.bar), foo.bar) makes the connection explicit.
Isn’t someone working on a “debug string” PEP that would mitigate this?
Maybe not a full PEP:
On 2020-02-04 3:33 a.m., Bruce Leban wrote:
Here is a much more readable alternative which has the advantage that
it is already supported by Python. I'm going to show a harder example
where I want to reference foo.bar and allow both foo and bar to be
refactored. Here's the original
On Mon, Feb 3, 2020 at 3:58 AM Richard Damon
wrote:
>
> IF Python had a proper refactoring tool (see my other message where I
> question the ability to do that) then the nameof operator would clearly
> help keep logging/debug strings in line with the program definitions.
>
> A statement like
On Feb 3, 2020, at 10:25, Andrew Barnert wrote:
>
> This is the same as Smalltalk, ObjC, Ruby, f-script, and all of the other
> languages that use the same dynamic type/data model as Python. And Smalltalk
> was the first language to have practical refactoring tools (although I don’t
> think
On Feb 3, 2020, at 03:59, Richard Damon wrote:
>
> On 2/3/20 12:02 AM, Bruce Leban wrote:
>> People keep mentioning refactoring. It's a red herring.
>>
>> No refactoring tool needs a nameof operator added to the language in order
>> to do its job. And certainly an operator that requires
To restate the motivation: the hope is that there is a potentially large
benefit of being able to more easily refactor code with something like a
nameof().
I am going to make the claim that:
1. this benefit is actually minimal and does not address a lot of other
existing refactoring
On 2/3/20 12:02 AM, Bruce Leban wrote:
People keep mentioning refactoring. It's a red herring.
No refactoring tool needs a nameof operator added to the language in
order to do its job. And certainly an operator that requires running
the program in order to use it is not going to be helpful.
People keep mentioning refactoring. It's a red herring.
No refactoring tool needs a nameof operator added to the language in order
to do its job. And certainly an operator that requires running the program
in order to use it is not going to be helpful. Refactoring tools analyze
the program; they
> On Feb 2, 2020, at 09:14, Johan Vergeer wrote:
>
> Thank you so much for this concrete proposal. I could not have described it
> clearer myself.
>
> I'm not sure about leaving out the parentheses. But this is mostly out of
> preference. Especially since Python also has a `type()`
That's based on my observation of how the discussion went. I have been
participating in such discussions for 30 years now and I can usually tell
how it will end long before most other participants. So I actually cringe
when I see discussion go on beyond the point of usefulness. This is such a
On 2020-02-02 2:00 p.m., Johan Vergeer wrote:
This would look pretty nice when it would just be used in f-strings.
My proposal isn't just about f-strings though. It should also allow developers
to get the name of a variable, class, method or function anywhere else.
where do you want to do
Thank you so much for this concrete proposal. I could not have described it
clearer myself.
I'm not sure about leaving out the parentheses. But this is mostly out of
preference. Especially since Python also has a `type()` function. I also
thought about using `name()` instead of `nameof()` to
This would look pretty nice when it would just be used in f-strings.
My proposal isn't just about f-strings though. It should also allow developers
to get the name of a variable, class, method or function anywhere else.
___
Python-ideas mailing list
On 2020-02-02 12:08 p.m., Jeff Allen wrote:
On 01/02/2020 02:48, Richard Damon wrote:
On 1/31/20 9:28 PM, Christopher Barker wrote:
I am really confused by this whole thread:
My understanding is that the impetus of the request is that if you
start from an expression like nameof(foo.bar)
On 01/02/2020 02:48, Richard Damon wrote:
On 1/31/20 9:28 PM, Christopher Barker wrote:
I am really confused by this whole thread:
My understanding is that the impetus of the request is that if you
start from an expression like nameof(foo.bar) to get to "bar" then if
you refactor your code
Hi Guido,
This thread had a lot of comments so far, both people that are pro and con.
To be honest, a lot of you are a smarter and more experienced than me, so I
receive all of it as learning moments.
What I am wondering about is your personal motivation to say the feature won't
every be a
> On 2 Feb 2020, at 01:00, Christopher Barker wrote:
>
> And no one answered my questions about what it would do in all the, very
> common cases where an object doesn't ahve a name, opr more than one.
This question has been answered multiple times. It's not about the object, it's
about the
On Fri, Jan 31, 2020 at 9:50 PM Richard Damon
wrote:
> On 1/31/20 9:28 PM, Christopher Barker wrote:
> > I am really confused by this whole thread:
>
> My understanding is that the impetus of the request is that if you start
> from an expression like nameof(foo.bar) to get to "bar" then if
The whole feature.
On Sat, Feb 1, 2020 at 14:35 Greg Ewing wrote:
> On 2/02/20 6:12 am, Guido van Rossum wrote:
>
> > Alex, this is not ever going to be added to Python,
>
> For clarity, are you rejecting the whole idea of a "nameof" feature,
> or just bytecode-hack implementations of it?
>
>
On 2/02/20 6:12 am, Guido van Rossum wrote:
Alex, this is not ever going to be added to Python,
For clarity, are you rejecting the whole idea of a "nameof" feature,
or just bytecode-hack implementations of it?
--
Greg
___
Python-ideas mailing list
t;> > speed
>> > scalar
>> >
>> > allowed expressions:
>> > length = length
>> > length + length : length
>> > time = time
>> > time + time : time
>> > scalar * length : length
>> > scalar * time : time
>> &g
On 1/31/20 10:58 PM, Andrew Barnert via Python-ideas wrote:
When refactoring code to rename something, there is no way to tell
whether a string—or a substring in a larger string—that matches the
name should be changed. But a nameof expression is unambiguously a
reference to the name, and
>
>
> This doesn't need a dedicated type system. Those are actual types,
> representing units - though you've omitted the actual units. Create a
> system of classes with actual units, for instance:
>
> class Meter(int):
> ...
>
> class Second(int):
> ...
>
> De
On Thu, Jan 30, 2020 at 07:17:34PM -, Johan Vergeer wrote:
> It is a couple of days later, but I managed to create a more expanded
> proposal.
>
> This proposal is about having a simple and consistent way of getting the name
> of an object.
> Whether it is a class, type, function, method or
On Jan 31, 2020, at 19:30, Soni L. wrote:
>
>
>> On 2020-02-01 12:00 a.m., Andrew Barnert wrote:
>> On Jan 31, 2020, at 14:51, Soni L. wrote:
>> >>> On 2020-01-31 2:36 p.m., Andrew Barnert wrote:
>> >> That does work, but that means foo.bar has to exist and have a value
>> >> before you look
Sorry, sent early… ignore that and try this version.
>
> On Jan 31, 2020, at 19:58, Andrew Barnert wrote:
>
> Here’s a concrete proposal.
>
tl;dr: `nameof x == "x"`, and `nameof x.y == "y"`.
Rationale:
Even though as far as Python is concerned `nameof x.y` is identical to `"y"`,
it can
Here’s a concrete proposal.
tl;dr: `nameof x == "x"`, and `nameof x.y == "y"`.
Rationale:
Even though as far as Python is concerned `nameof x.y` is identical to `"y"`,
it can make a difference to human readers—and to at least two classes of
automated tools.
When refactoring code to rename
On 2020-02-01 12:00 a.m., Andrew Barnert wrote:
On Jan 31, 2020, at 14:51, Soni L. wrote:
>>> On 2020-01-31 2:36 p.m., Andrew Barnert wrote:
>> That does work, but that means foo.bar has to exist and have a value before
you look up the name. Consider these cases:
>>class Spam:
>>
On Jan 31, 2020, at 14:51, Soni L. wrote:
>>> On 2020-01-31 2:36 p.m., Andrew Barnert wrote:
>> That does work, but that means foo.bar has to exist and have a value before
>> you look up the name. Consider these cases:
>>class Spam:
>>def __init__(self):
>>self.eggs = 42
On 1/31/20 9:28 PM, Christopher Barker wrote:
I am really confused by this whole thread:
(I know this has all been said in this thread, but I’m summarizing to
get us on the same page for the following comments)
Python has names, and it has values (objects, whatever) when we write
Python, we
I am really confused by this whole thread:
(I know this has all been said in this thread, but I’m summarizing to get
us on the same page for the following comments)
Python has names, and it has values (objects, whatever) when we write
Python, we bind names to values with the = operator. (And
On Sat, Feb 01, 2020 at 01:52:38AM +0200, Alex Hall wrote:
> Here's a simple runtime implementation that doesn't require any
> dependencies or source code access:
>
> import dis
So this is not likely to ever work in IronPython or Jython.
There are no stability guarantees about the byte-code
Here's a simple runtime implementation that doesn't require any
dependencies or source code access:
import dis
import inspect
from functools import lru_cache
def nameof(_):
frame = inspect.currentframe().f_back
return _nameof(frame.f_code, frame.f_lasti)
@lru_cache
def _nameof(code,
On 1/02/20 6:15 am, Richard Damon wrote:
One issue I am seeing is that x = nameof(foo.bar) is crossing the
line between compile time and run time behaviors. The resultant
string, “bar” needs to be generated at compile time, but the
operation still needs to do a check at run-time to determine if
On 2020-01-31 2:36 p.m., Andrew Barnert wrote:
On Jan 31, 2020, at 08:03, Soni L. wrote:
>
> Consider:
>
> x=nameof(foo.bar)
>
> in today's python becomes:
>
> foo.bar
> x="bar"
>
> and when running this you get an AttributeError or something.
>
> the benefit is that "bar" is only
On Jan 31, 2020, at 08:03, Soni L. wrote:
>
> Consider:
>
> x=nameof(foo.bar)
>
> in today's python becomes:
>
> foo.bar
> x="bar"
>
> and when running this you get an AttributeError or something.
>
> the benefit is that "bar" is only typed once, and the attribute access (and
> thus
On Jan 31, 2020, at 11:01 AM, Soni L. wrote:
>
> Consider:
>
> x=nameof(foo.bar)
>
> in today's python becomes:
>
> foo.bar
> x="bar"
>
> and when running this you get an AttributeError or something.
>
> the benefit is that "bar" is only typed once, and the attribute access (and
> thus
On 2020-01-30 7:17 p.m., Andrew Barnert via Python-ideas wrote:
On Jan 30, 2020, at 11:20, Johan Vergeer wrote:
> ## Attribute names
>
> You should be able to get the name of an attribute.
>
> ```python
> class Foo:
>bar: str = "Hello"
>
>def __init__(self):
>self.baz =
On Jan 30, 2020, at 11:20, Johan Vergeer wrote:
>
> It is a couple of days later, but I managed to create a more expanded
> proposal.
>
> This proposal is about having a simple and consistent way of getting the name
> of an object.
> Whether it is a class, type, function, method or variable
It is a couple of days later, but I managed to create a more expanded proposal.
This proposal is about having a simple and consistent way of getting the name
of an object.
Whether it is a class, type, function, method or variable or any other object.
# Why?
## Usage in strings
The first
Thanks everyone for your responses.
I would like to take all the information you gave me and do some research on
it. I'll try to get it done this weekend or beginning of next week.
___
Python-ideas mailing list -- python-ideas@python.org
To
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
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
> 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 =
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,
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
> 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
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
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
> 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
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
> 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
On 01/21/2020 02:08 PM, Chris Angelico wrote:
Yes, that's exactly what I mean, and exactly why self.__class__ is
used here. If you actually want "Person" even if it's actually a
Martian, you can use __class__.__name__ rather than
self.__class__.__name__ to get that.
Gotcha, thanks. I'm still
On Wed, Jan 22, 2020 at 9:04 AM Ethan Furman wrote:
>
> On 01/21/2020 11:25 AM, Chris Angelico wrote:
>
> > I'm not sure how this compares to what nameof(Person) should return,
> > but the above idiom (or something like it) is very common in Python,
> > as it allows repr to acknowledge a
On 01/21/2020 11:25 AM, Chris Angelico wrote:
I'm not sure how this compares to what nameof(Person) should return,
but the above idiom (or something like it) is very common in Python,
as it allows repr to acknowledge a subclass. If you create "class
Martian(Person): pass", then a Martian's repr
On 1/21/2020 4:50 PM, Eric V. Smith wrote:
On 1/21/2020 4:32 PM, 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
On 1/21/2020 4:32 PM, 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 source code
On Jan 21, 2020, at 13:32, Andrew Barnert wrote:
>
> On Jan 21, 2020, at 10:48, Johan Vergeer wrote:
>>
>> def __repr__(self):
>> return f"{nameof(Person)}({nameof(self.name)}: {self.name},
>> {nameof(self.age)}: {self.age})"
>
> What would the semantics of nameof be in Python?
On Jan 21, 2020, at 10:48, Johan Vergeer wrote:
>
> I have worked with both C# and Python for a while now and there is one
> feature of C# I'm missing in the Python language.
>
> This feature is the "nameof" operator.
>
Why? That looks like more code to accomplish exactly the same thing.
On Tue, Jan 21, 2020 at 1:49 PM Johan Vergeer
wrote:
> I have worked with both C# and Python for a while now and there is one
> feature of C# I'm missing in the Python language.
>
> This feature is the "nameof" operator. (
>
I believe Python 3.8's '=' f-string specifier (
https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging)
combined with __name__ does what you'd want:
return f'{self.__class__.__name__}({self.name=}, {self.age=})'
Outside of the __name__ dance,
On 1/21/20 3:11 PM, Ned Batchelder wrote:
On 1/21/20 1:42 PM, Johan Vergeer wrote:
def __repr__(self):
return f"{nameof(Person)}({nameof(self.name)}: {self.name},
{nameof(self.age)}: {self.age})"
I'm trying to understand what you are looking for. Why isn't the
above line
On 1/21/20 1:42 PM, Johan Vergeer wrote:
def __repr__(self):
return f"{nameof(Person)}({nameof(self.name)}: {self.name},
{nameof(self.age)}: {self.age})"
I'm trying to understand what you are looking for. Why isn't the above
line just:
return f"Person(name: {self.name},
On Wed, Jan 22, 2020 at 6:43 AM Eric V. Smith wrote:
>
> On 1/21/2020 2:25 PM, Chris Angelico wrote:
> >
> > Hmm. Maybe this should be a recipe in the docs, or something: "how to
> > make a repr that reconstructs an object".
> >
> > def describe(obj, attrs):
> > attrs = [f"{a}={getattr(obj,
On 1/21/2020 2:25 PM, Chris Angelico wrote:
Hmm. Maybe this should be a recipe in the docs, or something: "how to
make a repr that reconstructs an object".
def describe(obj, attrs):
attrs = [f"{a}={getattr(obj, a)!r}" for a in attrs]
return f"{type(obj).__name__}({", ".join(attrs)})"
Isn't the name of the class more reliably `type(ins).__qualname__`?
At any rate, I've actually wished for a shortcut to the name of an object's
class- of the name of the class object itself- many times in the past.
Mostly when writing reprs and exception messages. Not sure if that is in
line with
On Wed, Jan 22, 2020 at 5:48 AM Johan Vergeer wrote:
>
> I have worked with both C# and Python for a while now and there is one
> feature of C# I'm missing in the Python language.
>
> This feature is the "nameof" operator.
>
On Tue, Jan 21, 2020 at 1:49 PM Johan Vergeer
wrote:
> I have worked with both C# and Python for a while now and there is one
> feature of C# I'm missing in the Python language.
>
> This feature is the "nameof" operator. (
>
69 matches
Mail list logo