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):
def set(self, value):
def add(self, value):
self.value += value
def sub(self, value):
self.value -= value
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 try to use
set inside the class definition 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.