Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

It is surely a great calamity for a human being to have no obsessions. -- Robert Bly


devel / comp.lang.python / Functions as Enum member values

SubjectAuthor
o Functions as Enum member valuesColin McPhail

1
Functions as Enum member values

<mailman.454.1622476626.3087.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=13477&group=comp.lang.python#13477

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: colin.mc...@mac.com (Colin McPhail)
Newsgroups: comp.lang.python
Subject: Functions as Enum member values
Date: Mon, 31 May 2021 16:57:00 +0100
Lines: 90
Message-ID: <mailman.454.1622476626.3087.python-list@python.org>
References: <29936AAC-B092-49DC-8A95-FDFF426DC48D@mac.com>
Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.100.0.2.22\))
Content-Type: text/plain;
charset=us-ascii
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de 7FXutIuqDx3PShEbNklkmAxGCTV7DHFwOKA7mYjsBMAw==
Return-Path: <colin.mcphail@mac.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=mac.com header.i=@mac.com header.b=iXoKjM58;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.005
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; 'else:': 0.09;
'macos': 0.09; 'values.': 0.09; 'import': 0.14; 'problem': 0.15;
'"member': 0.16; 'colin': 0.16; 'individually': 0.16; 'int,':
0.16; 'iterate': 0.16; 'python3': 0.16; 'received:17.58.63': 0.16;
'subject:Functions': 0.16; 'subject:member': 0.16;
'subject:values': 0.16; 'values': 0.16; 'python': 0.16; 'instead':
0.17; 'to:addr:python-list': 0.23; 'wrong': 0.27; 'bit': 0.28;
'there': 0.31; 'member': 0.31; 'script': 0.32; 'received:17':
0.32; 'received:17.58': 0.32; 'received:me.com': 0.32; 'using':
0.33; 'class': 0.33; 'functions': 0.34; 'also,': 0.36; 'hi,':
0.39; 'test': 0.40; 'could': 0.40; "skip:' 10": 0.40; 'normal':
0.60; 'skip:e 10': 0.60; 'reference': 0.61; 'skip:h 10': 0.61;
'gave': 0.61; 'type:': 0.65; 'python.org': 0.68; 'below': 0.71
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mac.com; s=1a1hai;
t=1622476623; bh=kE7XhIQHDVQrWz1VJwch/8ElxyIozO86hMv3K5VFHHk=;
h=From:Content-Type:Mime-Version:Subject:Message-Id:Date:To;
b=iXoKjM58mg9QNoheg9DoUK56hYMbiYH/l8ekWvk09I0I3MF+ZGqWLNWbfDuboHqyi
un+mo2uDGGa2EzMpWq+0/sjhV1qiihpsiurE91jIk5AZ5MlNDQRXmERUWT/EZWESRc
PWAV6I2bVnwFfJOjawD1z87WcNrFanrl1pJwwxO8zUT04ClrZ54R41XXZFhrsM+Mlq
R7qDETUnLQfnAm+qvIxMnN3MGBlO6b9s0aX9MrMeAsh7fWyPfC7bz6LSH30guHoAdu
Gpr9mJAx1N3hS61HchwesvObfrsCot6vBM/Ns+z8DVwIGBkGhyVdYQadGEwXuhxuFb
BAWymZ/FzRPYA==
X-Mailer: Apple Mail (2.3654.100.0.2.22)
X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391, 18.0.761
definitions=2021-05-31_10:2021-05-31,
2021-05-31 signatures=0
X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0
malwarescore=0
phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 mlxscore=0
mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx
scancount=1 engine=8.0.1-2009150000 definitions=main-2105310118
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <29936AAC-B092-49DC-8A95-FDFF426DC48D@mac.com>
 by: Colin McPhail - Mon, 31 May 2021 15:57 UTC

Hi,

According to the enum module's documentation an Enum-based enumeration's members can have values of any type:

"Member values can be anything: int, str, etc.."

I defined one with functions as member values. This seemed to work as long as the functions were defined ahead of the enumeration's definition. However I then noticed that my enumeration class was a bit weird: even although I could reference the enumeration's members individually by name I couldn't iterate through the members in the normal fashion - it seemed to have no members. Also, calling type() on a member gave the result <class 'function'> instead of the expected <enum 'enum name'>.

I am using Python 3.9.5 from python.org on macOS 11.4. Below is a small test script and its output. Are my expectations wrong or is there a problem with Enum?

Regards,
Colin

-------------------------------
from enum import Enum

def connect_impl():
print("message from connect_impl()")

def help_impl():
print("message from help_impl()")

class Command1(Enum):
CONNECT = 1
HELP = 2

class Command2(Enum):
CONNECT = connect_impl
HELP = help_impl

if __name__ == "__main__":

def print_enum(cls, mbr):
print(f"{cls.__name__}\n type(): {type(Command1)}")
print(f" type of enum member: {type(mbr)}")
print(f" number of members: {len(cls)}")
print(" enum members:")
if len(cls) > 0:
for c in cls:
print(f" {c}")
else:
print(" <none>")
print(f" dir(): {dir(cls)}")

member_1 = Command1.HELP
print_enum(Command1, member_1)
print()

member_2 = Command2.HELP
print_enum(Command2, member_2)
print()
print("call Command2 member: ", end="")
member_2()
print()

-------------------------------
(qt6) % python3 try_enum.py
Command1
type(): <class 'enum.EnumMeta'>
type of enum member: <enum 'Command1'>
number of members: 2
enum members:
Command1.CONNECT
Command1.HELP
dir(): ['CONNECT', 'HELP', '__class__', '__doc__', '__members__', '__module__']

Command2
type(): <class 'enum.EnumMeta'>
type of enum member: <class 'function'>
number of members: 0
enum members:
<none>
dir(): ['__class__', '__doc__', '__members__', '__module__']

call Command2 member: message from help_impl()

(qt6) %
-------------------------------

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor