[Python] Can I use a builtin name as a method name of a Python class?

Discussão em 'Python' iniciado por Stack, Agosto 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have a class that performs some simple data manipulation, I need three methods: set, add, sub:

    class Entry(): # over-simplified but should be enough for the question
    def __init__(self, value):
    self.set(value)
    def set(self, value):
    self.value=value
    def add(self, value):
    self.value += value
    def sub(self, value):
    self.value -= value


    The problem is with the "set" method but defining it as a class method should not clash with the "set()" builtin function.

    The Python Style Guide states that argument names of functions and methods should not shadow built-in functions, but it's this the case for method names?

    Obviously I could choose another method name, but the question is more generic and valid for other possible method names (i.e. filter, sum, input).

    Continue reading...

Compartilhe esta Página