Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid overwriting Python functions #32

Open
sbellem opened this issue Aug 24, 2018 · 0 comments
Open

Avoid overwriting Python functions #32

sbellem opened this issue Aug 24, 2018 · 0 comments
Assignees
Labels
code quality Coding conventions, etc
Milestone

Comments

@sbellem
Copy link
Collaborator

sbellem commented Aug 24, 2018

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

@sbellem sbellem self-assigned this Aug 24, 2018
@sbellem sbellem added the code quality Coding conventions, etc label Aug 24, 2018
@sbellem sbellem added this to the 1.0 milestone Aug 24, 2018
@sbellem sbellem changed the title [tip] Avoid overwriting Python functions Avoid overwriting Python functions Aug 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality Coding conventions, etc
Projects
None yet
Development

No branches or pull requests

1 participant