List static member with no value in a Protocol class

Question:

This example only prints STATIC_MEMBER_1. It does not list STATIC_MEMBER_2. I’d like a way to list both of them.

class Foo(Protocol):
    MY_STATIC_MEMBER_1: int = 42
    MY_STATIC_MEMBER_2: int

print(dir(Foo))

I’ve tried using inspect and __mro__ but I cannot get it to list STATIC_MEMBER_2.

Asked By: Philip

||

Answers:

vars built-in function does what you need I guess
https://docs.python.org/3/library/functions.html#vars
Btw. your member is not static, it’s a class variable, kinda the same…but different in detail

vars(Foo)

mappingproxy({'__module__': '__main__', '__annotations__': {'MY_STATIC_MEMBER_1': <class 'int'>, 'MY_STATIC_MEMBER_2': <class 'int'>}, 'MY_STATIC_MEMBER_1': 42, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None, '__parameters__': (), '_is_protocol': True, '__subclasshook__': <function Protocol.__init_subclass__.<locals>._proto_hook at 0x7f7db7b9f1a0>, '__init__': <function _no_init_or_replace_init at 0x7f7db77dd4e0>, '__abstractmethods__': frozenset(), '_abc_impl': <_abc._abc_data object at 0x7f7db77ed4c0>})
Answered By: Gameplay
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.