Avoid overwriting Python functions
Opened this issue · 0 comments
From @sbellem on August 17, 2017 23:17
For example the function hash()
in reliablebroadcast.py could be confused with the Python built-in function hash()
. It could perhaps be renamed something like sha256_digest()
just to avoid confusion and potential problems in the future.
Another example is the usage of the callable namedinput()
(e.g. in reliablebroadcast()
:
def reliablebroadcast(sid, pid, N, f, leader, input, receive, send):
"""Reliable broadcast ... """"
# ...
if pid == leader:
# The leader erasure encodes the input, sending one strip to each participant
m = input() # block until an input is received
# ...
input()
is also a Python built-in function. If somehow input()
is the best name, one trick that is often recommended is to suffix the name with an underscore _
. In this case the callable would be renamed input_()
and the above code snippet would become:
def reliablebroadcast(sid, pid, N, f, leader, input, receive, send):
"""Reliable broadcast ... """"
# ...
if pid == leader:
# The leader erasure encodes the input, sending one strip to each participant
m = input_() # block until an input is received
# ...
Notice how the highlighting of the input()
function differs in each code snippet. I assume that is because Github uses MagicPython to highlight the code and MagicPython highlights Python built-in functions.
See related thread https://www.reddit.com/r/pythontips/comments/4m9wiw/avoid_overwriting_python_functions/?st=j6h17gu8&sh=f483ed6e
Also somewhat related Function and method arguments in PEP 8:
If a function argument's name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus class_ is better than clss. (Perhaps better is to avoid such clashes by using a synonym.)
Copied from original issue: amiller/HoneyBadgerBFT#23