Sitelet https://github.com/initc3/HoneyBadgerBFT-Python/issues/32
Skip to content

Avoid overwriting Python functions #32

Description

@sbellem

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions