[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-ideas
Subject:    [Python-ideas] Re: addition of "nameof" operator
From:       Todd <toddrjen () gmail ! com>
Date:       2020-01-21 19:00:45
Message-ID: CAFpSVpLbL=sJQ90UYV6RorOvA2ccLaCF+KVjHTfvZNHohNxbng () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


On Tue, Jan 21, 2020 at 1:49 PM Johan Vergeer <johanvergeer@gmail.com>
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. (
> https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof
> ).
> The place I use this the most in C# is in `ToString()` methods or logging
> messages.
> This makes sure the developer cannot forget to update the name of a member.
>
> As an example I created this `Person` class.
>
> ```
> class Person:
>     def __init__(self, name, age):
>         self.name = name
>         self.age = age
>
>     def __repr__(self):
>         return f"Person(name: {self.name}, age: {self.age})"
> ```
>
> With the `nameof` operator this would look like the following:
>
> ```
> class Person:
>     def __init__(self, name, age):
>         self.name = name
>         self.age = age
>
>     def __repr__(self):
>         return f"{nameof(Person)}({nameof(self.name)}: {self.name},
> {nameof(self.age)}: {self.age})"
> ```
>
> What do you think about this?


We can get the name of the class with "type(ins).__name__".  If you want to
get the names right it would be easier to loop over a list of strings and
use 'getattr' on them, which would mean you only have to change the name in
one place rather than two.

[Attachment #5 (text/html)]

<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr"></div><div \
dir="ltr" class="gmail_attr">On Tue, Jan 21, 2020 at 1:49 PM Johan Vergeer &lt;<a \
href="mailto:johanvergeer@gmail.com">johanvergeer@gmail.com</a>&gt; \
wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I have worked with \
both C# and Python for a while now and there is one feature of C# I&#39;m missing in \
the Python language.<br> <br>
This feature is the &quot;nameof&quot; operator. (<a \
href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof" \
rel="noreferrer" target="_blank">https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof</a>).<br>
 The place I use this the most in C# is in `ToString()` methods or logging \
messages.<br> This makes sure the developer cannot forget to update the name of a \
member.<br> <br>
As an example I created this `Person` class.<br>
<br>
```<br>
class Person:<br>
      def __init__(self, name, age):<br>
            <a href="http://self.name" rel="noreferrer" target="_blank">self.name</a> \
= name<br>  self.age = age<br>
<br>
      def __repr__(self):<br>
            return f&quot;Person(name: {<a href="http://self.name" rel="noreferrer" \
target="_blank">self.name</a>}, age: {self.age})&quot;<br> ```<br>
<br>
With the `nameof` operator this would look like the following:<br>
<br>
```<br>
class Person:<br>
      def __init__(self, name, age):<br>
            <a href="http://self.name" rel="noreferrer" target="_blank">self.name</a> \
= name<br>  self.age = age<br>
<br>
      def __repr__(self):<br>
            return f&quot;{nameof(Person)}({nameof(<a href="http://self.name" \
rel="noreferrer" target="_blank">self.name</a>)}: {<a href="http://self.name" \
rel="noreferrer" target="_blank">self.name</a>}, {nameof(self.age)}: \
{self.age})&quot;<br> ```<br>
<br>
What do you think about this?</blockquote><div><br></div><div>We can get the name of \
the class with &quot;type(ins).__name__&quot;.   If you want to get the names right \
it would be easier to loop over a list of strings and use &#39;getattr&#39; on them, \
which would mean you only have to change the name in one place rather than \
two.<br></div></div></div>



_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/BS4TX33ADL6TQ3HG3CVBVCNO4QHS2TKK/
 Code of Conduct: http://python.org/psf/codeofconduct/



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic