Shadowing a Built-in Function (Part 1, 4: Arithmetic operations)
Nobody has claimed this yet.
Assessment
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Newbie friendliness
- 62/100
- Issue type
- Documentation
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- python
- Domain
- documentation
Research direction
Locate the lesson containing the shown Python arithmetic example and run the snippet, including the commented-out initialization, to observe the built-in shadowing behavior. Update the example and explanation so the accumulator uses a non-shadowing name such as total_sum, while preserving the explanation of the resulting error.
Written by the indexing model from the issue text.
Description
sum = 0
number = int(input("First number: "))
sum = sum + number
number = int(input("Second number: "))
sum = sum + number
number = int(input("Third number: "))
sum = sum + number
print(f"The sum of the numbers: {sum}")
As you may well be aware, sum is a built-in function in Python (used for adding up items in lists, etc.). While Python allows us to use sum as a variable name, it "shadows" the built-in function, which can lead to confusing bugs later in our code. It is safer to use a name like total_sum instead.
For example, comment out the first line and try to run the code. It should result in a NameError, but it raises a TypeError instead. Why? A NameError only occurs when Python cannot find a name anywhere (not in our local variables, global variables, or built-in functions). Because # sum = 0 was commented out, Python didn't find sum as a variable, but it did find the pre-existing built-in function sum() that comes built into Python. Therefore, it didn't raise a NameError.
--
Thank you for this wonderful course!
- Dominant language
- JavaScript
- Stars
- 78
- Forks
- 28
- PR merge metrics
- No merged PRs in 30d
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rage/programming-26
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
rage/programming-26#77 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
rage/programming-26#74 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
rage/programming-26#68 ·
-
A typo in Part 4Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
rage/programming-26#56 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
rage/programming-26#55 ·
All issues in rage/programming-26
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 3 days
-
Add: Atlas TVOpenchannels:add check:passed
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 4 days
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
jaegertracing/jaeger-ui#4547 · 3 comments ·
Maintainers usually reply within 1 day
-
feedback simulation workshop
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
githubnext/gh-aw-workshop#4090 ·
Maintainers usually reply within 1 day
-
bug deck: add to staging level: missing p-feature: Manage Submissions p-feature: Submissions and process priority: MUST HAVE ready for dev lead role: missing size: missing time sensitive
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
hackforla/tdm-calculator#3581 ·
Maintainers usually reply within 2 days