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

You are fine. You just don't want to overwrite the built-ins if they are a built-in method __init__ __iter__ etc unless you are implementing the functionality expected by those methods.

You are "overwriting" a built in function as a class method, which means you aren't really overwriting anything. This is acceptable.


The whole thing about not shadowing builtin names is that you don't want to stop your self from being able to use them, so when your code does this:

x.set(a) #set the value to a
b = set((1,2,3)) #create a set

you can still access the builtin set so there is no conflict, the only problem is if you wanted to use set inside the class definition

class Entry():
    def __init__(self, value):
        self.set(value)
    def set(self, value):
        self.value=value
    possible_values = set((1,2,3,4,5)) #TypeError: set() missing 1 required positional argument: 'value'

Inside the class definition - and there only - is the built in name shadowed, so you have to consider which you would rather settle for: the unlikely scenario where you need to use set to define a class scope variable and get an error or using a non-intuitive name for your method.

Also note that if you like using method names that make sense to you and also want to use set in your class definition you can still access it with builtins.set for python 3 or __builtin__.set for python 2.

Tags:

Python

Pep8