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

Document tuples and __new__ #114071

Closed
Closed
Assignees
ethanfurman
Labels
docsDocumentation in the Doc dir
@blhsing

Description

@blhsing

In this StackOverflowanswer, Ethan Furman, the author of theenum module, demonstrated how a tuple value isspecial-cased so that it can beunpacked as arguments to the constructor of the mixin type, making it possible to extremely elegantly implement a member type with additional information such as a label for each member as requested by the SO question:

fromenumimportEnumclassLabelledEnumMixin:labels= {}def__new__(cls,value,label):member=object.__new__(cls)member._value_=valuemember.label=labelcls.labels[value]=labelreturnmember@classmethoddeflist_labels(cls):returnlist(lforc,lincls.labels.items())classTest(LabelledEnumMixin,Enum):A=1,"Label A"B=2,"Custom B"C=3,"Custom label for value C + another string"print(list(Test))print(Test.list_labels())

This outputs:

[<Test.A: 1>, <Test.B: 2>, <Test.C: 3>]['Label A', 'Custom B', 'Custom label for value C + another string']

Such a neat behavior of a tuple value is currently undocumented in theSupported__dunder__ names section of theenum's docs, however, making it more of an implementation detail rather than a publicly usable feature.

Please help document this feature properly so we can all benefit from the new possibilities this feature enables without fearing that we are using an undocumented implementation detail. Thanks.

Linked PRs

Metadata

Metadata

Assignees

Labels

docsDocumentation in the Doc dir

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp