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 OP's suggestions.

---
Ricky.

"I've never met a Kentucky man who wasn't either thinking about going home
or actually going home." - Happy Chandler


On Tue, Jan 21, 2020 at 2:03 PM Todd <toddr...@gmail.com> wrote:

> On Tue, Jan 21, 2020 at 1:49 PM Johan Vergeer <johanverg...@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.
> _______________________________________________
> 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-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/
>
_______________________________________________
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-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/VQ2QBT6ECJ2KSM4DS3WIXERAKAI5ZFFH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to