Search code examples
pythonreserved-wordsbuilt-in

Is it a bad practice to name a instance variable after a built-in


Having variables named after python built-ins is a bad practice, because it prevents their proper usage, and may confuse readers.

But what about an instance variable:

class MyClass:
    def __init__(self, type_):
        self.type = type_

Is there a good argument against this?


Solution

  • I would say it's ok.

    Grepping through Python's standard library you can see plenty of places where attributes are named the same as built-ins:

    For example:

    :~/cpython/Lib$ egrep -R "\.set[^a-zA-Z0-9_]" | wc -l
    583
    :~/cpython/Lib$ egrep -R "\.type[^a-zA-Z0-9_]" | wc -l
    319