1

I am a long-time C developer who is starting to use Python (more specifically 3.7.12) in some applications.

I am wondering if I can use something similar to the typedef enum structure that we have in C. My point is: It is very useful when we create a variable that can only assume some values and the IDE provide hints to those possible values, like the following example:

typedef enum{
   NO_ERROR_PC          = 0,        
   ERROR_ERROR_PC       = 1,        
   ERROR_CMD_NOT_KNOWN  = 2,
   ...
}PC_COM_ERROR_T;

PC_COM_ERROR_T          f_error;

switch(f_error){
   //... compiler checks if there is anything missing, 
   //or if a case for a value that is not defined has been created...
}

void newFuction(PC_COM_ERROR_T error){
    //makes very clear what values are accepted, provide hints and compiler checks
}

This is also very useful when we need to assign a value to f_error because the IDE already provide hints.

I have been wonder around, but I did not find anything that would perform as well as I need.

5
  • 3
    Look at Enum module Commented Jan 12, 2022 at 19:49
  • It does part of the job indeed. Do you have any more specific advice regarding the Enum module? Commented Jan 13, 2022 at 11:27
  • Since, I am not versed in C, I can't give you anything else. If you can be specific about what you are trying to do with it, maybe I can help. Commented Jan 13, 2022 at 14:03
  • To be more specific, I would like to have something that would limit the scope of possible inputs of a variable or identify, during coding, that one variable is been assignment to a value that is not listed in the Enum. Commented Jan 13, 2022 at 18:41
  • Unlike in C Python does not have strong typing, thus you can never limit the type of data assigned to a variable either during coding or during execution Python does offer type hinting, which helps define the scope of data types a variable can handle within the function signature, but this is not enforced during execution. Commented Jan 13, 2022 at 19:03

1 Answer 1

1

Welcome to Python, and thanks for asking a great question! I think you may want to try type hints. Most IDE's use them to provide very useful hints and linting.

Your code should approximately translate as:

from enum import IntEnum

class PC_COM_ERROR_T(IntEnum):
    NO_ERROR_PC = 0
    ERROR_ERROR_PC = 1
    ERROR_CMD_NOT_KNOWN = 2

f_error = PC_COM_ERROR_T.ERROR_ERROR_PC

def newFuction(error: PC_COM_ERROR_T):
    # Uncomment the line below you absolutely want to have strong typing.
    # assert isinstance(error, PC_COM_ERROR_T) 
    print(error.value)


newFuction(f_error)

As @itprorh66 wrote above, Python does not have strong typing. Coming from C, I understand that you miss the structure it provides. If you find a place where you absolutely have to have strong typing, you can insert assert isinstance(error, PC_COM_ERROR_T) as a function precondition, but I almost never see it being used.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the time and especially for the the isinstance line!
You are most welcome, @FelipeSchneider!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.