Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Make inspect.Parameter.__hash__ use _default instead of default #102302

Closed
Labels
performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
@Gouvernathor

Description

@Gouvernathor

Feature or enhancement

Enable subclasses of Parameter to add adefault property accessing hash(self) rather than having hash rely on thedefault property

Pitch

I wanted to make a subclass of Parameter which would store as _default the string eval-ing to the actual default value, and which would only eval it when accessed, and memorize it at that time.
So, I coded this :

classParameter(inspect.Parameter):__slots__= ()@property@functools.cachedefdefault(self):returnrenpy.python.py_eval(super(Parameter,self).default)

(not using @cached_property because I want to keep the empty slots)
This doesn't work, becausecache tries to hashself, and Parameter.__hash__ accessesself.default, rather thanself._default (so it does an infinite recursion loop).

The last edit to the hash method was purportedly to "fix a discrepancy between eq and hash". However, the eq still accesses the raw _default, and hash did so too before the "fix".
Overall, apart from my selfish use-case, I think it makes more sense for an immutable class to hash relative to the actual value instead of the less-reliable propertied value.
And there's a minor performance bonus too.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp