Functions as Enum member values
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 instead of the expected . 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("") 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(): type of enum member: number of members: 2 enum members: Command1.CONNECT Command1.HELP dir(): ['CONNECT', 'HELP', '__class__', '__doc__', '__members__', '__module__'] Command2 type(): type of enum member: number of members: 0 enum members: dir(): ['__class__', '__doc__', '__members__', '__module__'] call Command2 member: message from help_impl() (qt6) % --- -- https://mail.python.org/mailman/listinfo/python-list
Re: Functions as Enum member values
> On 31 May 2021, at 18:24, Peter Otten <__pete...@web.de> wrote: > > On 31/05/2021 17:57, Colin McPhail via Python-list wrote: >> 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.." > > You didn't read the fineprint ;) > Ah, neither I did. > """ > The rules for what is allowed are as follows: names that start and end with a > single underscore are reserved by enum and cannot be used; all other > attributes defined within an enumeration will become members of this > enumeration, with the exception of special methods (__str__(), __add__(), > etc.), descriptors (methods are also descriptors), and variable names listed > in _ignore_ > """" > > Functions written in Python are descriptors and therefore cannot be used. > Builtin functions aren't, leading to the following somewhat surprising > difference: > > >>> def foo(self): print("foo") > > >>> class A(Enum): > x = foo # foo.__get__ exists -> foo is a descriptor > # the enum library assumes you are adding a > # method to your > # class, not an enumeration value. > > y = sum # no sum.__get__ attribute -> function > # treated as an enumeration value. > > >>> list(A) > [>] > >>> A.x > > >>> A.y > > > >>> A.y.x() > foo > Thank you for explaining it. Regards, Colin -- https://mail.python.org/mailman/listinfo/python-list
Re: SSL/TLS in Python using STARTTLS and ssl/ssltelnet and telnetlib
> On 7 Nov 2019, at 03:24, Veek M wrote: > > Could someone suggest some introductory reading material that will allow > me to use 'telnetlib' with 'ssl' or 'ssltelnet'. > (currently using Pan since Knode is dropped on Debian) > > I'm trying to write something that will download the NNTP headers over > TLS. > > The idea is to > 1. telnet to port 119, send 'CAPABILITIES\r\n' using telnetlib > 2. then switch to TLS using STARTTLS > 3. I tried just connecting to port 119 using a new TLS connection NOT > OVER telnet and it didn't work. Apparently you need to pass the TLS > context to telnetlib or vice versa. > ... Any reason you're not using nntplib from the Python Standard Library? It supports the STARTTLS command. If you don't want to use nntplib you could look at its code to see how it works. -- Colin -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I place a preset into the text box?
Hi Steve, > On 28 Aug 2020, at 11:03, Steve wrote: > > > The following program compiles but does not quite do what I would like it to > do. Line 19 is the preset information but I do not seem to be able to get it > into the form by code. My purpose is to let the user make changes without > having to re-enter the entire code. I'm no Tk expert but does the following do what you want? (Strictly speaking, the parentheses in ("1234-abcd") are not wrong just unnecessary.) #=== import tkinter as tk from tkinter import ttk import sys window = tk.Tk() window.title("Python Tkinter Text Box") window.minsize(600,400) def Submit(): label.configure(text= 'The new code is: ' + NewCode.get()) def ClickExit(): #This exit closes the program but the form remains and is still active. # I want only to close the form. print("Exiting") #sys.exit() window.destroy() #OldCode = ("1234-abcd") OldCode = "1234-abcd" label = ttk.Label(window, text = "Enter the new code") label.grid(column = 1, row = 1) #NewCode = tk.StringVar() NewCode = tk.StringVar(value=OldCode) CodeEntered = ttk.Entry(window, width = 15, textvariable = NewCode) CodeEntered.grid(column = 2, row = 3) button = ttk.Button(window, text = "Submit", command = Submit) button.grid(column= 2, row = 5) button = ttk.Button(window, text = "Quit", command = ClickExit) button.grid(column= 2, row = 7) window.mainloop() x = (NewCode.get()) print("The new code entered is: " + x) #= Regards, Colin -- https://mail.python.org/mailman/listinfo/python-list