Is it bad practice to use a built-in function name as an attribute or method identifier?

前端 未结 4 760
眼角桃花
眼角桃花 2020-11-30 05:50

I know to never use built-in function names as variable identifiers.

But are there any reasons not to use them as attribute or method identifiers?

For examp

4条回答
  •  情话喂你
    2020-11-30 05:51

    I go back and forth on functions a lot when the input variables mimic python builtins. For example, the word bytes is a python builtin, but consider a utility library that parses bytes:

    def parse_bytes(bytes):
        pass
    

    I'd argue this has great readability, but pep8 linters don't like it. Instead I could do

    def parse_bytes(bytearray):
        pass
    
    def parse_bytes(somebytes):
        pass
    

    Or use type hinting

    def parse_bytes(b: bytes):
        pass
    

    But all of these seem worse. Same thing happens if your variable name is input...

    At the end of the day I usually go with somebytes

提交回复
热议问题