PacktPublishing / Data-Structures-and-Algorithms-The-Complete-Masterclass

Data Structures and Algorithms: The Complete Masterclass, by Packt Publishing
MIT License
50 stars 49 forks source link

Example code shadows builtin #1

Open spamwise opened 1 year ago

spamwise commented 1 year ago

https://github.com/PacktPublishing/Data-Structures-and-Algorithms-The-Complete-Masterclass/blob/fe6e5250cc442bd5b2030c45af0c3d5a7a446cdb/1.%20Big%20O%20Notation/2%20-%20bigo-whileloop.py#L11

Reusing the name of a builtin function for a local variable is technically permitted by Python, but is considered extremely bad practice, to the point where if you were to do this in an interview after advertising yourself as a Python programmer, it would likely disqualify you.

This came to my attention via SO: https://stackoverflow.com/questions/75564166/what-is-shadow-built-in-name-sum and seemed so bad for something that's intended as a guide for beginners that I felt it was important to come and give feedback. My recommendation would be to run all the code in this book through a standard linter; my guess is that other issues will come to light and it will be a simple matter to fix them.

ShubhamSarda commented 1 year ago

Hi Spamwise,

I understand that using sum as a variable name in Python is generally considered bad practice because it can conflict with the built-in sum() function. However, in the specific context of my code, I felt that using sum as a variable name was the most descriptive and intuitive choice. I was working with a small amount of code that wasn't using the built-in sum() function, and using sum as a variable name made it very clear what the purpose of the variable was. If I were working on a larger project or collaborating with others, I would definitely follow Python best practices and use a more descriptive variable name to avoid potential conflicts.

Currently, the course is being followed by people from all programming language backgrounds and sometimes I need to follow terms that are easy to understand by everyone, especially with beginner-level concepts. Thanks for pointing it out, will take care of this in future.