How to wrap a function that returns a bitmask in Python

Consider the C int get_events() function, which returns a bit mask for the following events:

 #define EVENT_KEY_DOWN 1 #define EVENT_KEY_UP 2 #define EVENT_KEY_PRESS 4 

It could return 5 , for example, which meant running EVENT_KEY_DOWN and EVENT_KEY_PRESS .

I thought of the following ways to return a value from a function in Python code:

  • "as is", that is 5
  • set of integers: (1, 4)
  • tuple of rows: ('EVENT_KEY_DOWN', 'EVENT_KEY_PRESS')

(In all cases, I could declare constants such as mymodule.EVENT_KEY_DOWN and mymodule.EVENT_KEY_PRESS .)

Is there a "recommended" way?

+5
source share
1 answer

Sparse is better than dense; readability indicator . I would choose a tuple of strings with this logic, but also look at the patterns of how you will use the API. For example, if you use performance-critical code, you should just leave it as a bitmask.

0
source

Source: https://habr.com/ru/post/1214572/


All Articles