Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __name__ to property #101860

Closed
eltoder opened this issue Feb 13, 2023 · 3 comments
Closed

Add __name__ to property #101860

eltoder opened this issue Feb 13, 2023 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@eltoder
Copy link
Contributor

eltoder commented Feb 13, 2023

class C:
    @property
    def foo(self):
        return 1

assert C.foo.__name__ == "foo"

It would be very handy if this worked, so properties can be introspected the same way as functions, classmethods, etc. Name can be simply taken from fget the same way this is done for __isabstractmethod__.

Linked PRs

@eltoder eltoder added the type-feature A feature request or enhancement label Feb 13, 2023
@arhadthedev arhadthedev added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Feb 13, 2023
@sobolevn
Copy link
Member

What about this case?

>>> def getter(o):
...     return 42
... 
>>> class Some:
...    number = property(getter)
... 
>>> Some().number
42

Should it be 'getter'?

@TeamSpen210
Copy link

Actually, property already defines __set_name__(), storing the name internally for use in formatting AttributeErrors. That's called during class creation, so it'll be accurate regardless where the callable comes from. All that's needed is for that to be exposed. This was added in #71981, but it was kept private to avoid expanding the API since a need wasn't specified.

@eltoder
Copy link
Contributor Author

eltoder commented Feb 13, 2023

@TeamSpen210 Nice find! I think the API expansion comments were about adding name as an argument to property constructor. Adding __name__ as a read-only attribute for introspection seems very much in the spirit of python.

eltoder added a commit to eltoder/cpython that referenced this issue Feb 13, 2023
Useful for introspection and consistent with functions and other
descriptors.
eltoder added a commit to eltoder/cpython that referenced this issue Feb 13, 2023
Useful for introspection and consistent with functions and other
descriptors.
eltoder added a commit to eltoder/cpython that referenced this issue Feb 13, 2023
Useful for introspection and consistent with functions and other
descriptors.
eltoder added a commit to eltoder/cpython that referenced this issue Feb 13, 2023
Useful for introspection and consistent with functions and other
descriptors.
eltoder added a commit to eltoder/cpython that referenced this issue Feb 17, 2024
Useful for introspection and consistent with functions and other
descriptors.
serhiy-storchaka pushed a commit that referenced this issue Feb 20, 2024
Useful for introspection and consistent with functions and other
descriptors.
woodruffw pushed a commit to woodruffw-forks/cpython that referenced this issue Mar 4, 2024
Useful for introspection and consistent with functions and other
descriptors.
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
Useful for introspection and consistent with functions and other
descriptors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants